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 | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 43 | #include <import/ebpttree.h> |
| 44 | #include <import/ebsttree.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 45 | #include <import/lru.h> |
| 46 | #include <import/xxhash.h> |
| 47 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 48 | #include <haproxy/api.h> |
| 49 | #include <haproxy/arg.h> |
| 50 | #include <haproxy/base64.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 51 | #include <haproxy/channel.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 52 | #include <haproxy/chunk.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 53 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 54 | #include <haproxy/connection.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 55 | #include <haproxy/dynbuf.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 56 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 57 | #include <haproxy/fd.h> |
| 58 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 59 | #include <haproxy/frontend.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 60 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 61 | #include <haproxy/http_rules.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 62 | #include <haproxy/log.h> |
Willy Tarreau | 6019fab | 2020-05-27 16:26:00 +0200 | [diff] [blame] | 63 | #include <haproxy/openssl-compat.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 64 | #include <haproxy/pattern-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 65 | #include <haproxy/proto_tcp.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 66 | #include <haproxy/proxy.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 67 | #include <haproxy/server.h> |
Willy Tarreau | 334099c | 2020-06-03 18:38:48 +0200 | [diff] [blame] | 68 | #include <haproxy/shctx.h> |
Willy Tarreau | 47d7f90 | 2020-06-04 14:25:47 +0200 | [diff] [blame] | 69 | #include <haproxy/ssl_ckch.h> |
Willy Tarreau | 52d8872 | 2020-06-04 14:29:23 +0200 | [diff] [blame] | 70 | #include <haproxy/ssl_crtlist.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 71 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | b2bd865 | 2020-06-04 14:21:22 +0200 | [diff] [blame] | 72 | #include <haproxy/ssl_utils.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 73 | #include <haproxy/stats-t.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 74 | #include <haproxy/stream-t.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 75 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 76 | #include <haproxy/task.h> |
Willy Tarreau | c2f7c58 | 2020-06-02 18:15:32 +0200 | [diff] [blame] | 77 | #include <haproxy/ticks.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 78 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 79 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 80 | #include <haproxy/vars.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 81 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 83 | /* ***** READ THIS before adding code here! ***** |
| 84 | * |
| 85 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 86 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 87 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 88 | * exclusively so that the whole code consistently uses the same macros. |
| 89 | * |
| 90 | * Whenever possible if a macro is missing in certain versions, it's better |
| 91 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 92 | */ |
| 93 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 94 | int sslconns = 0; |
| 95 | int totalsslconns = 0; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 96 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 97 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 98 | static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */ |
| 99 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 100 | struct global_ssl global_ssl = { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 101 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 102 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 103 | #endif |
| 104 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 105 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 106 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 107 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 108 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 109 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 110 | #endif |
| 111 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 112 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 113 | #endif |
| 114 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 115 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 116 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 117 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 118 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 119 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 120 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 121 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 122 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 123 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 124 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 125 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 126 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 127 | #endif |
| 128 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 129 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 130 | .capture_cipherlist = 0, |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 131 | .extra_files = SSL_GF_ALL, |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 132 | .extra_files_noext = 0, |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 133 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 134 | .keylog = 0 |
| 135 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 136 | }; |
| 137 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 138 | static BIO_METHOD *ha_meth; |
| 139 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 140 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 141 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 142 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Willy Tarreau | 9a5a0e0 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 143 | |
| 144 | /* ssl_sock_io_cb is exported to see it resolved in "show fd" */ |
| 145 | struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
| 146 | |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 147 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 148 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 149 | /* Methods to implement OpenSSL BIO */ |
| 150 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 151 | { |
| 152 | struct buffer tmpbuf; |
| 153 | struct ssl_sock_ctx *ctx; |
| 154 | int ret; |
| 155 | |
| 156 | ctx = BIO_get_data(h); |
| 157 | tmpbuf.size = num; |
| 158 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 159 | tmpbuf.data = num; |
| 160 | tmpbuf.head = 0; |
| 161 | ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 162 | if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_WR_SH))) { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 163 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 164 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 165 | } else if (ret == 0) |
| 166 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 171 | { |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | static int ha_ssl_puts(BIO *h, const char *str) |
| 177 | { |
| 178 | |
| 179 | return ha_ssl_write(h, str, strlen(str)); |
| 180 | } |
| 181 | |
| 182 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 183 | { |
| 184 | struct buffer tmpbuf; |
| 185 | struct ssl_sock_ctx *ctx; |
| 186 | int ret; |
| 187 | |
| 188 | ctx = BIO_get_data(h); |
| 189 | tmpbuf.size = size; |
| 190 | tmpbuf.area = buf; |
| 191 | tmpbuf.data = 0; |
| 192 | tmpbuf.head = 0; |
| 193 | ret = ctx->xprt->rcv_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, size, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 194 | if (ret == 0 && !(ctx->conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH))) { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 195 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 196 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 197 | } else if (ret == 0) |
| 198 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 199 | |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 204 | { |
| 205 | int ret = 0; |
| 206 | switch (cmd) { |
| 207 | case BIO_CTRL_DUP: |
| 208 | case BIO_CTRL_FLUSH: |
| 209 | ret = 1; |
| 210 | break; |
| 211 | } |
| 212 | return ret; |
| 213 | } |
| 214 | |
| 215 | static int ha_ssl_new(BIO *h) |
| 216 | { |
| 217 | BIO_set_init(h, 1); |
| 218 | BIO_set_data(h, NULL); |
| 219 | BIO_clear_flags(h, ~0); |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | static int ha_ssl_free(BIO *data) |
| 224 | { |
| 225 | |
| 226 | return 1; |
| 227 | } |
| 228 | |
| 229 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 230 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 231 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 232 | static HA_RWLOCK_T *ssl_rwlocks; |
| 233 | |
| 234 | |
| 235 | unsigned long ssl_id_function(void) |
| 236 | { |
| 237 | return (unsigned long)tid; |
| 238 | } |
| 239 | |
| 240 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 241 | { |
| 242 | if (mode & CRYPTO_LOCK) { |
| 243 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 244 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 245 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 246 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 247 | } |
| 248 | else { |
| 249 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 250 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 251 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 252 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | static int ssl_locking_init(void) |
| 257 | { |
| 258 | int i; |
| 259 | |
| 260 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 261 | if (!ssl_rwlocks) |
| 262 | return -1; |
| 263 | |
| 264 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 265 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 266 | |
| 267 | CRYPTO_set_id_callback(ssl_id_function); |
| 268 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 269 | |
| 270 | return 0; |
| 271 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 272 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 273 | #endif |
| 274 | |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 275 | __decl_thread(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 276 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 277 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 278 | /* |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 279 | * deduplicate cafile (and crlfile) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 280 | */ |
| 281 | struct cafile_entry { |
| 282 | X509_STORE *ca_store; |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 283 | STACK_OF(X509_NAME) *ca_list; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 284 | struct ebmb_node node; |
| 285 | char path[0]; |
| 286 | }; |
| 287 | |
| 288 | static struct eb_root cafile_tree = EB_ROOT_UNIQUE; |
| 289 | |
| 290 | static X509_STORE* ssl_store_get0_locations_file(char *path) |
| 291 | { |
| 292 | struct ebmb_node *eb; |
| 293 | |
| 294 | eb = ebst_lookup(&cafile_tree, path); |
| 295 | if (eb) { |
| 296 | struct cafile_entry *ca_e; |
| 297 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 298 | return ca_e->ca_store; |
| 299 | } |
| 300 | return NULL; |
| 301 | } |
| 302 | |
Remi Tricot-Le Breton | bcec63e | 2021-03-23 16:41:53 +0100 | [diff] [blame] | 303 | int ssl_store_load_locations_file(char *path, int create_if_none) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 304 | { |
Remi Tricot-Le Breton | bcec63e | 2021-03-23 16:41:53 +0100 | [diff] [blame] | 305 | X509_STORE *store = ssl_store_get0_locations_file(path); |
| 306 | |
| 307 | /* If this function is called by the CLI, we should not call the |
| 308 | * X509_STORE_load_locations function because it performs forbidden disk |
| 309 | * accesses. */ |
| 310 | if (!store && create_if_none) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 311 | struct cafile_entry *ca_e; |
Remi Tricot-Le Breton | bcec63e | 2021-03-23 16:41:53 +0100 | [diff] [blame] | 312 | store = X509_STORE_new(); |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 313 | if (X509_STORE_load_locations(store, path, NULL)) { |
| 314 | int pathlen; |
| 315 | pathlen = strlen(path); |
| 316 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 317 | if (ca_e) { |
| 318 | memcpy(ca_e->path, path, pathlen + 1); |
| 319 | ca_e->ca_store = store; |
| 320 | ebst_insert(&cafile_tree, &ca_e->node); |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 321 | } |
Remi Tricot-Le Breton | bcec63e | 2021-03-23 16:41:53 +0100 | [diff] [blame] | 322 | } else { |
| 323 | X509_STORE_free(store); |
| 324 | store = NULL; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 325 | } |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 326 | } |
Remi Tricot-Le Breton | bcec63e | 2021-03-23 16:41:53 +0100 | [diff] [blame] | 327 | return (store != NULL); |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 331 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 332 | { |
| 333 | X509_STORE *store; |
| 334 | store = ssl_store_get0_locations_file(path); |
| 335 | if (store_ctx && store) { |
| 336 | int i; |
| 337 | X509_OBJECT *obj; |
| 338 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 339 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 340 | obj = sk_X509_OBJECT_value(objs, i); |
| 341 | switch (X509_OBJECT_get_type(obj)) { |
| 342 | case X509_LU_X509: |
| 343 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 344 | break; |
| 345 | case X509_LU_CRL: |
| 346 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 347 | break; |
| 348 | default: |
| 349 | break; |
| 350 | } |
| 351 | } |
| 352 | return 1; |
| 353 | } |
| 354 | return 0; |
| 355 | } |
| 356 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 357 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 358 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 359 | { |
| 360 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 361 | return ssl_set_cert_crl_file(store_ctx, path); |
| 362 | } |
| 363 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 364 | /* |
| 365 | Extract CA_list from CA_file already in tree. |
| 366 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 367 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 368 | */ |
| 369 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 370 | { |
| 371 | struct ebmb_node *eb; |
| 372 | struct cafile_entry *ca_e; |
| 373 | |
| 374 | eb = ebst_lookup(&cafile_tree, path); |
| 375 | if (!eb) |
| 376 | return NULL; |
| 377 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 378 | |
| 379 | if (ca_e->ca_list == NULL) { |
| 380 | int i; |
| 381 | unsigned long key; |
| 382 | struct eb_root ca_name_tree = EB_ROOT; |
| 383 | struct eb64_node *node, *back; |
| 384 | struct { |
| 385 | struct eb64_node node; |
| 386 | X509_NAME *xname; |
| 387 | } *ca_name; |
| 388 | STACK_OF(X509_OBJECT) *objs; |
| 389 | STACK_OF(X509_NAME) *skn; |
| 390 | X509 *x; |
| 391 | X509_NAME *xn; |
| 392 | |
| 393 | skn = sk_X509_NAME_new_null(); |
| 394 | /* take x509 from cafile_tree */ |
| 395 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 396 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 397 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 398 | if (!x) |
| 399 | continue; |
| 400 | xn = X509_get_subject_name(x); |
| 401 | if (!xn) |
| 402 | continue; |
| 403 | /* Check for duplicates. */ |
| 404 | key = X509_NAME_hash(xn); |
| 405 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 406 | node && ca_name == NULL; |
| 407 | node = eb64_next(node)) { |
| 408 | ca_name = container_of(node, typeof(*ca_name), node); |
| 409 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 410 | ca_name = NULL; |
| 411 | } |
| 412 | /* find a duplicate */ |
| 413 | if (ca_name) |
| 414 | continue; |
| 415 | ca_name = calloc(1, sizeof *ca_name); |
| 416 | xn = X509_NAME_dup(xn); |
| 417 | if (!ca_name || |
| 418 | !xn || |
| 419 | !sk_X509_NAME_push(skn, xn)) { |
| 420 | free(ca_name); |
| 421 | X509_NAME_free(xn); |
| 422 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 423 | sk_X509_NAME_free(skn); |
| 424 | skn = NULL; |
| 425 | break; |
| 426 | } |
| 427 | ca_name->node.key = key; |
| 428 | ca_name->xname = xn; |
| 429 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 430 | } |
| 431 | ca_e->ca_list = skn; |
| 432 | /* remove temporary ca_name tree */ |
| 433 | node = eb64_first(&ca_name_tree); |
| 434 | while (node) { |
| 435 | ca_name = container_of(node, typeof(*ca_name), node); |
| 436 | back = eb64_next(node); |
| 437 | eb64_delete(node); |
| 438 | free(ca_name); |
| 439 | node = back; |
| 440 | } |
| 441 | } |
| 442 | return ca_e->ca_list; |
| 443 | } |
| 444 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 445 | struct pool_head *pool_head_ssl_capture = NULL; |
William Lallemand | 15e1694 | 2020-05-15 00:25:08 +0200 | [diff] [blame] | 446 | int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 447 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 448 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 449 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 450 | int ssl_keylog_index = -1; |
| 451 | struct pool_head *pool_head_ssl_keylog = NULL; |
| 452 | struct pool_head *pool_head_ssl_keylog_str = NULL; |
| 453 | #endif |
| 454 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 455 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 456 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 457 | #endif |
| 458 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 459 | #ifndef OPENSSL_NO_ENGINE |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 460 | unsigned int openssl_engines_initialized; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 461 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 462 | struct ssl_engine_list { |
| 463 | struct list list; |
| 464 | ENGINE *e; |
| 465 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 466 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 467 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 468 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 469 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 470 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 471 | static DH *local_dh_1024 = NULL; |
| 472 | static DH *local_dh_2048 = NULL; |
| 473 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 474 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 475 | #endif /* OPENSSL_NO_DH */ |
| 476 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 477 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 478 | /* X509V3 Extensions that will be added on generated certificates */ |
| 479 | #define X509V3_EXT_SIZE 5 |
| 480 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 481 | "basicConstraints", |
| 482 | "nsComment", |
| 483 | "subjectKeyIdentifier", |
| 484 | "authorityKeyIdentifier", |
| 485 | "keyUsage", |
| 486 | }; |
| 487 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 488 | "CA:FALSE", |
| 489 | "\"OpenSSL Generated Certificate\"", |
| 490 | "hash", |
| 491 | "keyid,issuer:always", |
| 492 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 493 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 494 | /* LRU cache to store generated certificate */ |
| 495 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 496 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 497 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 498 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 499 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 500 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 501 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 502 | /* The order here matters for picking a default context, |
| 503 | * keep the most common keytype at the bottom of the list |
| 504 | */ |
| 505 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 506 | "dsa", |
| 507 | "ecdsa", |
| 508 | "rsa" |
| 509 | }; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 510 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 511 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 512 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 513 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 514 | /* Dedicated callback functions for heartbeat and clienthello. |
| 515 | */ |
| 516 | #ifdef TLS1_RT_HEARTBEAT |
| 517 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 518 | int content_type, const void *buf, size_t len, |
| 519 | SSL *ssl); |
| 520 | #endif |
| 521 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 522 | int content_type, const void *buf, size_t len, |
| 523 | SSL *ssl); |
| 524 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 525 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 526 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 527 | int content_type, const void *buf, size_t len, |
| 528 | SSL *ssl); |
| 529 | #endif |
| 530 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 531 | /* List head of all registered SSL/TLS protocol message callbacks. */ |
| 532 | struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks); |
| 533 | |
| 534 | /* Registers the function <func> in order to be called on SSL/TLS protocol |
| 535 | * message processing. It will return 0 if the function <func> is not set |
| 536 | * or if it fails to allocate memory. |
| 537 | */ |
| 538 | int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func) |
| 539 | { |
| 540 | struct ssl_sock_msg_callback *cbk; |
| 541 | |
| 542 | if (!func) |
| 543 | return 0; |
| 544 | |
| 545 | cbk = calloc(1, sizeof(*cbk)); |
| 546 | if (!cbk) { |
| 547 | ha_alert("out of memory in ssl_sock_register_msg_callback().\n"); |
| 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | cbk->func = func; |
| 552 | |
| 553 | LIST_ADDQ(&ssl_sock_msg_callbacks, &cbk->list); |
| 554 | |
| 555 | return 1; |
| 556 | } |
| 557 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 558 | /* Used to register dedicated SSL/TLS protocol message callbacks. |
| 559 | */ |
| 560 | static int ssl_sock_register_msg_callbacks(void) |
| 561 | { |
| 562 | #ifdef TLS1_RT_HEARTBEAT |
| 563 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat)) |
| 564 | return ERR_ABORT; |
| 565 | #endif |
| 566 | if (global_ssl.capture_cipherlist > 0) { |
| 567 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello)) |
| 568 | return ERR_ABORT; |
| 569 | } |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 570 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 571 | if (global_ssl.keylog > 0) { |
| 572 | if (!ssl_sock_register_msg_callback(ssl_init_keylog)) |
| 573 | return ERR_ABORT; |
| 574 | } |
| 575 | #endif |
| 576 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 577 | return 0; |
| 578 | } |
| 579 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 580 | /* Used to free all SSL/TLS protocol message callbacks that were |
| 581 | * registered by using ssl_sock_register_msg_callback(). |
| 582 | */ |
| 583 | static void ssl_sock_unregister_msg_callbacks(void) |
| 584 | { |
| 585 | struct ssl_sock_msg_callback *cbk, *cbkback; |
| 586 | |
| 587 | list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) { |
| 588 | LIST_DEL(&cbk->list); |
| 589 | free(cbk); |
| 590 | } |
| 591 | } |
| 592 | |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 593 | SSL *ssl_sock_get_ssl_object(struct connection *conn) |
| 594 | { |
| 595 | if (!ssl_sock_is_ssl(conn)) |
| 596 | return NULL; |
| 597 | |
| 598 | return ((struct ssl_sock_ctx *)(conn->xprt_ctx))->ssl; |
| 599 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 600 | /* |
| 601 | * This function gives the detail of the SSL error. It is used only |
| 602 | * if the debug mode and the verbose mode are activated. It dump all |
| 603 | * the SSL error until the stack was empty. |
| 604 | */ |
| 605 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 606 | { |
| 607 | unsigned long ret; |
| 608 | |
| 609 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 610 | while(1) { |
| 611 | ret = ERR_get_error(); |
| 612 | if (ret == 0) |
| 613 | return; |
Willy Tarreau | 07451ac | 2021-03-02 19:32:39 +0100 | [diff] [blame] | 614 | fprintf(stderr, "fd[%#x] OpenSSL error[0x%lx] %s: %s\n", |
| 615 | conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 616 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 617 | } |
| 618 | } |
| 619 | } |
| 620 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 621 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 622 | #ifndef OPENSSL_NO_ENGINE |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 623 | 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] | 624 | { |
| 625 | int err_code = ERR_ABORT; |
| 626 | ENGINE *engine; |
| 627 | struct ssl_engine_list *el; |
| 628 | |
| 629 | /* grab the structural reference to the engine */ |
| 630 | engine = ENGINE_by_id(engine_id); |
| 631 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 632 | 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] | 633 | goto fail_get; |
| 634 | } |
| 635 | |
| 636 | if (!ENGINE_init(engine)) { |
| 637 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 638 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 639 | goto fail_init; |
| 640 | } |
| 641 | |
| 642 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 643 | 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] | 644 | goto fail_set_method; |
| 645 | } |
| 646 | |
| 647 | el = calloc(1, sizeof(*el)); |
| 648 | el->e = engine; |
| 649 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 650 | nb_engines++; |
| 651 | if (global_ssl.async) |
| 652 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 653 | return 0; |
| 654 | |
| 655 | fail_set_method: |
| 656 | /* release the functional reference from ENGINE_init() */ |
| 657 | ENGINE_finish(engine); |
| 658 | |
| 659 | fail_init: |
| 660 | /* release the structural reference from ENGINE_by_id() */ |
| 661 | ENGINE_free(engine); |
| 662 | |
| 663 | fail_get: |
| 664 | return err_code; |
| 665 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 666 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 667 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 668 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 669 | /* |
| 670 | * openssl async fd handler |
| 671 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 672 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 673 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 674 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 675 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 676 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 677 | * to poll this fd until it is requested |
| 678 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 679 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 680 | fd_cant_recv(fd); |
| 681 | |
| 682 | /* crypto engine is available, let's notify the associated |
| 683 | * connection that it can pursue its processing. |
| 684 | */ |
Olivier Houchard | a459826 | 2020-09-15 22:16:02 +0200 | [diff] [blame] | 685 | tasklet_wakeup(ctx->wait_event.tasklet); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 688 | /* |
| 689 | * openssl async delayed SSL_free handler |
| 690 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 691 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 692 | { |
| 693 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 694 | OSSL_ASYNC_FD all_fd[32]; |
| 695 | size_t num_all_fds = 0; |
| 696 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 697 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 698 | /* We suppose that the async job for a same SSL * |
| 699 | * are serialized. So if we are awake it is |
| 700 | * because the running job has just finished |
| 701 | * and we can remove all async fds safely |
| 702 | */ |
| 703 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 704 | if (num_all_fds > 32) { |
| 705 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 706 | return; |
| 707 | } |
| 708 | |
| 709 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 710 | for (i=0 ; i < num_all_fds ; i++) |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 711 | fd_stop_both(all_fd[i]); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 712 | |
| 713 | /* 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] | 714 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 715 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 716 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 717 | } |
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 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 720 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 721 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 722 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 723 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 724 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 725 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 726 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 727 | size_t num_add_fds = 0; |
| 728 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 729 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 730 | |
| 731 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 732 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 733 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 734 | 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] | 735 | return; |
| 736 | } |
| 737 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 738 | 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] | 739 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 740 | /* We remove unused fds from the fdtab */ |
| 741 | for (i=0 ; i < num_del_fds ; i++) |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 742 | fd_stop_both(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 743 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 744 | /* We add new fds to the fdtab */ |
| 745 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 746 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 749 | num_add_fds = 0; |
| 750 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 751 | if (num_add_fds > 32) { |
| 752 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 753 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 754 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 755 | |
| 756 | /* We activate the polling for all known async fds */ |
| 757 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 758 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 759 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 760 | /* To ensure that the fd cache won't be used |
| 761 | * We'll prefer to catch a real RD event |
| 762 | * because handling an EAGAIN on this fd will |
| 763 | * result in a context switch and also |
| 764 | * some engines uses a fd in blocking mode. |
| 765 | */ |
| 766 | fd_cant_recv(add_fd[i]); |
| 767 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 768 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 769 | } |
| 770 | #endif |
| 771 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 772 | #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] | 773 | /* |
| 774 | * This function returns the number of seconds elapsed |
| 775 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 776 | * date presented un ASN1_GENERALIZEDTIME. |
| 777 | * |
| 778 | * In parsing error case, it returns -1. |
| 779 | */ |
| 780 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 781 | { |
| 782 | long epoch; |
| 783 | char *p, *end; |
| 784 | const unsigned short month_offset[12] = { |
| 785 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 786 | }; |
| 787 | int year, month; |
| 788 | |
| 789 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 790 | |
| 791 | p = (char *)d->data; |
| 792 | end = p + d->length; |
| 793 | |
| 794 | if (end - p < 4) return -1; |
| 795 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 796 | p += 4; |
| 797 | if (end - p < 2) return -1; |
| 798 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 799 | if (month < 1 || month > 12) return -1; |
| 800 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 801 | We consider leap years and the current month (<marsh or not) */ |
| 802 | epoch = ( ((year - 1970) * 365) |
| 803 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 804 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 805 | + month_offset[month-1] |
| 806 | ) * 24 * 60 * 60; |
| 807 | p += 2; |
| 808 | if (end - p < 2) return -1; |
| 809 | /* Add the number of seconds of completed days of current month */ |
| 810 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 811 | p += 2; |
| 812 | if (end - p < 2) return -1; |
| 813 | /* Add the completed hours of the current day */ |
| 814 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 815 | p += 2; |
| 816 | if (end - p < 2) return -1; |
| 817 | /* Add the completed minutes of the current hour */ |
| 818 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 819 | p += 2; |
| 820 | if (p == end) return -1; |
| 821 | /* Test if there is available seconds */ |
| 822 | if (p[0] < '0' || p[0] > '9') |
| 823 | goto nosec; |
| 824 | if (end - p < 2) return -1; |
| 825 | /* Add the seconds of the current minute */ |
| 826 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 827 | p += 2; |
| 828 | if (p == end) return -1; |
| 829 | /* Ignore seconds float part if present */ |
| 830 | if (p[0] == '.') { |
| 831 | do { |
| 832 | if (++p == end) return -1; |
| 833 | } while (p[0] >= '0' && p[0] <= '9'); |
| 834 | } |
| 835 | |
| 836 | nosec: |
| 837 | if (p[0] == 'Z') { |
| 838 | if (end - p != 1) return -1; |
| 839 | return epoch; |
| 840 | } |
| 841 | else if (p[0] == '+') { |
| 842 | if (end - p != 5) return -1; |
| 843 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 844 | 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] | 845 | } |
| 846 | else if (p[0] == '-') { |
| 847 | if (end - p != 5) return -1; |
| 848 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 849 | 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] | 850 | } |
| 851 | |
| 852 | return -1; |
| 853 | } |
| 854 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 855 | /* |
| 856 | * struct alignment works here such that the key.key is the same as key_data |
| 857 | * Do not change the placement of key_data |
| 858 | */ |
| 859 | struct certificate_ocsp { |
| 860 | struct ebmb_node key; |
| 861 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 862 | struct buffer response; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 863 | int refcount; |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 864 | long expire; |
| 865 | }; |
| 866 | |
| 867 | struct ocsp_cbk_arg { |
| 868 | int is_single; |
| 869 | int single_kt; |
| 870 | union { |
| 871 | struct certificate_ocsp *s_ocsp; |
| 872 | /* |
| 873 | * m_ocsp will have multiple entries dependent on key type |
| 874 | * Entry 0 - DSA |
| 875 | * Entry 1 - ECDSA |
| 876 | * Entry 2 - RSA |
| 877 | */ |
| 878 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 879 | }; |
| 880 | }; |
| 881 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 882 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 883 | |
| 884 | /* This function starts to check if the OCSP response (in DER format) contained |
| 885 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 886 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 887 | * contained in the OCSP Response and exits on error if no match. |
| 888 | * If it's a valid OCSP Response: |
| 889 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 890 | * pointed by 'ocsp'. |
| 891 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 892 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 893 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 894 | * already present in the container, it will be overwritten. |
| 895 | * |
| 896 | * Note: OCSP response containing more than one OCSP Single response is not |
| 897 | * considered valid. |
| 898 | * |
| 899 | * Returns 0 on success, 1 in error case. |
| 900 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 901 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 902 | struct certificate_ocsp *ocsp, |
| 903 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 904 | { |
| 905 | OCSP_RESPONSE *resp; |
| 906 | OCSP_BASICRESP *bs = NULL; |
| 907 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 908 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 909 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 910 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 911 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 912 | int reason; |
| 913 | int ret = 1; |
| 914 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 915 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 916 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 917 | if (!resp) { |
| 918 | memprintf(err, "Unable to parse OCSP response"); |
| 919 | goto out; |
| 920 | } |
| 921 | |
| 922 | rc = OCSP_response_status(resp); |
| 923 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 924 | memprintf(err, "OCSP response status not successful"); |
| 925 | goto out; |
| 926 | } |
| 927 | |
| 928 | bs = OCSP_response_get1_basic(resp); |
| 929 | if (!bs) { |
| 930 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 931 | goto out; |
| 932 | } |
| 933 | |
| 934 | count_sr = OCSP_resp_count(bs); |
| 935 | if (count_sr > 1) { |
| 936 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 937 | goto out; |
| 938 | } |
| 939 | |
| 940 | sr = OCSP_resp_get0(bs, 0); |
| 941 | if (!sr) { |
| 942 | memprintf(err, "Failed to get OCSP single response"); |
| 943 | goto out; |
| 944 | } |
| 945 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 946 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 947 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 948 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 949 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 950 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 951 | goto out; |
| 952 | } |
| 953 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 954 | if (!nextupd) { |
| 955 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 956 | goto out; |
| 957 | } |
| 958 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 959 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 960 | if (!rc) { |
| 961 | memprintf(err, "OCSP single response: no longer valid."); |
| 962 | goto out; |
| 963 | } |
| 964 | |
| 965 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 966 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 967 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 968 | goto out; |
| 969 | } |
| 970 | } |
| 971 | |
| 972 | if (!ocsp) { |
| 973 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 974 | unsigned char *p; |
| 975 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 976 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 977 | if (!rc) { |
| 978 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 979 | goto out; |
| 980 | } |
| 981 | |
| 982 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 983 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 984 | goto out; |
| 985 | } |
| 986 | |
| 987 | p = key; |
| 988 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 989 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 990 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 991 | if (!ocsp) { |
| 992 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 993 | goto out; |
| 994 | } |
| 995 | } |
| 996 | |
| 997 | /* According to comments on "chunk_dup", the |
| 998 | previous chunk buffer will be freed */ |
| 999 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 1000 | memprintf(err, "OCSP response: Memory allocation error"); |
| 1001 | goto out; |
| 1002 | } |
| 1003 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1004 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 1005 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1006 | ret = 0; |
| 1007 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 1008 | ERR_clear_error(); |
| 1009 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1010 | if (bs) |
| 1011 | OCSP_BASICRESP_free(bs); |
| 1012 | |
| 1013 | if (resp) |
| 1014 | OCSP_RESPONSE_free(resp); |
| 1015 | |
| 1016 | return ret; |
| 1017 | } |
| 1018 | /* |
| 1019 | * External function use to update the OCSP response in the OCSP response's |
| 1020 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 1021 | * to update in DER format. |
| 1022 | * |
| 1023 | * Returns 0 on success, 1 in error case. |
| 1024 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1025 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1026 | { |
| 1027 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1028 | } |
| 1029 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1030 | #endif |
| 1031 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1032 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 1033 | 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) |
| 1034 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1035 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1036 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1037 | struct connection *conn; |
| 1038 | int head; |
| 1039 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1040 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1041 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1042 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1043 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1044 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1045 | |
| 1046 | keys = ref->tlskeys; |
| 1047 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1048 | |
| 1049 | if (enc) { |
| 1050 | memcpy(key_name, keys[head].name, 16); |
| 1051 | |
| 1052 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1053 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1054 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1055 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1056 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1057 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1058 | goto end; |
| 1059 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1060 | 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] | 1061 | ret = 1; |
| 1062 | } |
| 1063 | else if (ref->key_size_bits == 256 ) { |
| 1064 | |
| 1065 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1066 | goto end; |
| 1067 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1068 | 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] | 1069 | ret = 1; |
| 1070 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1071 | } else { |
| 1072 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1073 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1074 | goto found; |
| 1075 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1076 | ret = 0; |
| 1077 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1078 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1079 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1080 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1081 | 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] | 1082 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1083 | goto end; |
| 1084 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1085 | ret = i ? 2 : 1; |
| 1086 | } |
| 1087 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1088 | 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] | 1089 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1090 | goto end; |
| 1091 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1092 | ret = i ? 2 : 1; |
| 1093 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1094 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1095 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1096 | end: |
| 1097 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1098 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1102 | { |
| 1103 | struct tls_keys_ref *ref; |
| 1104 | |
| 1105 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1106 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1107 | return ref; |
| 1108 | return NULL; |
| 1109 | } |
| 1110 | |
| 1111 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1112 | { |
| 1113 | struct tls_keys_ref *ref; |
| 1114 | |
| 1115 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1116 | if (ref->unique_id == unique_id) |
| 1117 | return ref; |
| 1118 | return NULL; |
| 1119 | } |
| 1120 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1121 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1122 | * match existing ones, this function returns -1 |
| 1123 | * else it returns 0 on success. |
| 1124 | */ |
| 1125 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1126 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1127 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1128 | if (ref->key_size_bits == 128) { |
| 1129 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1130 | return -1; |
| 1131 | } |
| 1132 | else if (ref->key_size_bits == 256) { |
| 1133 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1134 | return -1; |
| 1135 | } |
| 1136 | else |
| 1137 | return -1; |
| 1138 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1139 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1140 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1141 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1142 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1143 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1144 | |
| 1145 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1146 | } |
| 1147 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1148 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1149 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1150 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1151 | |
| 1152 | if(!ref) { |
| 1153 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1154 | return 1; |
| 1155 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1156 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1157 | memprintf(err, "Invalid key size"); |
| 1158 | return 1; |
| 1159 | } |
| 1160 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1161 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1162 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1163 | |
| 1164 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1165 | * automatic ids. It's called just after the basic checks. It returns |
| 1166 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1167 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1168 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1169 | { |
| 1170 | int i = 0; |
| 1171 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1172 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1173 | |
| 1174 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1175 | if (ref->unique_id == -1) { |
| 1176 | /* Look for the first free id. */ |
| 1177 | while (1) { |
| 1178 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1179 | if (ref2->unique_id == i) { |
| 1180 | i++; |
| 1181 | break; |
| 1182 | } |
| 1183 | } |
| 1184 | if (&ref2->list == &tlskeys_reference) |
| 1185 | break; |
| 1186 | } |
| 1187 | |
| 1188 | /* Uses the unique id and increment it for the next entry. */ |
| 1189 | ref->unique_id = i; |
| 1190 | i++; |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | /* This sort the reference list by id. */ |
| 1195 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1196 | LIST_DEL(&ref->list); |
| 1197 | list_for_each_entry(ref3, &tkr, list) { |
| 1198 | if (ref->unique_id < ref3->unique_id) { |
| 1199 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1200 | break; |
| 1201 | } |
| 1202 | } |
| 1203 | if (&ref3->list == &tkr) |
| 1204 | LIST_ADDQ(&tkr, &ref->list); |
| 1205 | } |
| 1206 | |
| 1207 | /* swap root */ |
| 1208 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1209 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1210 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1211 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1212 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1213 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1214 | #ifndef OPENSSL_NO_OCSP |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1215 | int ocsp_ex_index = -1; |
| 1216 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1217 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1218 | { |
| 1219 | switch (evp_keytype) { |
| 1220 | case EVP_PKEY_RSA: |
| 1221 | return 2; |
| 1222 | case EVP_PKEY_DSA: |
| 1223 | return 0; |
| 1224 | case EVP_PKEY_EC: |
| 1225 | return 1; |
| 1226 | } |
| 1227 | |
| 1228 | return -1; |
| 1229 | } |
| 1230 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1231 | /* |
| 1232 | * Callback used to set OCSP status extension content in server hello. |
| 1233 | */ |
| 1234 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1235 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1236 | struct certificate_ocsp *ocsp; |
| 1237 | struct ocsp_cbk_arg *ocsp_arg; |
| 1238 | char *ssl_buf; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1239 | SSL_CTX *ctx; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1240 | EVP_PKEY *ssl_pkey; |
| 1241 | int key_type; |
| 1242 | int index; |
| 1243 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1244 | ctx = SSL_get_SSL_CTX(ssl); |
| 1245 | if (!ctx) |
| 1246 | return SSL_TLSEXT_ERR_NOACK; |
| 1247 | |
| 1248 | ocsp_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
| 1249 | if (!ocsp_arg) |
| 1250 | return SSL_TLSEXT_ERR_NOACK; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1251 | |
| 1252 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1253 | if (!ssl_pkey) |
| 1254 | return SSL_TLSEXT_ERR_NOACK; |
| 1255 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1256 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1257 | |
| 1258 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1259 | ocsp = ocsp_arg->s_ocsp; |
| 1260 | else { |
| 1261 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1262 | * the certificate type |
| 1263 | */ |
| 1264 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1265 | |
| 1266 | if (index < 0) |
| 1267 | return SSL_TLSEXT_ERR_NOACK; |
| 1268 | |
| 1269 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1270 | |
| 1271 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1272 | |
| 1273 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1274 | !ocsp->response.area || |
| 1275 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1276 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1277 | return SSL_TLSEXT_ERR_NOACK; |
| 1278 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1279 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1280 | if (!ssl_buf) |
| 1281 | return SSL_TLSEXT_ERR_NOACK; |
| 1282 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1283 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1284 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1285 | |
| 1286 | return SSL_TLSEXT_ERR_OK; |
| 1287 | } |
| 1288 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1289 | #endif |
| 1290 | |
Ilya Shipitsin | b3201a3 | 2020-10-18 09:11:50 +0500 | [diff] [blame] | 1291 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1292 | |
| 1293 | |
| 1294 | /* |
| 1295 | * Decrease the refcount of the struct ocsp_response and frees it if it's not |
| 1296 | * used anymore. Also removes it from the tree if free'd. |
| 1297 | */ |
| 1298 | static void ssl_sock_free_ocsp(struct certificate_ocsp *ocsp) |
| 1299 | { |
| 1300 | if (!ocsp) |
| 1301 | return; |
| 1302 | |
| 1303 | ocsp->refcount--; |
| 1304 | if (ocsp->refcount <= 0) { |
| 1305 | ebmb_delete(&ocsp->key); |
| 1306 | chunk_destroy(&ocsp->response); |
| 1307 | free(ocsp); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1312 | /* |
| 1313 | * 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] | 1314 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1315 | * status extension, the issuer's certificate is mandatory. It should be |
| 1316 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1317 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1318 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1319 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1320 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1321 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1322 | * |
| 1323 | * 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] | 1324 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1325 | */ |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1326 | 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] | 1327 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1328 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1329 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1330 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1331 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1332 | char *warn = NULL; |
| 1333 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1334 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1335 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1336 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1337 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1338 | if (!x) |
| 1339 | goto out; |
| 1340 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1341 | issuer = ckch->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1342 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1343 | if (chain) { |
| 1344 | /* check if one of the certificate of the chain is the issuer */ |
| 1345 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1346 | X509 *ti = sk_X509_value(chain, i); |
| 1347 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1348 | issuer = ti; |
| 1349 | break; |
| 1350 | } |
| 1351 | } |
| 1352 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1353 | if (!issuer) |
| 1354 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1355 | |
| 1356 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1357 | if (!cid) |
| 1358 | goto out; |
| 1359 | |
| 1360 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1361 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1362 | goto out; |
| 1363 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1364 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1365 | if (!ocsp) |
| 1366 | goto out; |
| 1367 | |
| 1368 | p = ocsp->key_data; |
| 1369 | i2d_OCSP_CERTID(cid, &p); |
| 1370 | |
| 1371 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1372 | if (iocsp == ocsp) |
| 1373 | ocsp = NULL; |
| 1374 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1375 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1376 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1377 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1378 | #endif |
| 1379 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1380 | |
| 1381 | if (!callback) { |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1382 | struct ocsp_cbk_arg *cb_arg; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1383 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1384 | |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1385 | cb_arg = calloc(1, sizeof(*cb_arg)); |
| 1386 | if (!cb_arg) |
| 1387 | goto out; |
| 1388 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1389 | cb_arg->is_single = 1; |
| 1390 | cb_arg->s_ocsp = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1391 | iocsp->refcount++; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1392 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1393 | pkey = X509_get_pubkey(x); |
| 1394 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1395 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1396 | |
| 1397 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1398 | SSL_CTX_set_ex_data(ctx, ocsp_ex_index, cb_arg); /* we use the ex_data instead of the cb_arg function here, so we can use the cleanup callback to free */ |
| 1399 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1400 | } else { |
| 1401 | /* |
| 1402 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1403 | * Update that cb_arg with the new cert's staple |
| 1404 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1405 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1406 | struct certificate_ocsp *tmp_ocsp; |
| 1407 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1408 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1409 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1410 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1411 | cb_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1412 | |
| 1413 | /* |
| 1414 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1415 | * the order of operations below matter, take care when changing it |
| 1416 | */ |
| 1417 | tmp_ocsp = cb_arg->s_ocsp; |
| 1418 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1419 | cb_arg->s_ocsp = NULL; |
| 1420 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1421 | cb_arg->is_single = 0; |
| 1422 | cb_arg->single_kt = 0; |
| 1423 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1424 | pkey = X509_get_pubkey(x); |
| 1425 | key_type = EVP_PKEY_base_id(pkey); |
| 1426 | EVP_PKEY_free(pkey); |
| 1427 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1428 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1429 | if (index >= 0 && !cb_arg->m_ocsp[index]) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1430 | cb_arg->m_ocsp[index] = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1431 | iocsp->refcount++; |
| 1432 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1433 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1434 | |
| 1435 | ret = 0; |
| 1436 | |
| 1437 | warn = NULL; |
William Lallemand | 86e4d63 | 2020-08-07 00:44:32 +0200 | [diff] [blame] | 1438 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, iocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1439 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1440 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1441 | } |
| 1442 | |
| 1443 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1444 | if (cid) |
| 1445 | OCSP_CERTID_free(cid); |
| 1446 | |
| 1447 | if (ocsp) |
| 1448 | free(ocsp); |
| 1449 | |
| 1450 | if (warn) |
| 1451 | free(warn); |
| 1452 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1453 | return ret; |
| 1454 | } |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 1455 | #endif |
| 1456 | |
| 1457 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1458 | 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] | 1459 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1460 | 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] | 1461 | } |
| 1462 | #endif |
| 1463 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1464 | |
Ilya Shipitsin | e4f38f9 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 1465 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1466 | |
| 1467 | #define CT_EXTENSION_TYPE 18 |
| 1468 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1469 | int sctl_ex_index = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1470 | |
| 1471 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1472 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1473 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1474 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1475 | *out = (unsigned char *) sctl->area; |
| 1476 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1477 | |
| 1478 | return 1; |
| 1479 | } |
| 1480 | |
| 1481 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1482 | { |
| 1483 | return 1; |
| 1484 | } |
| 1485 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1486 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1487 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1488 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1489 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1490 | 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] | 1491 | goto out; |
| 1492 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1493 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1494 | |
| 1495 | ret = 0; |
| 1496 | |
| 1497 | out: |
| 1498 | return ret; |
| 1499 | } |
| 1500 | |
| 1501 | #endif |
| 1502 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1503 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1504 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1505 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1506 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1507 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1508 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1509 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1510 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1511 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1512 | * change this to #if and do not assign a default value to this macro! |
| 1513 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1514 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1515 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1516 | 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] | 1517 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1518 | conn->err_code = CO_ER_SSL_RENEG; |
| 1519 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1520 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1521 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1522 | |
| 1523 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1524 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1525 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1526 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1527 | consider that the buffering was activated, |
| 1528 | so we rise the output buffer size from 4k |
| 1529 | to 16k */ |
| 1530 | write_bio = SSL_get_wbio(ssl); |
| 1531 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1532 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1533 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1537 | } |
| 1538 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1539 | /* Callback is called for each certificate of the chain during a verify |
| 1540 | ok is set to 1 if preverify detect no error on current certificate. |
| 1541 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1542 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1543 | { |
| 1544 | SSL *ssl; |
| 1545 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1546 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1547 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1548 | |
| 1549 | 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] | 1550 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1551 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1552 | ctx = conn->xprt_ctx; |
| 1553 | |
| 1554 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1555 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1556 | if (ok) /* no errors */ |
| 1557 | return ok; |
| 1558 | |
| 1559 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1560 | err = X509_STORE_CTX_get_error(x_store); |
| 1561 | |
| 1562 | /* check if CA error needs to be ignored */ |
| 1563 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1564 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1565 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1566 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1567 | } |
| 1568 | |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1569 | 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] | 1570 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1571 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1572 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1573 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1574 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1575 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1576 | return 0; |
| 1577 | } |
| 1578 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1579 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1580 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1581 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1582 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1583 | 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] | 1584 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1585 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1586 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1587 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1588 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1589 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1590 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1591 | } |
| 1592 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1593 | #ifdef TLS1_RT_HEARTBEAT |
| 1594 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 1595 | int content_type, const void *buf, size_t len, |
| 1596 | SSL *ssl) |
| 1597 | { |
| 1598 | /* test heartbeat received (write_p is set to 0 |
| 1599 | for a received record) */ |
| 1600 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
| 1601 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 1602 | const unsigned char *p = buf; |
| 1603 | unsigned int payload; |
| 1604 | |
| 1605 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
| 1606 | |
| 1607 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1608 | if (*p != TLS1_HB_REQUEST) |
| 1609 | return; |
| 1610 | |
| 1611 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
| 1612 | goto kill_it; |
| 1613 | |
| 1614 | payload = (p[1] * 256) + p[2]; |
| 1615 | if (3 + payload + 16 <= len) |
| 1616 | return; /* OK no problem */ |
| 1617 | kill_it: |
| 1618 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1619 | * advertised payload is larger than the advertised packet |
| 1620 | * length, so we have garbage in the buffer between the |
| 1621 | * payload and the end of the buffer (p+len). We can't know |
| 1622 | * if the SSL stack is patched, and we don't know if we can |
| 1623 | * safely wipe out the area between p+3+len and payload. |
| 1624 | * So instead, we prevent the response from being sent by |
| 1625 | * setting the max_send_fragment to 0 and we report an SSL |
| 1626 | * error, which will kill this connection. It will be reported |
| 1627 | * above as SSL_ERROR_SSL while an other handshake failure with |
| 1628 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1629 | */ |
| 1630 | ssl->max_send_fragment = 0; |
| 1631 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1632 | } |
| 1633 | } |
| 1634 | #endif |
| 1635 | |
| 1636 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 1637 | int content_type, const void *buf, size_t len, |
| 1638 | SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1639 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1640 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1641 | unsigned char *msg; |
| 1642 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1643 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1644 | |
| 1645 | /* This function is called for "from client" and "to server" |
| 1646 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1647 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1648 | */ |
| 1649 | |
| 1650 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1651 | * otherwise it is set to 1. |
| 1652 | */ |
| 1653 | if (write_p != 0) |
| 1654 | return; |
| 1655 | |
| 1656 | /* content_type contains the type of message received or sent |
| 1657 | * according with the SSL/TLS protocol spec. This message is |
| 1658 | * encoded with one byte. The value 256 (two bytes) is used |
| 1659 | * for designing the SSL/TLS record layer. According with the |
| 1660 | * rfc6101, the expected message (other than 256) are: |
| 1661 | * - change_cipher_spec(20) |
| 1662 | * - alert(21) |
| 1663 | * - handshake(22) |
| 1664 | * - application_data(23) |
| 1665 | * - (255) |
| 1666 | * We are interessed by the handshake and specially the client |
| 1667 | * hello. |
| 1668 | */ |
| 1669 | if (content_type != 22) |
| 1670 | return; |
| 1671 | |
| 1672 | /* The message length is at least 4 bytes, containing the |
| 1673 | * message type and the message length. |
| 1674 | */ |
| 1675 | if (len < 4) |
| 1676 | return; |
| 1677 | |
| 1678 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1679 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1680 | * - hello_request(0) |
| 1681 | * - client_hello(1) |
| 1682 | * - server_hello(2) |
| 1683 | * - certificate(11) |
| 1684 | * - server_key_exchange (12) |
| 1685 | * - certificate_request(13) |
| 1686 | * - server_hello_done(14) |
| 1687 | * We are interested by the client hello. |
| 1688 | */ |
| 1689 | msg = (unsigned char *)buf; |
| 1690 | if (msg[0] != 1) |
| 1691 | return; |
| 1692 | |
| 1693 | /* Next three bytes are the length of the message. The total length |
| 1694 | * must be this decoded length + 4. If the length given as argument |
| 1695 | * is not the same, we abort the protocol dissector. |
| 1696 | */ |
| 1697 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1698 | if (len < rec_len + 4) |
| 1699 | return; |
| 1700 | msg += 4; |
| 1701 | end = msg + rec_len; |
| 1702 | if (end < msg) |
| 1703 | return; |
| 1704 | |
| 1705 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1706 | * 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] | 1707 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1708 | */ |
| 1709 | msg += 1 + 1 + 4 + 28; |
| 1710 | if (msg > end) |
| 1711 | return; |
| 1712 | |
| 1713 | /* Next, is session id: |
| 1714 | * if present, we have to jump by length + 1 for the size information |
| 1715 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1716 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1717 | if (msg[0] > 0) |
| 1718 | msg += msg[0]; |
| 1719 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1720 | if (msg > end) |
| 1721 | return; |
| 1722 | |
| 1723 | /* Next two bytes are the ciphersuite length. */ |
| 1724 | if (msg + 2 > end) |
| 1725 | return; |
| 1726 | rec_len = (msg[0] << 8) + msg[1]; |
| 1727 | msg += 2; |
| 1728 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1729 | return; |
| 1730 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1731 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1732 | if (!capture) |
| 1733 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1734 | /* Compute the xxh64 of the ciphersuite. */ |
| 1735 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1736 | |
| 1737 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1738 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1739 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1740 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1741 | |
| 1742 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1743 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1744 | |
| 1745 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 1746 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1747 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 1748 | int content_type, const void *buf, size_t len, |
| 1749 | SSL *ssl) |
| 1750 | { |
| 1751 | struct ssl_keylog *keylog; |
| 1752 | |
| 1753 | if (SSL_get_ex_data(ssl, ssl_keylog_index)) |
| 1754 | return; |
| 1755 | |
| 1756 | keylog = pool_alloc(pool_head_ssl_keylog); |
| 1757 | if (!keylog) |
| 1758 | return; |
| 1759 | |
| 1760 | memset(keylog, 0, sizeof(*keylog)); |
| 1761 | |
| 1762 | if (!SSL_set_ex_data(ssl, ssl_keylog_index, keylog)) { |
| 1763 | pool_free(pool_head_ssl_keylog, keylog); |
| 1764 | return; |
| 1765 | } |
| 1766 | } |
| 1767 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1768 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1769 | /* Callback is called for ssl protocol analyse */ |
| 1770 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1771 | { |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1772 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1773 | struct ssl_sock_msg_callback *cbk; |
| 1774 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1775 | /* Try to call all callback functions that were registered by using |
| 1776 | * ssl_sock_register_msg_callback(). |
| 1777 | */ |
| 1778 | list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) { |
| 1779 | cbk->func(conn, write_p, version, content_type, buf, len, ssl); |
| 1780 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1781 | } |
| 1782 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1783 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1784 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1785 | const unsigned char *in, unsigned int inlen, |
| 1786 | void *arg) |
| 1787 | { |
| 1788 | struct server *srv = arg; |
| 1789 | |
| 1790 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1791 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1792 | return SSL_TLSEXT_ERR_OK; |
| 1793 | return SSL_TLSEXT_ERR_NOACK; |
| 1794 | } |
| 1795 | #endif |
| 1796 | |
| 1797 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1798 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1799 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1800 | */ |
| 1801 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1802 | unsigned int *len, void *arg) |
| 1803 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1804 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1805 | |
| 1806 | *data = (const unsigned char *)conf->npn_str; |
| 1807 | *len = conf->npn_len; |
| 1808 | return SSL_TLSEXT_ERR_OK; |
| 1809 | } |
| 1810 | #endif |
| 1811 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1812 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1813 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1814 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1815 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1816 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1817 | unsigned char *outlen, |
| 1818 | const unsigned char *server, |
| 1819 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1820 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1821 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1822 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1823 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1824 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1825 | return SSL_TLSEXT_ERR_NOACK; |
| 1826 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1827 | return SSL_TLSEXT_ERR_OK; |
| 1828 | } |
| 1829 | #endif |
| 1830 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1831 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1832 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1833 | |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1834 | /* Configure a DNS SAN extenion on a certificate. */ |
| 1835 | int ssl_sock_add_san_ext(X509V3_CTX* ctx, X509* cert, const char *servername) { |
| 1836 | int failure = 0; |
| 1837 | X509_EXTENSION *san_ext = NULL; |
| 1838 | CONF *conf = NULL; |
| 1839 | struct buffer *san_name = get_trash_chunk(); |
| 1840 | |
| 1841 | conf = NCONF_new(NULL); |
| 1842 | if (!conf) { |
| 1843 | failure = 1; |
| 1844 | goto cleanup; |
| 1845 | } |
| 1846 | |
| 1847 | /* Build an extension based on the DNS entry above */ |
| 1848 | chunk_appendf(san_name, "DNS:%s", servername); |
| 1849 | san_ext = X509V3_EXT_nconf_nid(conf, ctx, NID_subject_alt_name, san_name->area); |
| 1850 | if (!san_ext) { |
| 1851 | failure = 1; |
| 1852 | goto cleanup; |
| 1853 | } |
| 1854 | |
| 1855 | /* Add the extension */ |
| 1856 | if (!X509_add_ext(cert, san_ext, -1 /* Add to end */)) { |
| 1857 | failure = 1; |
| 1858 | goto cleanup; |
| 1859 | } |
| 1860 | |
| 1861 | /* Success */ |
| 1862 | failure = 0; |
| 1863 | |
| 1864 | cleanup: |
| 1865 | if (NULL != san_ext) X509_EXTENSION_free(san_ext); |
| 1866 | if (NULL != conf) NCONF_free(conf); |
| 1867 | |
| 1868 | return failure; |
| 1869 | } |
| 1870 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1871 | /* Create a X509 certificate with the specified servername and serial. This |
| 1872 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1873 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1874 | 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] | 1875 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 1876 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
| 1877 | EVP_PKEY *capkey = bind_conf->ca_sign_ckch->key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1878 | SSL_CTX *ssl_ctx = NULL; |
| 1879 | X509 *newcrt = NULL; |
| 1880 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1881 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1882 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1883 | X509_NAME *name; |
| 1884 | const EVP_MD *digest; |
| 1885 | X509V3_CTX ctx; |
| 1886 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1887 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1888 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1889 | /* Get the private key of the default certificate and use it */ |
Ilya Shipitsin | 842976d | 2020-12-19 03:12:12 +0500 | [diff] [blame] | 1890 | #ifdef HAVE_SSL_CTX_get0_privatekey |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1891 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1892 | #else |
| 1893 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1894 | if (tmp_ssl) |
| 1895 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1896 | #endif |
| 1897 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1898 | goto mkcert_error; |
| 1899 | |
| 1900 | /* Create the certificate */ |
| 1901 | if (!(newcrt = X509_new())) |
| 1902 | goto mkcert_error; |
| 1903 | |
| 1904 | /* Set version number for the certificate (X509v3) and the serial |
| 1905 | * number */ |
| 1906 | if (X509_set_version(newcrt, 2L) != 1) |
| 1907 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 1908 | 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] | 1909 | |
| 1910 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1911 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1912 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1913 | goto mkcert_error; |
| 1914 | |
| 1915 | /* set public key in the certificate */ |
| 1916 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1917 | goto mkcert_error; |
| 1918 | |
| 1919 | /* Set issuer name from the CA */ |
| 1920 | if (!(name = X509_get_subject_name(cacert))) |
| 1921 | goto mkcert_error; |
| 1922 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1923 | goto mkcert_error; |
| 1924 | |
| 1925 | /* Set the subject name using the same, but the CN */ |
| 1926 | name = X509_NAME_dup(name); |
| 1927 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1928 | (const unsigned char *)servername, |
| 1929 | -1, -1, 0) != 1) { |
| 1930 | X509_NAME_free(name); |
| 1931 | goto mkcert_error; |
| 1932 | } |
| 1933 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1934 | X509_NAME_free(name); |
| 1935 | goto mkcert_error; |
| 1936 | } |
| 1937 | X509_NAME_free(name); |
| 1938 | |
| 1939 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1940 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1941 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1942 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1943 | X509_EXTENSION *ext; |
| 1944 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1945 | 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] | 1946 | goto mkcert_error; |
| 1947 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1948 | X509_EXTENSION_free(ext); |
| 1949 | goto mkcert_error; |
| 1950 | } |
| 1951 | X509_EXTENSION_free(ext); |
| 1952 | } |
| 1953 | |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1954 | /* Add SAN extension */ |
| 1955 | if (ssl_sock_add_san_ext(&ctx, newcrt, servername)) { |
| 1956 | goto mkcert_error; |
| 1957 | } |
| 1958 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1959 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1960 | |
| 1961 | key_type = EVP_PKEY_base_id(capkey); |
| 1962 | |
| 1963 | if (key_type == EVP_PKEY_DSA) |
| 1964 | digest = EVP_sha1(); |
| 1965 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1966 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1967 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1968 | digest = EVP_sha256(); |
| 1969 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 1970 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1971 | int nid; |
| 1972 | |
| 1973 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1974 | goto mkcert_error; |
| 1975 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1976 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1977 | #else |
| 1978 | goto mkcert_error; |
| 1979 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1980 | } |
| 1981 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1982 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1983 | goto mkcert_error; |
| 1984 | |
| 1985 | /* Create and set the new SSL_CTX */ |
| 1986 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1987 | goto mkcert_error; |
| 1988 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1989 | goto mkcert_error; |
| 1990 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1991 | goto mkcert_error; |
| 1992 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1993 | goto mkcert_error; |
| 1994 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 1995 | /* Build chaining the CA cert and the rest of the chain, keep these order */ |
| 1996 | #if defined(SSL_CTX_add1_chain_cert) |
| 1997 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, bind_conf->ca_sign_ckch->cert)) { |
| 1998 | goto mkcert_error; |
| 1999 | } |
| 2000 | |
| 2001 | if (bind_conf->ca_sign_ckch->chain) { |
| 2002 | for (i = 0; i < sk_X509_num(bind_conf->ca_sign_ckch->chain); i++) { |
| 2003 | X509 *chain_cert = sk_X509_value(bind_conf->ca_sign_ckch->chain, i); |
| 2004 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, chain_cert)) { |
| 2005 | goto mkcert_error; |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | #endif |
| 2010 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2011 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2012 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2013 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2014 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2015 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2016 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2017 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2018 | 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] | 2019 | EC_KEY *ecc; |
| 2020 | int nid; |
| 2021 | |
| 2022 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2023 | goto end; |
| 2024 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2025 | goto end; |
| 2026 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2027 | EC_KEY_free(ecc); |
| 2028 | } |
| 2029 | #endif |
| 2030 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2031 | return ssl_ctx; |
| 2032 | |
| 2033 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2034 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2035 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2036 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2037 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2038 | return NULL; |
| 2039 | } |
| 2040 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2041 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2042 | 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] | 2043 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 2044 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2045 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2046 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2047 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2048 | } |
| 2049 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2050 | /* 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] | 2051 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2052 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2053 | 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] | 2054 | { |
| 2055 | struct lru64 *lru = NULL; |
| 2056 | |
| 2057 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2058 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2059 | lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_ckch->cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2060 | if (lru && lru->domain) { |
| 2061 | if (ssl) |
| 2062 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2063 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2064 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2065 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2066 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2067 | } |
| 2068 | return NULL; |
| 2069 | } |
| 2070 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2071 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2072 | * function is not thread-safe, it should only be used to check if a certificate |
| 2073 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2074 | * thread). It is kept for backward compatibility. */ |
| 2075 | SSL_CTX * |
| 2076 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2077 | { |
| 2078 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2079 | } |
| 2080 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2081 | /* Set a certificate int the LRU cache used to store generated |
| 2082 | * certificate. Return 0 on success, otherwise -1 */ |
| 2083 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2084 | 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] | 2085 | { |
| 2086 | struct lru64 *lru = NULL; |
| 2087 | |
| 2088 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2089 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2090 | lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_ckch->cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2091 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2092 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2093 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2094 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2095 | if (lru->domain && lru->data) |
| 2096 | lru->free((SSL_CTX *)lru->data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2097 | lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_ckch->cert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2098 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2099 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2100 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2101 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2102 | } |
| 2103 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2104 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2105 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2106 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2107 | { |
| 2108 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2109 | } |
| 2110 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2111 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2112 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2113 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2114 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2115 | 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] | 2116 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2117 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2118 | SSL_CTX *ssl_ctx = NULL; |
| 2119 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2120 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2121 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2122 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2123 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2124 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2125 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2126 | if (lru && lru->domain) |
| 2127 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2128 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2129 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2130 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2131 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2132 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2133 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2134 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2135 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2136 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2137 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2138 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2139 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2140 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2141 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2142 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2143 | return 0; |
| 2144 | } |
| 2145 | static int |
| 2146 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2147 | { |
| 2148 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2149 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2150 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2151 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2152 | 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] | 2153 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2154 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2155 | } |
| 2156 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2157 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2158 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2159 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2160 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2161 | |
| 2162 | 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] | 2163 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2164 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2165 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2166 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2167 | #endif |
| 2168 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2169 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2170 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2171 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2172 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2173 | 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] | 2174 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2175 | 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] | 2176 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2177 | #endif |
| 2178 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2179 | 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] | 2180 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2181 | 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] | 2182 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2183 | #endif |
| 2184 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2185 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2186 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2187 | /* Unusable in this context. */ |
| 2188 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2189 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2190 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2191 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2192 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2193 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2194 | |
| 2195 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2196 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2197 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2198 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2199 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2200 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2201 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2202 | } |
| 2203 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2204 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2205 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2206 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2207 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2208 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2209 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2210 | } |
| 2211 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2212 | 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] | 2213 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2214 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2215 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2216 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2217 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2218 | } |
| 2219 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2220 | 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] | 2221 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2222 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2223 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2224 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2225 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2226 | } |
| 2227 | 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] | 2228 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2229 | 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] | 2230 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2231 | #endif |
| 2232 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2233 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2234 | #if SSL_OP_NO_TLSv1_3 |
| 2235 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2236 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2237 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2238 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2239 | #endif |
| 2240 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2241 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2242 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 2243 | struct methodVersions methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2244 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2245 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2246 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2247 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2248 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2249 | {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] | 2250 | }; |
| 2251 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2252 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2253 | { |
| 2254 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2255 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2256 | SSL_set_SSL_CTX(ssl, ctx); |
| 2257 | } |
| 2258 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2259 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2260 | |
| 2261 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2262 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2263 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2264 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2265 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2266 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2267 | return SSL_TLSEXT_ERR_OK; |
| 2268 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2269 | } |
| 2270 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2271 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2272 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2273 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2274 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2275 | #else |
| 2276 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2277 | { |
| 2278 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2279 | struct connection *conn; |
| 2280 | struct bind_conf *s; |
| 2281 | const uint8_t *extension_data; |
| 2282 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2283 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2284 | |
| 2285 | char *wildp = NULL; |
| 2286 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2287 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2288 | 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] | 2289 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2290 | int i; |
| 2291 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2292 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2293 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2294 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2295 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2296 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2297 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2298 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2299 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2300 | #else |
| 2301 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2302 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2303 | /* |
| 2304 | * The server_name extension was given too much extensibility when it |
| 2305 | * was written, so parsing the normal case is a bit complex. |
| 2306 | */ |
| 2307 | size_t len; |
| 2308 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2309 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2310 | /* Extract the length of the supplied list of names. */ |
| 2311 | len = (*extension_data++) << 8; |
| 2312 | len |= *extension_data++; |
| 2313 | if (len + 2 != extension_len) |
| 2314 | goto abort; |
| 2315 | /* |
| 2316 | * The list in practice only has a single element, so we only consider |
| 2317 | * the first one. |
| 2318 | */ |
| 2319 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2320 | goto abort; |
| 2321 | extension_len = len - 1; |
| 2322 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2323 | if (extension_len <= 2) |
| 2324 | goto abort; |
| 2325 | len = (*extension_data++) << 8; |
| 2326 | len |= *extension_data++; |
| 2327 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2328 | || memchr(extension_data, 0, len) != NULL) |
| 2329 | goto abort; |
| 2330 | servername = extension_data; |
| 2331 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2332 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2333 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2334 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2335 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2336 | } |
| 2337 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2338 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2339 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2340 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2341 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2342 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2343 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2344 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2345 | goto abort; |
| 2346 | } |
| 2347 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2348 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2349 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2350 | 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] | 2351 | #else |
| 2352 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2353 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2354 | uint8_t sign; |
| 2355 | size_t len; |
| 2356 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2357 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2358 | len = (*extension_data++) << 8; |
| 2359 | len |= *extension_data++; |
| 2360 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2361 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2362 | if (len % 2 != 0) |
| 2363 | goto abort; |
| 2364 | for (; len > 0; len -= 2) { |
| 2365 | extension_data++; /* hash */ |
| 2366 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2367 | switch (sign) { |
| 2368 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2369 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2370 | break; |
| 2371 | case TLSEXT_signature_ecdsa: |
| 2372 | has_ecdsa_sig = 1; |
| 2373 | break; |
| 2374 | default: |
| 2375 | continue; |
| 2376 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2377 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2378 | break; |
| 2379 | } |
| 2380 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2381 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2382 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2383 | } |
| 2384 | 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] | 2385 | const SSL_CIPHER *cipher; |
| 2386 | size_t len; |
| 2387 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2388 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2389 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2390 | len = ctx->cipher_suites_len; |
| 2391 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2392 | #else |
| 2393 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2394 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2395 | if (len % 2 != 0) |
| 2396 | goto abort; |
| 2397 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2398 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2399 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2400 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2401 | #else |
| 2402 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2403 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2404 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2405 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2406 | break; |
| 2407 | } |
| 2408 | } |
| 2409 | } |
| 2410 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2411 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2412 | trash.area[i] = tolower(servername[i]); |
| 2413 | if (!wildp && (trash.area[i] == '.')) |
| 2414 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2415 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2416 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2417 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2418 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2419 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2420 | /* Look for an ECDSA, RSA and DSA certificate, first in the single |
| 2421 | * name and if not found in the wildcard */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2422 | for (i = 0; i < 2; i++) { |
| 2423 | if (i == 0) /* lookup in full qualified names */ |
| 2424 | node = ebst_lookup(&s->sni_ctx, trash.area); |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2425 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2426 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2427 | else |
| 2428 | break; |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2429 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2430 | for (n = node; n; n = ebmb_next_dup(n)) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2431 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2432 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2433 | if (!container_of(n, struct sni_ctx, name)->neg) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2434 | struct sni_ctx *sni, *sni_tmp; |
| 2435 | int skip = 0; |
| 2436 | |
| 2437 | if (i == 1 && wildp) { /* wildcard */ |
| 2438 | /* If this is a wildcard, look for an exclusion on the same crt-list line */ |
| 2439 | sni = container_of(n, struct sni_ctx, name); |
| 2440 | list_for_each_entry(sni_tmp, &sni->ckch_inst->sni_ctx, by_ckch_inst) { |
| 2441 | if (sni_tmp->neg && (!strcmp((const char *)sni_tmp->name.key, trash.area))) { |
| 2442 | skip = 1; |
| 2443 | break; |
| 2444 | } |
| 2445 | } |
| 2446 | if (skip) |
| 2447 | continue; |
| 2448 | } |
| 2449 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2450 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2451 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2452 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2453 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2454 | break; |
| 2455 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2456 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2457 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2458 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2459 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2460 | if (!node_anonymous) |
| 2461 | node_anonymous = n; |
| 2462 | break; |
| 2463 | } |
| 2464 | } |
| 2465 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2466 | } |
| 2467 | /* Once the certificates are found, select them depending on what is |
| 2468 | * supported in the client and by key_signature priority order: EDSA > |
| 2469 | * RSA > DSA */ |
William Lallemand | 5b1d1f6 | 2020-08-14 15:30:13 +0200 | [diff] [blame] | 2470 | if (has_ecdsa_sig && node_ecdsa) |
| 2471 | node = node_ecdsa; |
| 2472 | else if (has_rsa_sig && node_rsa) |
| 2473 | node = node_rsa; |
| 2474 | else if (node_anonymous) |
| 2475 | node = node_anonymous; |
| 2476 | else if (node_ecdsa) |
| 2477 | node = node_ecdsa; /* no ecdsa signature case (< TLSv1.2) */ |
| 2478 | else |
| 2479 | node = node_rsa; /* no rsa signature case (far far away) */ |
| 2480 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2481 | if (node) { |
| 2482 | /* switch ctx */ |
| 2483 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2484 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 2485 | if (conf) { |
| 2486 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2487 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2488 | if (conf->early_data) |
| 2489 | allow_early = 1; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2490 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2491 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
| 2492 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2493 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2494 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2495 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2496 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2497 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2498 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2499 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2500 | } |
| 2501 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2502 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2503 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2504 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2505 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2506 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2507 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2508 | allow_early: |
| 2509 | #ifdef OPENSSL_IS_BORINGSSL |
| 2510 | if (allow_early) |
| 2511 | SSL_set_early_data_enabled(ssl, 1); |
| 2512 | #else |
| 2513 | if (!allow_early) |
| 2514 | SSL_set_max_early_data(ssl, 0); |
| 2515 | #endif |
| 2516 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2517 | abort: |
| 2518 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2519 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2520 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2521 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2522 | #else |
| 2523 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2524 | return 0; |
| 2525 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2529 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2530 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2531 | * warning when no match is found, which implies the default (first) cert |
| 2532 | * will keep being used. |
| 2533 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2534 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2535 | { |
| 2536 | const char *servername; |
| 2537 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2538 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2539 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2540 | int i; |
| 2541 | (void)al; /* shut gcc stupid warning */ |
| 2542 | |
| 2543 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2544 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2545 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2546 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2547 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2548 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2549 | if (s->strict_sni) |
| 2550 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2551 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2552 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2553 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2554 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2555 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2556 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2557 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2558 | if (!servername[i]) |
| 2559 | break; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 2560 | trash.area[i] = tolower((unsigned char)servername[i]); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2561 | if (!wildp && (trash.area[i] == '.')) |
| 2562 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2563 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2564 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2565 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2566 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2567 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2568 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2569 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2570 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2571 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2572 | node = n; |
| 2573 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2574 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2575 | } |
| 2576 | if (!node && wildp) { |
| 2577 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2578 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2579 | /* lookup a not neg filter */ |
| 2580 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2581 | node = n; |
| 2582 | break; |
| 2583 | } |
| 2584 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2585 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2586 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2587 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2588 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2589 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2590 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2591 | return SSL_TLSEXT_ERR_OK; |
| 2592 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2593 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2594 | if (s->strict_sni) { |
| 2595 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2596 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2597 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2598 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2599 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2600 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2601 | } |
| 2602 | |
| 2603 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2604 | 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] | 2605 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2606 | return SSL_TLSEXT_ERR_OK; |
| 2607 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2608 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2609 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2610 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2611 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2612 | |
| 2613 | static DH * ssl_get_dh_1024(void) |
| 2614 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2615 | static unsigned char dh1024_p[]={ |
| 2616 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2617 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2618 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2619 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2620 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2621 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2622 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2623 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2624 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2625 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2626 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2627 | }; |
| 2628 | static unsigned char dh1024_g[]={ |
| 2629 | 0x02, |
| 2630 | }; |
| 2631 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2632 | BIGNUM *p; |
| 2633 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2634 | DH *dh = DH_new(); |
| 2635 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2636 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2637 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2638 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2639 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2640 | DH_free(dh); |
| 2641 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2642 | } else { |
| 2643 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2644 | } |
| 2645 | } |
| 2646 | return dh; |
| 2647 | } |
| 2648 | |
| 2649 | static DH *ssl_get_dh_2048(void) |
| 2650 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2651 | static unsigned char dh2048_p[]={ |
| 2652 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2653 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2654 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2655 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2656 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2657 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2658 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2659 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2660 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2661 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2662 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2663 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2664 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2665 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2666 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2667 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2668 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2669 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2670 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2671 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2672 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2673 | 0xB7,0x1F,0x77,0xF3, |
| 2674 | }; |
| 2675 | static unsigned char dh2048_g[]={ |
| 2676 | 0x02, |
| 2677 | }; |
| 2678 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2679 | BIGNUM *p; |
| 2680 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2681 | DH *dh = DH_new(); |
| 2682 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2683 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2684 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2685 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2686 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2687 | DH_free(dh); |
| 2688 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2689 | } else { |
| 2690 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2691 | } |
| 2692 | } |
| 2693 | return dh; |
| 2694 | } |
| 2695 | |
| 2696 | static DH *ssl_get_dh_4096(void) |
| 2697 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2698 | static unsigned char dh4096_p[]={ |
| 2699 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2700 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2701 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2702 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2703 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2704 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2705 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2706 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2707 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2708 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2709 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2710 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2711 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2712 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2713 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2714 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2715 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2716 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2717 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2718 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2719 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2720 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2721 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2722 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2723 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2724 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2725 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2726 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2727 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2728 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2729 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2730 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2731 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2732 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2733 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2734 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2735 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2736 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2737 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2738 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2739 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2740 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2741 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2742 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2743 | static unsigned char dh4096_g[]={ |
| 2744 | 0x02, |
| 2745 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2746 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2747 | BIGNUM *p; |
| 2748 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2749 | DH *dh = DH_new(); |
| 2750 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2751 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2752 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2753 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2754 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2755 | DH_free(dh); |
| 2756 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2757 | } else { |
| 2758 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2759 | } |
| 2760 | } |
| 2761 | return dh; |
| 2762 | } |
| 2763 | |
| 2764 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2765 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2766 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2767 | { |
| 2768 | DH *dh = NULL; |
| 2769 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2770 | int type; |
| 2771 | |
| 2772 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2773 | |
| 2774 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2775 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2776 | */ |
| 2777 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2778 | keylen = EVP_PKEY_bits(pkey); |
| 2779 | } |
| 2780 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2781 | if (keylen > global_ssl.default_dh_param) { |
| 2782 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2783 | } |
| 2784 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2785 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2786 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2787 | } |
| 2788 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2789 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2790 | } |
| 2791 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2792 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2793 | } |
| 2794 | |
| 2795 | return dh; |
| 2796 | } |
| 2797 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2798 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2799 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2800 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2801 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2802 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2803 | if (in == NULL) |
| 2804 | goto end; |
| 2805 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2806 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2807 | goto end; |
| 2808 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2809 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2810 | |
| 2811 | end: |
| 2812 | if (in) |
| 2813 | BIO_free(in); |
| 2814 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2815 | ERR_clear_error(); |
| 2816 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2817 | return dh; |
| 2818 | } |
| 2819 | |
| 2820 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2821 | { |
| 2822 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2823 | |
| 2824 | if (global_dh) { |
| 2825 | return 0; |
| 2826 | } |
| 2827 | |
| 2828 | return -1; |
| 2829 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2830 | #endif |
| 2831 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2832 | /* 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] | 2833 | 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] | 2834 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2835 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2836 | { |
| 2837 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2838 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2839 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2840 | if (*name == '!') { |
| 2841 | neg = 1; |
| 2842 | name++; |
| 2843 | } |
| 2844 | if (*name == '*') { |
| 2845 | wild = 1; |
| 2846 | name++; |
| 2847 | } |
| 2848 | /* !* filter is a nop */ |
| 2849 | if (neg && wild) |
| 2850 | return order; |
| 2851 | if (*name) { |
| 2852 | int j, len; |
| 2853 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2854 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 2855 | trash.area[j] = tolower((unsigned char)name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2856 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2857 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2858 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2859 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2860 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2861 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2862 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2863 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2864 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2865 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2866 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2867 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2868 | sc->order = order++; |
| 2869 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2870 | sc->wild = wild; |
| 2871 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 2872 | sc->ckch_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2873 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2874 | } |
| 2875 | return order; |
| 2876 | } |
| 2877 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2878 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2879 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 2880 | * This function can't return an error. |
| 2881 | * |
| 2882 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 2883 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 2884 | 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] | 2885 | { |
| 2886 | |
| 2887 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 2888 | struct ebmb_node *node; |
| 2889 | |
| 2890 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 2891 | |
| 2892 | /* ignore if sc0 was already inserted in a tree */ |
| 2893 | if (sc0->name.node.leaf_p) |
| 2894 | continue; |
| 2895 | |
| 2896 | /* Check for duplicates. */ |
| 2897 | if (sc0->wild) |
| 2898 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 2899 | else |
| 2900 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 2901 | |
| 2902 | for (; node; node = ebmb_next_dup(node)) { |
| 2903 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 2904 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 2905 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 2906 | /* it's a duplicate, we should remove and free it */ |
| 2907 | LIST_DEL(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2908 | SSL_CTX_free(sc0->ctx); |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2909 | free(sc0); |
| 2910 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 2911 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2912 | } |
| 2913 | } |
| 2914 | |
| 2915 | /* if duplicate, ignore the insertion */ |
| 2916 | if (!sc0) |
| 2917 | continue; |
| 2918 | |
| 2919 | if (sc0->wild) |
| 2920 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 2921 | else |
| 2922 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
Remi Tricot-Le Breton | 358a822 | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 2923 | } |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2924 | |
Remi Tricot-Le Breton | 358a822 | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 2925 | /* replace the default_ctx if required with the instance's ctx. */ |
| 2926 | if (ckch_inst->is_default) { |
| 2927 | SSL_CTX_free(bind_conf->default_ctx); |
| 2928 | SSL_CTX_up_ref(ckch_inst->ctx); |
| 2929 | bind_conf->default_ctx = ckch_inst->ctx; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2930 | } |
| 2931 | } |
| 2932 | |
| 2933 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2934 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2935 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2936 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2937 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 2938 | /* tree of crtlist (crt-list/directory) */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 2939 | struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2940 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2941 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2942 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2943 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 2944 | * DH parameter available in ckchs nor in global, the default |
| 2945 | * DH parameters are applied on the SSL_CTX. |
| 2946 | * Returns a bitfield containing the flags: |
| 2947 | * ERR_FATAL in any fatal error case |
| 2948 | * ERR_ALERT if a reason of the error is availabine in err |
| 2949 | * ERR_WARN if a warning is available into err |
| 2950 | * The value 0 means there is no error nor warning and |
| 2951 | * the operation succeed. |
| 2952 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2953 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2954 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 2955 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2956 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2957 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2958 | DH *dh = NULL; |
| 2959 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 2960 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2961 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2962 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 2963 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 2964 | err && *err ? *err : "", path); |
| 2965 | #if defined(SSL_CTX_set_dh_auto) |
| 2966 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2967 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2968 | err && *err ? *err : ""); |
| 2969 | #else |
| 2970 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2971 | err && *err ? *err : ""); |
| 2972 | #endif |
| 2973 | ret |= ERR_WARN; |
| 2974 | goto end; |
| 2975 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2976 | |
| 2977 | if (ssl_dh_ptr_index >= 0) { |
| 2978 | /* store a pointer to the DH params to avoid complaining about |
| 2979 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2980 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2981 | } |
| 2982 | } |
| 2983 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2984 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 2985 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 2986 | err && *err ? *err : "", path); |
| 2987 | #if defined(SSL_CTX_set_dh_auto) |
| 2988 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2989 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2990 | err && *err ? *err : ""); |
| 2991 | #else |
| 2992 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2993 | err && *err ? *err : ""); |
| 2994 | #endif |
| 2995 | ret |= ERR_WARN; |
| 2996 | goto end; |
| 2997 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2998 | } |
| 2999 | else { |
| 3000 | /* Clear openssl global errors stack */ |
| 3001 | ERR_clear_error(); |
| 3002 | |
Willy Tarreau | 58ee231 | 2020-11-05 19:38:05 +0100 | [diff] [blame] | 3003 | if (global_ssl.default_dh_param && global_ssl.default_dh_param <= 1024) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3004 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3005 | if (local_dh_1024 == NULL) |
| 3006 | local_dh_1024 = ssl_get_dh_1024(); |
| 3007 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3008 | if (local_dh_1024 == NULL) { |
| 3009 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3010 | err && *err ? *err : "", path); |
| 3011 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3012 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3013 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3014 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3015 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 3016 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3017 | err && *err ? *err : "", path); |
| 3018 | #if defined(SSL_CTX_set_dh_auto) |
| 3019 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3020 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3021 | err && *err ? *err : ""); |
| 3022 | #else |
| 3023 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3024 | err && *err ? *err : ""); |
| 3025 | #endif |
| 3026 | ret |= ERR_WARN; |
| 3027 | goto end; |
| 3028 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3029 | } |
| 3030 | else { |
| 3031 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 3032 | } |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3033 | } |
| 3034 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3035 | end: |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3036 | ERR_clear_error(); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3037 | return ret; |
| 3038 | } |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3039 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3040 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3041 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3042 | * Returns a bitfield containing the flags: |
| 3043 | * ERR_FATAL in any fatal error case |
| 3044 | * ERR_ALERT if the reason of the error is available in err |
| 3045 | * ERR_WARN if a warning is available into err |
| 3046 | * The value 0 means there is no error nor warning and |
| 3047 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3048 | */ |
| 3049 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3050 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3051 | int errcode = 0; |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3052 | STACK_OF(X509) *find_chain = NULL; |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3053 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3054 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3055 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3056 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3057 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3058 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3059 | } |
| 3060 | |
| 3061 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3062 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3063 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3064 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3065 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3066 | } |
| 3067 | |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3068 | if (ckch->chain) { |
| 3069 | find_chain = ckch->chain; |
| 3070 | } else { |
| 3071 | /* Find Certificate Chain in global */ |
| 3072 | struct issuer_chain *issuer; |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 3073 | issuer = ssl_get0_issuer_chain(ckch->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3074 | if (issuer) |
| 3075 | find_chain = issuer->chain; |
| 3076 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3077 | |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3078 | if (!find_chain) { |
| 3079 | /* always put a null chain stack in the SSL_CTX so it does not |
| 3080 | * try to build the chain from the verify store */ |
| 3081 | find_chain = sk_X509_new_null(); |
| 3082 | } |
| 3083 | |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3084 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3085 | #ifdef SSL_CTX_set1_chain |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3086 | if (!SSL_CTX_set1_chain(ctx, find_chain)) { |
| 3087 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3088 | err && *err ? *err : "", path); |
| 3089 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3090 | goto end; |
| 3091 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3092 | #else |
| 3093 | { /* legacy compat (< openssl 1.0.2) */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3094 | X509 *ca; |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3095 | STACK_OF(X509) *chain; |
| 3096 | chain = X509_chain_up_ref(find_chain); |
| 3097 | while ((ca = sk_X509_shift(chain))) |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3098 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3099 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3100 | err && *err ? *err : "", path); |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3101 | X509_free(ca); |
| 3102 | sk_X509_pop_free(chain, X509_free); |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3103 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3104 | goto end; |
| 3105 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3106 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3107 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3108 | |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3109 | #ifdef SSL_CTX_build_cert_chain |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3110 | /* remove the Root CA from the SSL_CTX if the option is activated */ |
| 3111 | if (global_ssl.skip_self_issued_ca) { |
| 3112 | if (!SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT|SSL_BUILD_CHAIN_FLAG_UNTRUSTED|SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)) { |
| 3113 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3114 | err && *err ? *err : "", path); |
| 3115 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3116 | goto end; |
| 3117 | } |
| 3118 | } |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3119 | #endif |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3120 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3121 | #ifndef OPENSSL_NO_DH |
| 3122 | /* store a NULL pointer to indicate we have not yet loaded |
| 3123 | a custom DH param file */ |
| 3124 | if (ssl_dh_ptr_index >= 0) { |
| 3125 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3126 | } |
| 3127 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3128 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 3129 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3130 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3131 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3132 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3133 | } |
| 3134 | #endif |
| 3135 | |
Ilya Shipitsin | e4f38f9 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 3136 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3137 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 3138 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 3139 | 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] | 3140 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3141 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3142 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3143 | } |
| 3144 | } |
| 3145 | #endif |
| 3146 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 3147 | #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] | 3148 | /* Load OCSP Info into context */ |
| 3149 | if (ckch->ocsp_response) { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 3150 | if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3151 | 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", |
| 3152 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3153 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3154 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3155 | } |
| 3156 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3157 | #endif |
| 3158 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3159 | end: |
| 3160 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3161 | } |
| 3162 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3163 | /* |
| 3164 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3165 | * |
| 3166 | * Returns a bitfield containing the flags: |
| 3167 | * ERR_FATAL in any fatal error case |
| 3168 | * ERR_ALERT if the reason of the error is available in err |
| 3169 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3170 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3171 | 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] | 3172 | 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] | 3173 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3174 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3175 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3176 | int order = 0; |
| 3177 | X509_NAME *xname; |
| 3178 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3179 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3180 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3181 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3182 | STACK_OF(GENERAL_NAME) *names; |
| 3183 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3184 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3185 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3186 | int errcode = 0; |
| 3187 | |
| 3188 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3189 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3190 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3191 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3192 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3193 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3194 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3195 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3196 | if (!ctx) { |
| 3197 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3198 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3199 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3200 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3201 | } |
| 3202 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3203 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 3204 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3205 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3206 | |
| 3207 | ckch_inst = ckch_inst_new(); |
| 3208 | if (!ckch_inst) { |
| 3209 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3210 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3211 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3212 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3213 | } |
| 3214 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3215 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3216 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3217 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3218 | switch(EVP_PKEY_base_id(pkey)) { |
| 3219 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3220 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3221 | break; |
| 3222 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3223 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3224 | break; |
| 3225 | case EVP_PKEY_DSA: |
| 3226 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3227 | break; |
| 3228 | } |
| 3229 | EVP_PKEY_free(pkey); |
| 3230 | } |
| 3231 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3232 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3233 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3234 | 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] | 3235 | if (order < 0) { |
| 3236 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3237 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3238 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3239 | } |
| 3240 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3241 | } |
| 3242 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3243 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3244 | 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] | 3245 | if (names) { |
| 3246 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3247 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3248 | if (name->type == GEN_DNS) { |
| 3249 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3250 | 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] | 3251 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3252 | if (order < 0) { |
| 3253 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3254 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3255 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3256 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3257 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3258 | } |
| 3259 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3260 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3261 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3262 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3263 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3264 | i = -1; |
| 3265 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3266 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3267 | ASN1_STRING *value; |
| 3268 | |
| 3269 | value = X509_NAME_ENTRY_get_data(entry); |
| 3270 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3271 | 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] | 3272 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3273 | if (order < 0) { |
| 3274 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3275 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3276 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3277 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3278 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3279 | } |
| 3280 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3281 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3282 | * the tree, so it will be discovered and cleaned in time. |
| 3283 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3284 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3285 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3286 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3287 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3288 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3289 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3290 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3291 | } |
| 3292 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3293 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3294 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3295 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3296 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3297 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3298 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3299 | |
Remi Tricot-Le Breton | 358a822 | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3300 | /* Always keep a reference to the newly constructed SSL_CTX in the |
| 3301 | * instance. This way if the instance has no SNIs, the SSL_CTX will |
| 3302 | * still be linked. */ |
| 3303 | SSL_CTX_up_ref(ctx); |
| 3304 | ckch_inst->ctx = ctx; |
| 3305 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3306 | /* everything succeed, the ckch instance can be used */ |
| 3307 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3308 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3309 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3310 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3311 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3312 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3313 | *ckchi = ckch_inst; |
| 3314 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3315 | |
| 3316 | error: |
| 3317 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3318 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3319 | if (ckch_inst->is_default) |
| 3320 | SSL_CTX_free(ctx); |
| 3321 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3322 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3323 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3324 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3325 | SSL_CTX_free(ctx); |
| 3326 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3327 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3328 | } |
| 3329 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 3330 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3331 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 3332 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3333 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3334 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3335 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3336 | |
| 3337 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | e7eb1fe | 2020-09-16 16:17:51 +0200 | [diff] [blame] | 3338 | 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] | 3339 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3340 | if (errcode & ERR_CODE) |
| 3341 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3342 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3343 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3344 | |
| 3345 | /* succeed, add the instance to the ckch_store's list of instance */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3346 | LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3347 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3348 | } |
| 3349 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3350 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3351 | |
| 3352 | |
| 3353 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3354 | * done once. Zero is returned if the operation fails. No error is returned |
| 3355 | * if the random is said as not implemented, because we expect that openssl |
| 3356 | * will use another method once needed. |
| 3357 | */ |
| 3358 | static int ssl_initialize_random() |
| 3359 | { |
| 3360 | unsigned char random; |
| 3361 | static int random_initialized = 0; |
| 3362 | |
| 3363 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3364 | random_initialized = 1; |
| 3365 | |
| 3366 | return random_initialized; |
| 3367 | } |
| 3368 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3369 | /* Load a crt-list file, this is done in 2 parts: |
| 3370 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 3371 | * - generate the instances by iterating on entries in the crtlist struct |
| 3372 | * |
| 3373 | * Nothing is locked there, this function is used in the configuration parser. |
| 3374 | * |
| 3375 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3376 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3377 | 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] | 3378 | { |
| 3379 | struct crtlist *crtlist = NULL; |
| 3380 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3381 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3382 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3383 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3384 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3385 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3386 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 3387 | if (!bind_conf_node) { |
| 3388 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 3389 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3390 | goto error; |
| 3391 | } |
| 3392 | bind_conf_node->next = NULL; |
| 3393 | bind_conf_node->bind_conf = bind_conf; |
| 3394 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3395 | /* strip trailing slashes, including first one */ |
| 3396 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 3397 | *end = 0; |
| 3398 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3399 | /* look for an existing crtlist or create one */ |
| 3400 | eb = ebst_lookup(&crtlists_tree, file); |
| 3401 | if (eb) { |
| 3402 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 3403 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3404 | /* load a crt-list OR a directory */ |
| 3405 | if (dir) |
| 3406 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 3407 | else |
| 3408 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 3409 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3410 | if (!(cfgerr & ERR_CODE)) |
| 3411 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 3412 | } |
| 3413 | |
| 3414 | if (cfgerr & ERR_CODE) { |
| 3415 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3416 | goto error; |
| 3417 | } |
| 3418 | |
| 3419 | /* generates ckch instance from the crtlist_entry */ |
| 3420 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3421 | struct ckch_store *store; |
| 3422 | struct ckch_inst *ckch_inst = NULL; |
| 3423 | |
| 3424 | store = entry->node.key; |
| 3425 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 3426 | if (cfgerr & ERR_CODE) { |
| 3427 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 3428 | goto error; |
| 3429 | } |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3430 | LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 3431 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3432 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3433 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3434 | /* add the bind_conf to the list */ |
| 3435 | bind_conf_node->next = crtlist->bind_conf; |
| 3436 | crtlist->bind_conf = bind_conf_node; |
| 3437 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3438 | return cfgerr; |
| 3439 | error: |
| 3440 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3441 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3442 | struct ckch_inst *inst, *s_inst; |
| 3443 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3444 | lastentry = entry; /* which entry we tried to generate last */ |
| 3445 | if (lastentry) { |
| 3446 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3447 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 3448 | break; |
| 3449 | |
| 3450 | 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] | 3451 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3452 | /* this was not generated for this bind_conf, skip */ |
| 3453 | if (inst->bind_conf != bind_conf) |
| 3454 | continue; |
| 3455 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3456 | /* free the sni_ctx and instance */ |
| 3457 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3458 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3459 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3460 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3461 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3462 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3463 | return cfgerr; |
| 3464 | } |
| 3465 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3466 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 3467 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 3468 | { |
| 3469 | struct stat buf; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3470 | int cfgerr = 0; |
| 3471 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3472 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3473 | int found = 0; /* did we found a file to load ? */ |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3474 | |
| 3475 | if ((ckchs = ckchs_lookup(path))) { |
| 3476 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3477 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3478 | found++; |
| 3479 | } else if (stat(path, &buf) == 0) { |
| 3480 | found++; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3481 | if (S_ISDIR(buf.st_mode) == 0) { |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3482 | ckchs = ckchs_load_cert_file(path, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3483 | if (!ckchs) |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3484 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3485 | 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] | 3486 | } else { |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3487 | cfgerr |= 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] | 3488 | } |
| 3489 | } else { |
| 3490 | /* stat failed, could be a bundle */ |
| 3491 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3492 | char fp[MAXPATHLEN+1] = {0}; |
| 3493 | int n = 0; |
| 3494 | |
| 3495 | /* Load all possible certs and keys in separate ckch_store */ |
| 3496 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3497 | struct stat buf; |
| 3498 | int ret; |
| 3499 | |
| 3500 | ret = snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3501 | if (ret > sizeof(fp)) |
| 3502 | continue; |
| 3503 | |
| 3504 | if ((ckchs = ckchs_lookup(fp))) { |
| 3505 | cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3506 | found++; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3507 | } else { |
| 3508 | if (stat(fp, &buf) == 0) { |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3509 | found++; |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3510 | ckchs = ckchs_load_cert_file(fp, err); |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3511 | if (!ckchs) |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3512 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3513 | cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3514 | } |
| 3515 | } |
| 3516 | } |
William Lallemand | 00b831e | 2020-12-04 15:45:02 +0100 | [diff] [blame] | 3517 | #if HA_OPENSSL_VERSION_NUMBER < 0x10101000L |
| 3518 | if (found) { |
| 3519 | memprintf(err, "%sCan't load '%s'. Loading a multi certificates bundle requires OpenSSL >= 1.1.1\n", |
| 3520 | err && *err ? *err : "", path); |
| 3521 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3522 | } |
| 3523 | #endif |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3524 | } |
| 3525 | } |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3526 | if (!found) { |
| 3527 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3528 | err && *err ? *err : "", path, strerror(errno)); |
| 3529 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3530 | } |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3531 | |
| 3532 | return cfgerr; |
| 3533 | } |
| 3534 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3535 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3536 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3537 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3538 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3539 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3540 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3541 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3542 | SSL_OP_NO_SSLv2 | |
| 3543 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3544 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3545 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3546 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3547 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3548 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3549 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3550 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3551 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3552 | SSL_MODE_RELEASE_BUFFERS | |
| 3553 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3554 | 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] | 3555 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3556 | int flags = MC_SSL_O_ALL; |
| 3557 | int cfgerr = 0; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 3558 | const int default_min_ver = CONF_TLSV12; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3559 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3560 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3561 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3562 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3563 | 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] | 3564 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3565 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3566 | 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] | 3567 | else |
| 3568 | flags = conf_ssl_methods->flags; |
| 3569 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3570 | min = conf_ssl_methods->min; |
| 3571 | max = conf_ssl_methods->max; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 3572 | |
| 3573 | /* default minimum is TLSV12, */ |
| 3574 | if (!min) { |
| 3575 | if (!max || (max >= default_min_ver)) { |
| 3576 | min = default_min_ver; |
| 3577 | } else { |
| 3578 | 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). " |
| 3579 | "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n", |
| 3580 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name); |
| 3581 | min = max; |
| 3582 | } |
| 3583 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3584 | /* 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] | 3585 | if (min) |
| 3586 | flags |= (methodVersions[min].flag - 1); |
| 3587 | if (max) |
| 3588 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3589 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3590 | min = max = CONF_TLSV_NONE; |
| 3591 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3592 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3593 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3594 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3595 | if (min) { |
| 3596 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3597 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3598 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3599 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3600 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3601 | hole = 0; |
| 3602 | } |
| 3603 | max = i; |
| 3604 | } |
| 3605 | else { |
| 3606 | min = max = i; |
| 3607 | } |
| 3608 | } |
| 3609 | else { |
| 3610 | if (min) |
| 3611 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3612 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3613 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3614 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3615 | 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] | 3616 | cfgerr += 1; |
| 3617 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3618 | /* save real min/max in bind_conf */ |
| 3619 | conf_ssl_methods->min = min; |
| 3620 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3621 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3622 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3623 | /* 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] | 3624 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3625 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3626 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3627 | else |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 3628 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) { |
| 3629 | /* clear every version flags in case SSL_CTX_new() |
| 3630 | * returns an SSL_CTX with disabled versions */ |
| 3631 | SSL_CTX_clear_options(ctx, methodVersions[i].option); |
| 3632 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3633 | if (flags & methodVersions[i].flag) |
| 3634 | options |= methodVersions[i].option; |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 3635 | |
| 3636 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3637 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3638 | /* 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] | 3639 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3640 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3641 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3642 | |
| 3643 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3644 | options |= SSL_OP_NO_TICKET; |
| 3645 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3646 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 3647 | |
| 3648 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 3649 | options |= SSL_OP_NO_RENEGOTIATION; |
| 3650 | #endif |
| 3651 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3652 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3653 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3654 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3655 | if (global_ssl.async) |
| 3656 | mode |= SSL_MODE_ASYNC; |
| 3657 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3658 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3659 | if (global_ssl.life_time) |
| 3660 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3661 | |
| 3662 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3663 | #ifdef OPENSSL_IS_BORINGSSL |
| 3664 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3665 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 3666 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 3667 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 3668 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3669 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3670 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3671 | #else |
| 3672 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3673 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3674 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3675 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3676 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3677 | } |
| 3678 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3679 | |
| 3680 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3681 | { |
| 3682 | if (first == block) { |
| 3683 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3684 | if (first->len > 0) |
| 3685 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | /* return first block from sh_ssl_sess */ |
| 3690 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3691 | { |
| 3692 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3693 | |
| 3694 | } |
| 3695 | |
| 3696 | /* store a session into the cache |
| 3697 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3698 | * data: asn1 encoded session |
| 3699 | * data_len: asn1 encoded session length |
| 3700 | * Returns 1 id session was stored (else 0) |
| 3701 | */ |
| 3702 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3703 | { |
| 3704 | struct shared_block *first; |
| 3705 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3706 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3707 | 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] | 3708 | if (!first) { |
| 3709 | /* Could not retrieve enough free blocks to store that session */ |
| 3710 | return 0; |
| 3711 | } |
| 3712 | |
| 3713 | /* STORE the key in the first elem */ |
| 3714 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3715 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3716 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3717 | |
| 3718 | /* it returns the already existing node |
| 3719 | or current node if none, never returns null */ |
| 3720 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3721 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3722 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3723 | /* release the reserved row */ |
| 3724 | shctx_row_dec_hot(ssl_shctx, first); |
| 3725 | /* replace the previous session already in the tree */ |
| 3726 | sh_ssl_sess = oldsh_ssl_sess; |
| 3727 | /* ignore the previous session data, only use the header */ |
| 3728 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3729 | shctx_row_inc_hot(ssl_shctx, first); |
| 3730 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3731 | } |
| 3732 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3733 | 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] | 3734 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3735 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3736 | } |
| 3737 | |
| 3738 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3739 | |
| 3740 | return 1; |
| 3741 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3742 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3743 | /* SSL callback used when a new session is created while connecting to a server */ |
| 3744 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 3745 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 3746 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3747 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3748 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 3749 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3750 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3751 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 3752 | int len; |
| 3753 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3754 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3755 | len = i2d_SSL_SESSION(sess, NULL); |
| 3756 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 3757 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 3758 | } else { |
| 3759 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3760 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 3761 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 3762 | } |
| 3763 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 3764 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 3765 | &ptr); |
| 3766 | } |
| 3767 | } else { |
| 3768 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3769 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 3770 | } |
| 3771 | |
| 3772 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3773 | } |
| 3774 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3775 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3776 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3777 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3778 | { |
| 3779 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 3780 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 3781 | unsigned char *p; |
| 3782 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3783 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3784 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3785 | |
| 3786 | /* 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] | 3787 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3788 | * note: SSL_SESSION_set1_id is using |
| 3789 | * a memcpy so we need to use a different pointer |
| 3790 | * than sid_data or sid_ctx_data to avoid valgrind |
| 3791 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3792 | */ |
| 3793 | |
| 3794 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3795 | |
| 3796 | /* copy value in an other buffer */ |
| 3797 | memcpy(encid, sid_data, sid_length); |
| 3798 | |
| 3799 | /* pad with 0 */ |
| 3800 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 3801 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 3802 | |
| 3803 | /* force length to zero to avoid ASN1 encoding */ |
| 3804 | SSL_SESSION_set1_id(sess, encid, 0); |
| 3805 | |
| 3806 | /* force length to zero to avoid ASN1 encoding */ |
| 3807 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3808 | |
| 3809 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 3810 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 3811 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 3812 | goto err; |
| 3813 | |
| 3814 | p = encsess; |
| 3815 | |
| 3816 | /* process ASN1 session encoding before the lock */ |
| 3817 | i2d_SSL_SESSION(sess, &p); |
| 3818 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3819 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3820 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3821 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3822 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3823 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3824 | err: |
| 3825 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3826 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 3827 | 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] | 3828 | |
| 3829 | return 0; /* do not increment session reference count */ |
| 3830 | } |
| 3831 | |
| 3832 | /* 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] | 3833 | 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] | 3834 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3835 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3836 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 3837 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3838 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3839 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3840 | |
| 3841 | global.shctx_lookups++; |
| 3842 | |
| 3843 | /* allow the session to be freed automatically by openssl */ |
| 3844 | *do_copy = 0; |
| 3845 | |
| 3846 | /* tree key is zeros padded sessionid */ |
| 3847 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3848 | memcpy(tmpkey, key, key_len); |
| 3849 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 3850 | key = tmpkey; |
| 3851 | } |
| 3852 | |
| 3853 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3854 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3855 | |
| 3856 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3857 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 3858 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3859 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3860 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3861 | global.shctx_misses++; |
| 3862 | return NULL; |
| 3863 | } |
| 3864 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3865 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 3866 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3867 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3868 | 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] | 3869 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3870 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3871 | |
| 3872 | /* decode ASN1 session */ |
| 3873 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3874 | 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] | 3875 | /* Reset session id and session id contenxt */ |
| 3876 | if (sess) { |
| 3877 | SSL_SESSION_set1_id(sess, key, key_len); |
| 3878 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 3879 | } |
| 3880 | |
| 3881 | return sess; |
| 3882 | } |
| 3883 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3884 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3885 | /* 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] | 3886 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3887 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3888 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3889 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 3890 | unsigned int sid_length; |
| 3891 | const unsigned char *sid_data; |
| 3892 | (void)ctx; |
| 3893 | |
| 3894 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3895 | /* tree key is zeros padded sessionid */ |
| 3896 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3897 | memcpy(tmpkey, sid_data, sid_length); |
| 3898 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 3899 | sid_data = tmpkey; |
| 3900 | } |
| 3901 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3902 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3903 | |
| 3904 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3905 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 3906 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3907 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3908 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3909 | } |
| 3910 | |
| 3911 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3912 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3913 | } |
| 3914 | |
| 3915 | /* Set session cache mode to server and disable openssl internal cache. |
| 3916 | * Set shared cache callbacks on an ssl context. |
| 3917 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3918 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3919 | { |
| 3920 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 3921 | |
| 3922 | if (!ssl_shctx) { |
| 3923 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 3924 | return; |
| 3925 | } |
| 3926 | |
| 3927 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 3928 | SSL_SESS_CACHE_NO_INTERNAL | |
| 3929 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 3930 | |
| 3931 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3932 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 3933 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 3934 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3935 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 3936 | |
| 3937 | /* |
| 3938 | * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format |
| 3939 | * |
| 3940 | * The format is: |
| 3941 | * * <Label> <space> <ClientRandom> <space> <Secret> |
| 3942 | * We only need to copy the secret as there is a sample fetch for the ClientRandom |
| 3943 | */ |
| 3944 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 3945 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 3946 | void SSL_CTX_keylog(const SSL *ssl, const char *line) |
| 3947 | { |
| 3948 | struct ssl_keylog *keylog; |
| 3949 | char *lastarg = NULL; |
| 3950 | char *dst = NULL; |
| 3951 | |
| 3952 | keylog = SSL_get_ex_data(ssl, ssl_keylog_index); |
| 3953 | if (!keylog) |
| 3954 | return; |
| 3955 | |
| 3956 | lastarg = strrchr(line, ' '); |
| 3957 | if (lastarg == NULL || ++lastarg == NULL) |
| 3958 | return; |
| 3959 | |
| 3960 | dst = pool_alloc(pool_head_ssl_keylog_str); |
| 3961 | if (!dst) |
| 3962 | return; |
| 3963 | |
| 3964 | strncpy(dst, lastarg, SSL_KEYLOG_MAX_SECRET_SIZE-1); |
| 3965 | dst[SSL_KEYLOG_MAX_SECRET_SIZE-1] = '\0'; |
| 3966 | |
| 3967 | if (strncmp(line, "CLIENT_RANDOM ", strlen("CLIENT RANDOM ")) == 0) { |
| 3968 | if (keylog->client_random) |
| 3969 | goto error; |
| 3970 | keylog->client_random = dst; |
| 3971 | |
| 3972 | } else if (strncmp(line, "CLIENT_EARLY_TRAFFIC_SECRET ", strlen("CLIENT_EARLY_TRAFFIC_SECRET ")) == 0) { |
| 3973 | if (keylog->client_early_traffic_secret) |
| 3974 | goto error; |
| 3975 | keylog->client_early_traffic_secret = dst; |
| 3976 | |
| 3977 | } else if (strncmp(line, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ", strlen("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 3978 | if(keylog->client_handshake_traffic_secret) |
| 3979 | goto error; |
| 3980 | keylog->client_handshake_traffic_secret = dst; |
| 3981 | |
| 3982 | } else if (strncmp(line, "SERVER_HANDSHAKE_TRAFFIC_SECRET ", strlen("SERVER_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 3983 | if (keylog->server_handshake_traffic_secret) |
| 3984 | goto error; |
| 3985 | keylog->server_handshake_traffic_secret = dst; |
| 3986 | |
| 3987 | } else if (strncmp(line, "CLIENT_TRAFFIC_SECRET_0 ", strlen("CLIENT_TRAFFIC_SECRET_0 ")) == 0) { |
| 3988 | if (keylog->client_traffic_secret_0) |
| 3989 | goto error; |
| 3990 | keylog->client_traffic_secret_0 = dst; |
| 3991 | |
| 3992 | } else if (strncmp(line, "SERVER_TRAFFIC_SECRET_0 ", strlen("SERVER_TRAFFIC_SECRET_0 ")) == 0) { |
| 3993 | if (keylog->server_traffic_secret_0) |
| 3994 | goto error; |
| 3995 | keylog->server_traffic_secret_0 = dst; |
| 3996 | |
| 3997 | } else if (strncmp(line, "EARLY_EXPORTER_SECRET ", strlen("EARLY_EXPORTER_SECRET ")) == 0) { |
| 3998 | if (keylog->early_exporter_secret) |
| 3999 | goto error; |
| 4000 | keylog->early_exporter_secret = dst; |
| 4001 | |
| 4002 | } else if (strncmp(line, "EXPORTER_SECRET ", strlen("EXPORTER_SECRET ")) == 0) { |
| 4003 | if (keylog->exporter_secret) |
| 4004 | goto error; |
| 4005 | keylog->exporter_secret = dst; |
| 4006 | } else { |
| 4007 | goto error; |
| 4008 | } |
| 4009 | |
| 4010 | return; |
| 4011 | |
| 4012 | error: |
| 4013 | pool_free(pool_head_ssl_keylog_str, dst); |
| 4014 | |
| 4015 | return; |
| 4016 | } |
| 4017 | #endif |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4018 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4019 | /* |
| 4020 | * This function applies the SSL configuration on a SSL_CTX |
| 4021 | * It returns an error code and fills the <err> buffer |
| 4022 | */ |
| 4023 | 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] | 4024 | { |
| 4025 | struct proxy *curproxy = bind_conf->frontend; |
| 4026 | int cfgerr = 0; |
| 4027 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4028 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4029 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4030 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4031 | const char *conf_ciphersuites; |
| 4032 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4033 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4034 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4035 | if (ssl_conf) { |
| 4036 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4037 | int i, min, max; |
| 4038 | int flags = MC_SSL_O_ALL; |
| 4039 | |
| 4040 | /* 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] | 4041 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4042 | 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] | 4043 | if (min) |
| 4044 | flags |= (methodVersions[min].flag - 1); |
| 4045 | if (max) |
| 4046 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4047 | min = max = CONF_TLSV_NONE; |
| 4048 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4049 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4050 | if (min) |
| 4051 | max = i; |
| 4052 | else |
| 4053 | min = max = i; |
| 4054 | } |
| 4055 | /* save real min/max */ |
| 4056 | conf_ssl_methods->min = min; |
| 4057 | conf_ssl_methods->max = max; |
| 4058 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4059 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4060 | 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] | 4061 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4062 | } |
| 4063 | } |
| 4064 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4065 | 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] | 4066 | case SSL_SOCK_VERIFY_NONE: |
| 4067 | verify = SSL_VERIFY_NONE; |
| 4068 | break; |
| 4069 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4070 | verify = SSL_VERIFY_PEER; |
| 4071 | break; |
| 4072 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4073 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4074 | break; |
| 4075 | } |
| 4076 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4077 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4078 | 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] | 4079 | 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] | 4080 | 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] | 4081 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4082 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4083 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4084 | 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] | 4085 | 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] | 4086 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4087 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4088 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 4089 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 4090 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4091 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4092 | } |
| 4093 | 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] | 4094 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 4095 | 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] | 4096 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4097 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4098 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4099 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4100 | 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] | 4101 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4102 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4103 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4104 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4105 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4106 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4107 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4108 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4109 | 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] | 4110 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4111 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4112 | else { |
| 4113 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4114 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4115 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4116 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4117 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4118 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4119 | #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] | 4120 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4121 | 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] | 4122 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4123 | 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] | 4124 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4125 | } |
| 4126 | } |
| 4127 | #endif |
| 4128 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4129 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4130 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4131 | if (conf_ciphers && |
| 4132 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4133 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4134 | 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] | 4135 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4136 | } |
| 4137 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4138 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4139 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4140 | if (conf_ciphersuites && |
| 4141 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4142 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4143 | 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] | 4144 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4145 | } |
| 4146 | #endif |
| 4147 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4148 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4149 | /* If tune.ssl.default-dh-param has not been set, |
| 4150 | neither has ssl-default-dh-file and no static DH |
| 4151 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4152 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4153 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4154 | (ssl_dh_ptr_index == -1 || |
| 4155 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Willy Tarreau | 3ba77d2 | 2020-05-08 09:31:18 +0200 | [diff] [blame] | 4156 | /* default to dh-param 2048 */ |
| 4157 | global_ssl.default_dh_param = 2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4158 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4159 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4160 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4161 | if (local_dh_1024 == NULL) { |
| 4162 | local_dh_1024 = ssl_get_dh_1024(); |
| 4163 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4164 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4165 | if (local_dh_2048 == NULL) { |
| 4166 | local_dh_2048 = ssl_get_dh_2048(); |
| 4167 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4168 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4169 | if (local_dh_4096 == NULL) { |
| 4170 | local_dh_4096 = ssl_get_dh_4096(); |
| 4171 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4172 | } |
| 4173 | } |
| 4174 | } |
| 4175 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4176 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4177 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Ilya Shipitsin | bcc2560 | 2021-02-08 16:55:06 +0500 | [diff] [blame] | 4178 | #ifdef SSL_CTRL_SET_MSG_CALLBACK |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4179 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4180 | #endif |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 4181 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4182 | SSL_CTX_set_keylog_callback(ctx, SSL_CTX_keylog); |
| 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 |
Ilya Shipitsin | 0aa8c29 | 2020-11-04 00:39:07 +0500 | [diff] [blame] | 4203 | #if defined(SSL_CTX_set1_curves_list) |
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 */ |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4255 | if (strcasecmp(pattern, hostname) == 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 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 |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4286 | || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 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); |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4298 | if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0)) |
| 4299 | || (suffixlen && (strncasecmp(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 | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 4747 | if (srv->ssl_ctx.reused_sess) { |
| 4748 | int i; |
| 4749 | |
| 4750 | for (i = 0; i < global.nbthread; i++) |
| 4751 | free(srv->ssl_ctx.reused_sess[i].ptr); |
| 4752 | free(srv->ssl_ctx.reused_sess); |
| 4753 | } |
| 4754 | |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4755 | if (srv->ssl_ctx.ctx) |
| 4756 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4757 | } |
| 4758 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4759 | /* 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] | 4760 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4761 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4762 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4763 | { |
| 4764 | struct ebmb_node *node, *back; |
| 4765 | struct sni_ctx *sni; |
| 4766 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4767 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4768 | while (node) { |
| 4769 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4770 | back = ebmb_next(node); |
| 4771 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4772 | SSL_CTX_free(sni->ctx); |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 4773 | LIST_DEL(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4774 | free(sni); |
| 4775 | node = back; |
| 4776 | } |
| 4777 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4778 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4779 | while (node) { |
| 4780 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4781 | back = ebmb_next(node); |
| 4782 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4783 | SSL_CTX_free(sni->ctx); |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 4784 | LIST_DEL(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4785 | free(sni); |
| 4786 | node = back; |
| 4787 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 4788 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4789 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4790 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4791 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4792 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4793 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4794 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 4795 | |
| 4796 | |
| 4797 | void ssl_sock_deinit() |
| 4798 | { |
| 4799 | crtlist_deinit(); /* must be free'd before the ckchs */ |
| 4800 | ckch_deinit(); |
| 4801 | } |
| 4802 | REGISTER_POST_DEINIT(ssl_sock_deinit); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4803 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4804 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4805 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4806 | { |
| 4807 | ssl_sock_free_ca(bind_conf); |
| 4808 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4809 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4810 | free(bind_conf->ca_sign_file); |
| 4811 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 4812 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4813 | free(bind_conf->keys_ref->filename); |
| 4814 | free(bind_conf->keys_ref->tlskeys); |
| 4815 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4816 | free(bind_conf->keys_ref); |
| 4817 | } |
| 4818 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4819 | bind_conf->ca_sign_pass = NULL; |
| 4820 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4821 | } |
| 4822 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4823 | /* Load CA cert file and private key used to generate certificates */ |
| 4824 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4825 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4826 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4827 | struct proxy *px = bind_conf->frontend; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4828 | struct cert_key_and_chain *ckch = NULL; |
| 4829 | int ret = 0; |
| 4830 | char *err = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4831 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 4832 | if (!bind_conf->generate_certs) |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4833 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4834 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4835 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4836 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4837 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4838 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4839 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4840 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4841 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4842 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4843 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4844 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 4845 | "no CA certificate File configured at [%s:%d].\n", |
| 4846 | px->id, bind_conf->file, bind_conf->line); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4847 | goto failed; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4848 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4849 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4850 | /* Allocate cert structure */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 4851 | ckch = calloc(1, sizeof(*ckch)); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4852 | if (!ckch) { |
| 4853 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain allocation failure\n", |
| 4854 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 4855 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4856 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4857 | |
| 4858 | /* Try to parse file */ |
| 4859 | if (ssl_sock_load_files_into_ckch(bind_conf->ca_sign_file, ckch, &err)) { |
| 4860 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain loading failed: %s\n", |
| 4861 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line, err); |
| 4862 | if (err) free(err); |
| 4863 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4864 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4865 | |
| 4866 | /* Fail if missing cert or pkey */ |
| 4867 | if ((!ckch->cert) || (!ckch->key)) { |
| 4868 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain missing certificate or private key\n", |
| 4869 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 4870 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4871 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4872 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4873 | /* Final assignment to bind */ |
| 4874 | bind_conf->ca_sign_ckch = ckch; |
| 4875 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4876 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4877 | failed: |
| 4878 | if (ckch) { |
| 4879 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 4880 | free(ckch); |
| 4881 | } |
| 4882 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4883 | bind_conf->generate_certs = 0; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4884 | ret++; |
| 4885 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4886 | } |
| 4887 | |
| 4888 | /* Release CA cert and private key used to generate certificated */ |
| 4889 | void |
| 4890 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4891 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4892 | if (bind_conf->ca_sign_ckch) { |
| 4893 | ssl_sock_free_cert_key_and_chain_contents(bind_conf->ca_sign_ckch); |
| 4894 | free(bind_conf->ca_sign_ckch); |
| 4895 | bind_conf->ca_sign_ckch = NULL; |
| 4896 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4897 | } |
| 4898 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4899 | /* |
| 4900 | * This function is called if SSL * context is not yet allocated. The function |
| 4901 | * is designed to be called before any other data-layer operation and sets the |
| 4902 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4903 | * It returns 0 on success and -1 in error case. |
| 4904 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4905 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4906 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4907 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4908 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4909 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4910 | return 0; |
| 4911 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4912 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4913 | return 0; |
| 4914 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4915 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 4916 | if (!ctx) { |
| 4917 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4918 | return -1; |
| 4919 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4920 | ctx->wait_event.tasklet = tasklet_new(); |
| 4921 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4922 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4923 | pool_free(ssl_sock_ctx_pool, ctx); |
| 4924 | return -1; |
| 4925 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4926 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 4927 | ctx->wait_event.tasklet->context = ctx; |
Willy Tarreau | b421f9d | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 4928 | ctx->wait_event.tasklet->state |= TASK_HEAVY; // assign it to the bulk queue during handshake |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4929 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 4930 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 4931 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4932 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 4933 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 4934 | ctx->xprt_st = 0; |
| 4935 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4936 | |
| 4937 | /* Only work with sockets for now, this should be adapted when we'll |
| 4938 | * add QUIC support. |
| 4939 | */ |
| 4940 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 4941 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4942 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 4943 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 4944 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4945 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4946 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4947 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4948 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4949 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4950 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4951 | /* If it is in client mode initiate SSL session |
| 4952 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4953 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4954 | int may_retry = 1; |
| 4955 | |
| 4956 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4957 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4958 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 4959 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4960 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4961 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4962 | goto retry_connect; |
| 4963 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4964 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4965 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4966 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4967 | ctx->bio = BIO_new(ha_meth); |
| 4968 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 4969 | SSL_free(ctx->ssl); |
| 4970 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4971 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4972 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4973 | goto retry_connect; |
| 4974 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4975 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4976 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4977 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4978 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4979 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4980 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4981 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4982 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 4983 | SSL_free(ctx->ssl); |
| 4984 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4985 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4986 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4987 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4988 | goto retry_connect; |
| 4989 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4990 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4991 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4992 | } |
| 4993 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4994 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4995 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 4996 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 4997 | 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] | 4998 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4999 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5000 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5001 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5002 | } else if (sess) { |
| 5003 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5004 | } |
| 5005 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5006 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5007 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5008 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5009 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5010 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5011 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5012 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5013 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5014 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5015 | return 0; |
| 5016 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5017 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5018 | int may_retry = 1; |
| 5019 | |
| 5020 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5021 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5022 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 5023 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5024 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5025 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5026 | goto retry_accept; |
| 5027 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5028 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5029 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5030 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5031 | ctx->bio = BIO_new(ha_meth); |
| 5032 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 5033 | SSL_free(ctx->ssl); |
| 5034 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5035 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5036 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5037 | goto retry_accept; |
| 5038 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5039 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5040 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5041 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5042 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5043 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5044 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5045 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5046 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5047 | SSL_free(ctx->ssl); |
| 5048 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5049 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5050 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5051 | goto retry_accept; |
| 5052 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5053 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5054 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5055 | } |
| 5056 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5057 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 5058 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 5059 | b_alloc(&ctx->early_buf); |
| 5060 | SSL_set_max_early_data(ctx->ssl, |
| 5061 | /* Only allow early data if we managed to allocate |
| 5062 | * a buffer. |
| 5063 | */ |
| 5064 | (!b_is_null(&ctx->early_buf)) ? |
| 5065 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 5066 | } |
| 5067 | #endif |
| 5068 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5069 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5070 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5071 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5072 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5073 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Willy Tarreau | 5c6176d | 2021-02-05 10:24:54 +0100 | [diff] [blame] | 5074 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) |
Willy Tarreau | 5930fb2 | 2021-02-03 11:21:38 +0100 | [diff] [blame] | 5075 | conn->flags |= CO_FL_EARLY_SSL_HS; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5076 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5077 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5078 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5079 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5080 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5081 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5082 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5083 | return 0; |
| 5084 | } |
| 5085 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5086 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5087 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5088 | if (ctx && ctx->wait_event.tasklet) |
| 5089 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5090 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5091 | return -1; |
| 5092 | } |
| 5093 | |
| 5094 | |
| 5095 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5096 | * updates the FD status if it wants some polling before being called again. |
| 5097 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5098 | * otherwise it returns non-zero and removes itself from the connection's |
| 5099 | * flags (the bit is provided in <flag> by the caller). |
| 5100 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5101 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5102 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5103 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5104 | int ret; |
Willy Tarreau | d287140 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5105 | socklen_t lskerr; |
| 5106 | int skerr; |
| 5107 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5108 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5109 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5110 | return 0; |
| 5111 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5112 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5113 | goto out_error; |
| 5114 | |
Willy Tarreau | d287140 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5115 | /* don't start calculating a handshake on a dead connection */ |
| 5116 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) |
| 5117 | goto out_error; |
| 5118 | |
| 5119 | /* FIXME/WT: for now we don't have a clear way to inspect the connection |
| 5120 | * status from the lower layers, so let's check the FD directly. Ideally |
| 5121 | * the xprt layers should provide some status indicating their knowledge |
| 5122 | * of shutdowns or error. |
| 5123 | */ |
| 5124 | skerr = 0; |
| 5125 | lskerr = sizeof(skerr); |
| 5126 | if ((getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) < 0) || |
| 5127 | skerr != 0) |
| 5128 | goto out_error; |
| 5129 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5130 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5131 | /* |
| 5132 | * Check if we have early data. If we do, we have to read them |
| 5133 | * before SSL_do_handshake() is called, And there's no way to |
| 5134 | * detect early data, except to try to read them |
| 5135 | */ |
| 5136 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5137 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5138 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5139 | while (1) { |
| 5140 | ret = SSL_read_early_data(ctx->ssl, |
| 5141 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 5142 | &read_data); |
| 5143 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5144 | goto check_error; |
| 5145 | if (read_data > 0) { |
| 5146 | conn->flags |= CO_FL_EARLY_DATA; |
| 5147 | b_add(&ctx->early_buf, read_data); |
| 5148 | } |
| 5149 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5150 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5151 | if (!b_data(&ctx->early_buf)) |
| 5152 | b_free(&ctx->early_buf); |
| 5153 | break; |
| 5154 | } |
| 5155 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5156 | } |
| 5157 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5158 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5159 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5160 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5161 | * the reneg handshake. |
| 5162 | * Here we use SSL_peek as a workaround for reneg. |
| 5163 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5164 | 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] | 5165 | char c; |
| 5166 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5167 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5168 | if (ret <= 0) { |
| 5169 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5170 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5171 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5172 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5173 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5174 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5175 | 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] | 5176 | return 0; |
| 5177 | } |
| 5178 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5179 | /* handshake may have been completed but we have |
| 5180 | * no more data to read. |
| 5181 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5182 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5183 | ret = 1; |
| 5184 | goto reneg_ok; |
| 5185 | } |
| 5186 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5187 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5188 | 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] | 5189 | return 0; |
| 5190 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5191 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5192 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5193 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5194 | return 0; |
| 5195 | } |
| 5196 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5197 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5198 | /* if errno is null, then connection was successfully established */ |
| 5199 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5200 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5201 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5202 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5203 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5204 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5205 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5206 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5207 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5208 | /* 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] | 5209 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5210 | empty_handshake = state == TLS_ST_BEFORE; |
| 5211 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5212 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5213 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5214 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5215 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5216 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5217 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5218 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5219 | else |
| 5220 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5221 | } |
| 5222 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5223 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5224 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5225 | else |
| 5226 | conn->err_code = CO_ER_SSL_ABORT; |
| 5227 | } |
| 5228 | } |
| 5229 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5230 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5231 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5232 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5233 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5234 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5235 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5236 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5237 | goto out_error; |
| 5238 | } |
| 5239 | else { |
| 5240 | /* Fail on all other handshake errors */ |
| 5241 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5242 | * buffer, causing an RST to be emitted upon close() on |
| 5243 | * TCP sockets. We first try to drain possibly pending |
| 5244 | * data to avoid this as much as possible. |
| 5245 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5246 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5247 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5248 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5249 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5250 | goto out_error; |
| 5251 | } |
| 5252 | } |
| 5253 | /* read some data: consider handshake completed */ |
| 5254 | goto reneg_ok; |
| 5255 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5256 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5257 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5258 | if (ret != 1) { |
| 5259 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5260 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5261 | |
| 5262 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5263 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5264 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5265 | 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] | 5266 | return 0; |
| 5267 | } |
| 5268 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5269 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5270 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5271 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5272 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5273 | return 0; |
| 5274 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5275 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5276 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5277 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5278 | return 0; |
| 5279 | } |
| 5280 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5281 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5282 | /* if errno is null, then connection was successfully established */ |
| 5283 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5284 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5285 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5286 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5287 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5288 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5289 | #else |
| 5290 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5291 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5292 | /* 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] | 5293 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5294 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5295 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5296 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5297 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5298 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5299 | if (empty_handshake) { |
| 5300 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5301 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5302 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5303 | else |
| 5304 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5305 | } |
| 5306 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5307 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5308 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5309 | else |
| 5310 | conn->err_code = CO_ER_SSL_ABORT; |
| 5311 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5312 | } |
| 5313 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5314 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5315 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5316 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5317 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5318 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5319 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5320 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5321 | goto out_error; |
| 5322 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5323 | else { |
| 5324 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5325 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5326 | * buffer, causing an RST to be emitted upon close() on |
| 5327 | * TCP sockets. We first try to drain possibly pending |
| 5328 | * data to avoid this as much as possible. |
| 5329 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5330 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5331 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5332 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5333 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5334 | goto out_error; |
| 5335 | } |
| 5336 | } |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5337 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5338 | else { |
| 5339 | /* |
| 5340 | * If the server refused the early data, we have to send a |
| 5341 | * 425 to the client, as we no longer have the data to sent |
| 5342 | * them again. |
| 5343 | */ |
| 5344 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5345 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5346 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5347 | goto out_error; |
| 5348 | } |
| 5349 | } |
| 5350 | } |
| 5351 | #endif |
| 5352 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5353 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5354 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5355 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5356 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5357 | /* ASYNC engine API doesn't support moving read/write |
| 5358 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5359 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5360 | */ |
| 5361 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5362 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5363 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5364 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5365 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5366 | if (objt_server(conn->target)) { |
| 5367 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5368 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5369 | 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] | 5370 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5371 | else { |
| 5372 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5373 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5374 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5375 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5376 | } |
| 5377 | |
| 5378 | /* The connection is now established at both layers, it's time to leave */ |
| 5379 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5380 | return 1; |
| 5381 | |
| 5382 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5383 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5384 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5385 | ERR_clear_error(); |
| 5386 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5387 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5388 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5389 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5390 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5391 | } |
| 5392 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5393 | /* Fail on all other handshake errors */ |
| 5394 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5395 | if (!conn->err_code) |
| 5396 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5397 | return 0; |
| 5398 | } |
| 5399 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5400 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5401 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5402 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5403 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 5404 | * unless the transport layer was already released. |
| 5405 | */ |
| 5406 | 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] | 5407 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5408 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5409 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 5410 | if (!ctx) |
| 5411 | return -1; |
| 5412 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5413 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5414 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5415 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5416 | ctx->subs = es; |
| 5417 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5418 | |
| 5419 | /* we may have to subscribe to lower layers for new events */ |
| 5420 | event_type &= ~ctx->wait_event.events; |
| 5421 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 5422 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5423 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5426 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5427 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5428 | * subscribe() call. It always returns zero. |
| 5429 | */ |
| 5430 | 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] | 5431 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5432 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5433 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5434 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5435 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5436 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5437 | es->events &= ~event_type; |
| 5438 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5439 | ctx->subs = NULL; |
| 5440 | |
| 5441 | /* If we subscribed, and we're not doing the handshake, |
| 5442 | * then we subscribed because the upper layer asked for it, |
| 5443 | * as the upper layer is no longer interested, we can |
| 5444 | * unsubscribe too. |
| 5445 | */ |
| 5446 | event_type &= ctx->wait_event.events; |
| 5447 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 5448 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5449 | |
| 5450 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5451 | } |
| 5452 | |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5453 | /* The connection has been taken over, so destroy the old tasklet and create |
| 5454 | * a new one. The original thread ID must be passed into orig_tid |
| 5455 | * It should be called with the takeover lock for the old thread held. |
| 5456 | * Returns 0 on success, and -1 on failure |
| 5457 | */ |
| 5458 | static int ssl_takeover(struct connection *conn, void *xprt_ctx, int orig_tid) |
| 5459 | { |
| 5460 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5461 | struct tasklet *tl = tasklet_new(); |
| 5462 | |
| 5463 | if (!tl) |
| 5464 | return -1; |
| 5465 | |
| 5466 | ctx->wait_event.tasklet->context = NULL; |
| 5467 | tasklet_wakeup_on(ctx->wait_event.tasklet, orig_tid); |
| 5468 | ctx->wait_event.tasklet = tl; |
| 5469 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 5470 | ctx->wait_event.tasklet->context = ctx; |
| 5471 | return 0; |
| 5472 | } |
| 5473 | |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 5474 | /* notify the next xprt that the connection is about to become idle and that it |
| 5475 | * may be stolen at any time after the function returns and that any tasklet in |
| 5476 | * the chain must be careful before dereferencing its context. |
| 5477 | */ |
| 5478 | static void ssl_set_idle(struct connection *conn, void *xprt_ctx) |
| 5479 | { |
| 5480 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5481 | |
| 5482 | if (!ctx || !ctx->wait_event.tasklet) |
| 5483 | return; |
| 5484 | |
| 5485 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 5486 | if (ctx->xprt) |
| 5487 | xprt_set_idle(conn, ctx->xprt, ctx->xprt_ctx); |
| 5488 | } |
| 5489 | |
| 5490 | /* notify the next xprt that the connection is not idle anymore and that it may |
| 5491 | * not be stolen before the next xprt_set_idle(). |
| 5492 | */ |
| 5493 | static void ssl_set_used(struct connection *conn, void *xprt_ctx) |
| 5494 | { |
| 5495 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5496 | |
| 5497 | if (!ctx || !ctx->wait_event.tasklet) |
| 5498 | return; |
| 5499 | |
| 5500 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 5501 | if (ctx->xprt) |
| 5502 | xprt_set_used(conn, ctx->xprt, ctx->xprt_ctx); |
| 5503 | } |
| 5504 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 5505 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 5506 | * Returns 0 on success, and non-zero on failure. |
| 5507 | */ |
| 5508 | 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) |
| 5509 | { |
| 5510 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5511 | |
| 5512 | if (oldxprt_ops != NULL) |
| 5513 | *oldxprt_ops = ctx->xprt; |
| 5514 | if (oldxprt_ctx != NULL) |
| 5515 | *oldxprt_ctx = ctx->xprt_ctx; |
| 5516 | ctx->xprt = toadd_ops; |
| 5517 | ctx->xprt_ctx = toadd_ctx; |
| 5518 | return 0; |
| 5519 | } |
| 5520 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 5521 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 5522 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 5523 | * XPRT. |
| 5524 | */ |
| 5525 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 5526 | { |
| 5527 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5528 | |
| 5529 | if (ctx->xprt_ctx == toremove_ctx) { |
| 5530 | ctx->xprt_ctx = newctx; |
| 5531 | ctx->xprt = newops; |
| 5532 | return 0; |
| 5533 | } |
| 5534 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 5535 | } |
| 5536 | |
Willy Tarreau | 9a5a0e0 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 5537 | struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5538 | { |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5539 | struct tasklet *tl = (struct tasklet *)t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5540 | struct ssl_sock_ctx *ctx = context; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5541 | struct connection *conn; |
| 5542 | int conn_in_list; |
| 5543 | int ret = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5544 | |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 5545 | if (state & TASK_F_USR1) { |
| 5546 | /* the tasklet was idling on an idle connection, it might have |
| 5547 | * been stolen, let's be careful! |
| 5548 | */ |
| 5549 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 5550 | if (tl->context == NULL) { |
| 5551 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 5552 | tasklet_free(tl); |
| 5553 | return NULL; |
| 5554 | } |
| 5555 | conn = ctx->conn; |
| 5556 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 5557 | if (conn_in_list) |
| 5558 | MT_LIST_DEL(&conn->list); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5559 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 5560 | } else { |
| 5561 | conn = ctx->conn; |
| 5562 | conn_in_list = 0; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5563 | } |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 5564 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5565 | /* First if we're doing an handshake, try that */ |
Willy Tarreau | b421f9d | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5566 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5567 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
Willy Tarreau | b421f9d | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5568 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 5569 | /* handshake completed, leave the bulk queue */ |
Willy Tarreau | a0c6da1 | 2021-03-09 17:58:02 +0100 | [diff] [blame] | 5570 | _HA_ATOMIC_AND(&tl->state, ~TASK_HEAVY); |
Willy Tarreau | b421f9d | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5571 | } |
| 5572 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5573 | /* If we had an error, or the handshake is done and I/O is available, |
| 5574 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5575 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5576 | * we can't be sure conn_fd_handler() will be called again. |
| 5577 | */ |
| 5578 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 5579 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5580 | int woke = 0; |
| 5581 | |
| 5582 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5583 | if (ctx->subs) { |
| 5584 | tasklet_wakeup(ctx->subs->tasklet); |
| 5585 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5586 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5587 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5588 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5589 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5590 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5591 | * upper layers know. If we have no mux, create it, |
| 5592 | * and once we have a mux, call its wake method if we didn't |
| 5593 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5594 | */ |
| 5595 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5596 | if (!ctx->conn->mux) |
| 5597 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5598 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5599 | ret = ctx->conn->mux->wake(ctx->conn); |
| 5600 | goto leave; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5601 | } |
| 5602 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5603 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5604 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5605 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 5606 | ctx->subs->events & SUB_RETRY_RECV) { |
| 5607 | tasklet_wakeup(ctx->subs->tasklet); |
| 5608 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 5609 | if (!ctx->subs->events) |
| 5610 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5611 | } |
| 5612 | #endif |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5613 | leave: |
| 5614 | if (!ret && conn_in_list) { |
| 5615 | struct server *srv = objt_server(conn->target); |
| 5616 | |
| 5617 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | a9d7b76 | 2020-07-10 08:28:20 +0200 | [diff] [blame] | 5618 | MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5619 | else |
Willy Tarreau | a9d7b76 | 2020-07-10 08:28:20 +0200 | [diff] [blame] | 5620 | MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5621 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5622 | return NULL; |
| 5623 | } |
| 5624 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5625 | /* 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] | 5626 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5627 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5628 | * flags are updated with whatever special event is detected (error, read0, |
| 5629 | * empty). The caller is responsible for taking care of those events and |
| 5630 | * avoiding the call if inappropriate. The function does not call the |
| 5631 | * connection's polling update function, so the caller is responsible for this. |
| 5632 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5633 | 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] | 5634 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5635 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5636 | ssize_t ret; |
| 5637 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5638 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5639 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5640 | goto out_error; |
| 5641 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5642 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5643 | if (b_data(&ctx->early_buf)) { |
| 5644 | try = b_contig_space(buf); |
| 5645 | if (try > b_data(&ctx->early_buf)) |
| 5646 | try = b_data(&ctx->early_buf); |
| 5647 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 5648 | b_add(buf, try); |
| 5649 | b_del(&ctx->early_buf, try); |
| 5650 | if (b_data(&ctx->early_buf) == 0) |
| 5651 | b_free(&ctx->early_buf); |
| 5652 | return try; |
| 5653 | } |
| 5654 | #endif |
| 5655 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5656 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5657 | /* a handshake was requested */ |
| 5658 | return 0; |
| 5659 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5660 | /* read the largest possible block. For this, we perform only one call |
| 5661 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5662 | * in which case we accept to do it once again. A new attempt is made on |
| 5663 | * EINTR too. |
| 5664 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5665 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5666 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5667 | try = b_contig_space(buf); |
| 5668 | if (!try) |
| 5669 | break; |
| 5670 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5671 | if (try > count) |
| 5672 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5673 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5674 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5675 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5676 | if (conn->flags & CO_FL_ERROR) { |
| 5677 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5678 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5679 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5680 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5681 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5682 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5683 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5684 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5685 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5686 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5687 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5688 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5689 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5690 | 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] | 5691 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5692 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5693 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5694 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5695 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5696 | break; |
| 5697 | } |
| 5698 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5699 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5700 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5701 | SUB_RETRY_RECV, |
| 5702 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5703 | /* handshake is running, and it may need to re-enable read */ |
| 5704 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5705 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5706 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5707 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5708 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5709 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5710 | break; |
| 5711 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5712 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5713 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5714 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5715 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5716 | * stack before shutting down the connection for |
| 5717 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5718 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5719 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5720 | /* otherwise it's a real error */ |
| 5721 | goto out_error; |
| 5722 | } |
| 5723 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5724 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5725 | return done; |
| 5726 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5727 | clear_ssl_error: |
| 5728 | /* Clear openssl global errors stack */ |
| 5729 | ssl_sock_dump_errors(conn); |
| 5730 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5731 | read0: |
| 5732 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5733 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5734 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5735 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5736 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5737 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5738 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5739 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5740 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5741 | } |
| 5742 | |
| 5743 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5744 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5745 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5746 | * 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] | 5747 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5748 | * a second call may be performed. The connection's flags are updated with |
| 5749 | * whatever special event is detected (error, empty). The caller is responsible |
| 5750 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5751 | * 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] | 5752 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5753 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5754 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5755 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5756 | 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] | 5757 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5758 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5759 | ssize_t ret; |
| 5760 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5761 | |
| 5762 | done = 0; |
| 5763 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5764 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5765 | goto out_error; |
| 5766 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5767 | 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] | 5768 | /* a handshake was requested */ |
| 5769 | return 0; |
| 5770 | |
| 5771 | /* send the largest possible block. For this we perform only one call |
| 5772 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5773 | * in which case we accept to do it once again. |
| 5774 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5775 | while (count) { |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5776 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5777 | size_t written_data; |
| 5778 | #endif |
| 5779 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5780 | try = b_contig_data(buf, done); |
| 5781 | if (try > count) |
| 5782 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5783 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5784 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5785 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5786 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5787 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5788 | } |
| 5789 | else { |
| 5790 | /* we need to keep the information about the fact that |
| 5791 | * we're not limiting the upcoming send(), because if it |
| 5792 | * fails, we'll have to retry with at least as many data. |
| 5793 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5794 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5795 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5796 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5797 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5798 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5799 | unsigned int max_early; |
| 5800 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5801 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5802 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5803 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5804 | if (SSL_get0_session(ctx->ssl)) |
| 5805 | 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] | 5806 | else |
| 5807 | max_early = 0; |
| 5808 | } |
| 5809 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5810 | if (try + ctx->sent_early_data > max_early) { |
| 5811 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5812 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5813 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5814 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5815 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5816 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5817 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5818 | 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] | 5819 | if (ret == 1) { |
| 5820 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5821 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5822 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5823 | 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] | 5824 | /* Initiate the handshake, now */ |
| 5825 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 5826 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5827 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5828 | } |
| 5829 | |
| 5830 | } else |
| 5831 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5832 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5833 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5834 | if (conn->flags & CO_FL_ERROR) { |
| 5835 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5836 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5837 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5838 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5839 | /* A send succeeded, so we can consider ourself connected */ |
| 5840 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5841 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5842 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5843 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5844 | } |
| 5845 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5846 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5847 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5848 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5849 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5850 | /* handshake is running, and it may need to re-enable write */ |
| 5851 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5852 | 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] | 5853 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5854 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5855 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5856 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5857 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5858 | break; |
| 5859 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5860 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5861 | break; |
| 5862 | } |
| 5863 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5864 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5865 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5866 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5867 | SUB_RETRY_RECV, |
| 5868 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5869 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5870 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5871 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5872 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5873 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5874 | break; |
| 5875 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5876 | goto out_error; |
| 5877 | } |
| 5878 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5879 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5880 | return done; |
| 5881 | |
| 5882 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5883 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5884 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5885 | ERR_clear_error(); |
| 5886 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5887 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5888 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5889 | } |
| 5890 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5891 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5892 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5893 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5894 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5895 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5896 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5897 | if (ctx->wait_event.events != 0) |
| 5898 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 5899 | ctx->wait_event.events, |
| 5900 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5901 | if (ctx->subs) { |
| 5902 | ctx->subs->events = 0; |
| 5903 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5904 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5905 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 5906 | if (ctx->xprt->close) |
| 5907 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5908 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5909 | if (global_ssl.async) { |
| 5910 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5911 | size_t num_all_fds = 0; |
| 5912 | int i; |
| 5913 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5914 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5915 | if (num_all_fds > 32) { |
| 5916 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5917 | return; |
| 5918 | } |
| 5919 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5920 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5921 | |
| 5922 | /* If an async job is pending, we must try to |
| 5923 | to catch the end using polling before calling |
| 5924 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5925 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5926 | for (i=0 ; i < num_all_fds ; i++) { |
| 5927 | /* switch on an handler designed to |
| 5928 | * handle the SSL_free |
| 5929 | */ |
| 5930 | afd = all_fd[i]; |
| 5931 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5932 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5933 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5934 | /* To ensure that the fd cache won't be used |
| 5935 | * and we'll catch a real RD event. |
| 5936 | */ |
| 5937 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5938 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5939 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5940 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5941 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5942 | return; |
| 5943 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5944 | /* Else we can remove the fds from the fdtab |
| 5945 | * and call SSL_free. |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 5946 | * note: we do a fd_stop_both and not a delete |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5947 | * because the fd is owned by the engine. |
| 5948 | * the engine is responsible to close |
| 5949 | */ |
| 5950 | for (i=0 ; i < num_all_fds ; i++) |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 5951 | fd_stop_both(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5952 | } |
| 5953 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5954 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5955 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5956 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5957 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5958 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5959 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5960 | } |
| 5961 | |
| 5962 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5963 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5964 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5965 | 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] | 5966 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5967 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5968 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5969 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5970 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5971 | if (!clean) |
| 5972 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5973 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5974 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5975 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5976 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5977 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5978 | ERR_clear_error(); |
| 5979 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5980 | } |
| 5981 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5982 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5983 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 5984 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 5985 | { |
| 5986 | struct ssl_sock_ctx *ctx; |
| 5987 | X509 *crt; |
| 5988 | |
| 5989 | if (!ssl_sock_is_ssl(conn)) |
| 5990 | return 0; |
| 5991 | |
| 5992 | ctx = conn->xprt_ctx; |
| 5993 | |
| 5994 | crt = SSL_get_certificate(ctx->ssl); |
| 5995 | if (!crt) |
| 5996 | return 0; |
| 5997 | |
| 5998 | return cert_get_pkey_algo(crt, out); |
| 5999 | } |
| 6000 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6001 | /* used for ppv2 cert signature (can be used for logging) */ |
| 6002 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 6003 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6004 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6005 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6006 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 6007 | X509 *crt; |
| 6008 | |
| 6009 | if (!ssl_sock_is_ssl(conn)) |
| 6010 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6011 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6012 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6013 | if (!crt) |
| 6014 | return NULL; |
| 6015 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6016 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6017 | } |
| 6018 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6019 | /* used for ppv2 authority */ |
| 6020 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6021 | { |
| 6022 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6023 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6024 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6025 | if (!ssl_sock_is_ssl(conn)) |
| 6026 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6027 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6028 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6029 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6030 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6031 | #endif |
| 6032 | } |
| 6033 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6034 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6035 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6036 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6037 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6038 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6039 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6040 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6041 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6042 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6043 | } |
| 6044 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6045 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6046 | const char *ssl_sock_get_proto_version(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 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6050 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6051 | return NULL; |
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 SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6054 | } |
| 6055 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6056 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6057 | { |
| 6058 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6059 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6060 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 6061 | if (!ssl_sock_is_ssl(conn)) |
| 6062 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6063 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6064 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6065 | #endif |
| 6066 | } |
| 6067 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6068 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6069 | * to disable SNI. |
| 6070 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6071 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6072 | { |
| 6073 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6074 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6075 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6076 | char *prev_name; |
| 6077 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6078 | if (!ssl_sock_is_ssl(conn)) |
| 6079 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6080 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6081 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6082 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6083 | * new connection will present a new SNI. As an optimization we could |
| 6084 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6085 | * server. |
| 6086 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6087 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6088 | if ((!prev_name && hostname) || |
| 6089 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6090 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6091 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6092 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6093 | #endif |
| 6094 | } |
| 6095 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6096 | /* Extract peer certificate's common name into the chunk dest |
| 6097 | * Returns |
| 6098 | * the len of the extracted common name |
| 6099 | * or 0 if no CN found in DN |
| 6100 | * or -1 on error case (i.e. no peer certificate) |
| 6101 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6102 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6103 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6104 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6105 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6106 | X509 *crt = NULL; |
| 6107 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6108 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6109 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6110 | .area = (char *)&find_cn, |
| 6111 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6112 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6113 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6114 | |
| 6115 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6116 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6117 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6118 | |
| 6119 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6120 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6121 | if (!crt) |
| 6122 | goto out; |
| 6123 | |
| 6124 | name = X509_get_subject_name(crt); |
| 6125 | if (!name) |
| 6126 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6127 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6128 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6129 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6130 | if (crt) |
| 6131 | X509_free(crt); |
| 6132 | |
| 6133 | return result; |
| 6134 | } |
| 6135 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6136 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6137 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6138 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6139 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6140 | X509 *crt = NULL; |
| 6141 | |
| 6142 | if (!ssl_sock_is_ssl(conn)) |
| 6143 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6144 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6145 | |
| 6146 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6147 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6148 | if (!crt) |
| 6149 | return 0; |
| 6150 | |
| 6151 | X509_free(crt); |
| 6152 | return 1; |
| 6153 | } |
| 6154 | |
| 6155 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6156 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6157 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6158 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6159 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6160 | if (!ssl_sock_is_ssl(conn)) |
| 6161 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6162 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6163 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6164 | } |
| 6165 | |
| 6166 | /* returns result from SSL verify */ |
| 6167 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6168 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6169 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6170 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6171 | if (!ssl_sock_is_ssl(conn)) |
| 6172 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6173 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6174 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6175 | } |
| 6176 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6177 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6178 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6179 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6180 | * freed by the caller. NPN is also checked if available since older versions |
| 6181 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6182 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6183 | 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] | 6184 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6185 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6186 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6187 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6188 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6189 | return 0; |
| 6190 | |
| 6191 | *str = NULL; |
| 6192 | |
| 6193 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6194 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6195 | if (*str) |
| 6196 | return 1; |
| 6197 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6198 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6199 | 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] | 6200 | if (*str) |
| 6201 | return 1; |
| 6202 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6203 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6204 | return 0; |
| 6205 | } |
| 6206 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6207 | /* "issuers-chain-path" load chain certificate in global */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6208 | 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] | 6209 | { |
| 6210 | X509 *ca; |
| 6211 | X509_NAME *name = NULL; |
| 6212 | ASN1_OCTET_STRING *skid = NULL; |
| 6213 | STACK_OF(X509) *chain = NULL; |
| 6214 | struct issuer_chain *issuer; |
| 6215 | struct eb64_node *node; |
| 6216 | char *path; |
| 6217 | u64 key; |
| 6218 | int ret = 0; |
| 6219 | |
| 6220 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 6221 | if (chain == NULL) { |
| 6222 | chain = sk_X509_new_null(); |
| 6223 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 6224 | name = X509_get_subject_name(ca); |
| 6225 | } |
| 6226 | if (!sk_X509_push(chain, ca)) { |
| 6227 | X509_free(ca); |
| 6228 | goto end; |
| 6229 | } |
| 6230 | } |
| 6231 | if (!chain) { |
| 6232 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 6233 | goto end; |
| 6234 | } |
| 6235 | if (!skid) { |
| 6236 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 6237 | goto end; |
| 6238 | } |
| 6239 | if (!name) { |
| 6240 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 6241 | goto end; |
| 6242 | } |
| 6243 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6244 | 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] | 6245 | issuer = container_of(node, typeof(*issuer), node); |
| 6246 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 6247 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 6248 | goto end; |
| 6249 | } |
| 6250 | } |
| 6251 | issuer = calloc(1, sizeof *issuer); |
| 6252 | path = strdup(fp); |
| 6253 | if (!issuer || !path) { |
| 6254 | free(issuer); |
| 6255 | free(path); |
| 6256 | goto end; |
| 6257 | } |
| 6258 | issuer->node.key = key; |
| 6259 | issuer->path = path; |
| 6260 | issuer->chain = chain; |
| 6261 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6262 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6263 | ret = 1; |
| 6264 | end: |
| 6265 | if (skid) |
| 6266 | ASN1_OCTET_STRING_free(skid); |
| 6267 | if (chain) |
| 6268 | sk_X509_pop_free(chain, X509_free); |
| 6269 | return ret; |
| 6270 | } |
| 6271 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 6272 | struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6273 | { |
| 6274 | AUTHORITY_KEYID *akid; |
| 6275 | struct issuer_chain *issuer = NULL; |
| 6276 | |
| 6277 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
William Lallemand | 829d071 | 2020-11-19 16:24:13 +0100 | [diff] [blame] | 6278 | if (akid && akid->keyid) { |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6279 | struct eb64_node *node; |
| 6280 | u64 hk; |
| 6281 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 6282 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 6283 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 6284 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 6285 | issuer = ti; |
| 6286 | break; |
| 6287 | } |
| 6288 | } |
| 6289 | AUTHORITY_KEYID_free(akid); |
| 6290 | } |
| 6291 | return issuer; |
| 6292 | } |
| 6293 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6294 | void ssl_free_global_issuers(void) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6295 | { |
| 6296 | struct eb64_node *node, *back; |
| 6297 | struct issuer_chain *issuer; |
| 6298 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6299 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6300 | while (node) { |
| 6301 | issuer = container_of(node, typeof(*issuer), node); |
| 6302 | back = eb64_next(node); |
| 6303 | eb64_delete(node); |
| 6304 | free(issuer->path); |
| 6305 | sk_X509_pop_free(issuer->chain, X509_free); |
| 6306 | free(issuer); |
| 6307 | node = back; |
| 6308 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6309 | } |
| 6310 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6311 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6312 | static int ssl_check_async_engine_count(void) { |
| 6313 | int err_code = 0; |
| 6314 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6315 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6316 | 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] | 6317 | err_code = ERR_ABORT; |
| 6318 | } |
| 6319 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6320 | } |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6321 | #endif |
| 6322 | |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6323 | /* "show fd" helper to dump ssl internals. Warning: the output buffer is often |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6324 | * the common trash! It returns non-zero if the connection entry looks suspicious. |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6325 | */ |
Willy Tarreau | 6cb467e | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 6326 | static int ssl_sock_show_fd(struct buffer *buf, const struct connection *conn, const void *ctx) |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6327 | { |
| 6328 | const struct ssl_sock_ctx *sctx = ctx; |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6329 | int ret = 0; |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6330 | |
| 6331 | if (!sctx) |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6332 | return ret; |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6333 | |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6334 | if (sctx->conn != conn) { |
| 6335 | chunk_appendf(&trash, " xctx.conn=%p(BOGUS)", sctx->conn); |
| 6336 | ret = 1; |
| 6337 | } |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6338 | chunk_appendf(&trash, " xctx.st=%d", sctx->xprt_st); |
| 6339 | |
| 6340 | if (sctx->xprt) { |
| 6341 | chunk_appendf(&trash, " .xprt=%s", sctx->xprt->name); |
| 6342 | if (sctx->xprt_ctx) |
| 6343 | chunk_appendf(&trash, " .xctx=%p", sctx->xprt_ctx); |
| 6344 | } |
| 6345 | |
| 6346 | chunk_appendf(&trash, " .wait.ev=%d", sctx->wait_event.events); |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6347 | |
| 6348 | /* as soon as a shutdown is reported the lower layer unregisters its |
| 6349 | * subscriber, so the situations below are transient and rare enough to |
| 6350 | * be reported as suspicious. In any case they shouldn't last. |
| 6351 | */ |
| 6352 | if ((sctx->wait_event.events & 1) && (conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_ERROR))) |
| 6353 | ret = 1; |
| 6354 | if ((sctx->wait_event.events & 2) && (conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR))) |
| 6355 | ret = 1; |
| 6356 | |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6357 | chunk_appendf(&trash, " .subs=%p", sctx->subs); |
| 6358 | if (sctx->subs) { |
| 6359 | chunk_appendf(&trash, "(ev=%d tl=%p", sctx->subs->events, sctx->subs->tasklet); |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6360 | if (sctx->subs->tasklet->calls >= 1000000) |
| 6361 | ret = 1; |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6362 | chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 6363 | sctx->subs->tasklet->calls, |
| 6364 | sctx->subs->tasklet->context); |
| 6365 | resolve_sym_name(&trash, NULL, sctx->subs->tasklet->process); |
| 6366 | chunk_appendf(&trash, ")"); |
| 6367 | } |
| 6368 | chunk_appendf(&trash, " .sent_early=%d", sctx->sent_early_data); |
| 6369 | chunk_appendf(&trash, " .early_in=%d", (int)sctx->early_buf.data); |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6370 | return ret; |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6371 | } |
| 6372 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6373 | /* This function is used with TLS ticket keys management. It permits to browse |
| 6374 | * each reference. The variable <getnext> must contain the current node, |
| 6375 | * <end> point to the root node. |
| 6376 | */ |
| 6377 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6378 | static inline |
| 6379 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 6380 | { |
| 6381 | struct tls_keys_ref *ref = getnext; |
| 6382 | |
| 6383 | while (1) { |
| 6384 | |
| 6385 | /* Get next list entry. */ |
| 6386 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 6387 | |
| 6388 | /* If the entry is the last of the list, return NULL. */ |
| 6389 | if (&ref->list == end) |
| 6390 | return NULL; |
| 6391 | |
| 6392 | return ref; |
| 6393 | } |
| 6394 | } |
| 6395 | |
| 6396 | static inline |
| 6397 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 6398 | { |
| 6399 | int id; |
| 6400 | char *error; |
| 6401 | |
| 6402 | /* If the reference starts by a '#', this is numeric id. */ |
| 6403 | if (reference[0] == '#') { |
| 6404 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 6405 | id = strtol(reference + 1, &error, 10); |
| 6406 | if (*error != '\0') |
| 6407 | return NULL; |
| 6408 | |
| 6409 | /* Perform the unique id lookup. */ |
| 6410 | return tlskeys_ref_lookupid(id); |
| 6411 | } |
| 6412 | |
| 6413 | /* Perform the string lookup. */ |
| 6414 | return tlskeys_ref_lookup(reference); |
| 6415 | } |
| 6416 | #endif |
| 6417 | |
| 6418 | |
| 6419 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6420 | |
| 6421 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 6422 | |
| 6423 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 6424 | return cli_io_handler_tlskeys_files(appctx); |
| 6425 | } |
| 6426 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6427 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 6428 | * (next index to be dumped), and cli.p0 (next key reference). |
| 6429 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6430 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 6431 | |
| 6432 | struct stream_interface *si = appctx->owner; |
| 6433 | |
| 6434 | switch (appctx->st2) { |
| 6435 | case STAT_ST_INIT: |
| 6436 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 6437 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6438 | * later and restart at the state "STAT_ST_INIT". |
| 6439 | */ |
| 6440 | chunk_reset(&trash); |
| 6441 | |
| 6442 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 6443 | chunk_appendf(&trash, "# id secret\n"); |
| 6444 | else |
| 6445 | chunk_appendf(&trash, "# id (file)\n"); |
| 6446 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6447 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6448 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6449 | return 0; |
| 6450 | } |
| 6451 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6452 | /* Now, we start the browsing of the references lists. |
| 6453 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 6454 | * available field of this pointer is <list>. It is used with the function |
| 6455 | * tlskeys_list_get_next() for retruning the first available entry |
| 6456 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6457 | if (appctx->ctx.cli.p0 == NULL) { |
| 6458 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 6459 | 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] | 6460 | } |
| 6461 | |
| 6462 | appctx->st2 = STAT_ST_LIST; |
| 6463 | /* fall through */ |
| 6464 | |
| 6465 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6466 | while (appctx->ctx.cli.p0) { |
| 6467 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6468 | |
| 6469 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6470 | 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] | 6471 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6472 | |
| 6473 | if (appctx->ctx.cli.i1 == 0) |
| 6474 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 6475 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6476 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6477 | int head; |
| 6478 | |
| 6479 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 6480 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6481 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6482 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6483 | |
| 6484 | chunk_reset(t2); |
| 6485 | /* 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] | 6486 | if (ref->key_size_bits == 128) { |
| 6487 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 6488 | sizeof(struct tls_sess_key_128), |
| 6489 | t2->area, t2->size); |
| 6490 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 6491 | t2->area); |
| 6492 | } |
| 6493 | else if (ref->key_size_bits == 256) { |
| 6494 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 6495 | sizeof(struct tls_sess_key_256), |
| 6496 | t2->area, t2->size); |
| 6497 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 6498 | t2->area); |
| 6499 | } |
| 6500 | else { |
| 6501 | /* This case should never happen */ |
| 6502 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 6503 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6504 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6505 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6506 | /* let's try again later from this stream. We add ourselves into |
| 6507 | * this stream's users so that it can remove us upon termination. |
| 6508 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6509 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6510 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6511 | return 0; |
| 6512 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6513 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6514 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6515 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6516 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6517 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6518 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6519 | /* let's try again later from this stream. We add ourselves into |
| 6520 | * this stream's users so that it can remove us upon termination. |
| 6521 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6522 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6523 | return 0; |
| 6524 | } |
| 6525 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6526 | 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] | 6527 | break; |
| 6528 | |
| 6529 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6530 | 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] | 6531 | } |
| 6532 | |
| 6533 | appctx->st2 = STAT_ST_FIN; |
| 6534 | /* fall through */ |
| 6535 | |
| 6536 | default: |
| 6537 | appctx->st2 = STAT_ST_FIN; |
| 6538 | return 1; |
| 6539 | } |
| 6540 | return 0; |
| 6541 | } |
| 6542 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6543 | /* 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] | 6544 | 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] | 6545 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6546 | /* no parameter, shows only file list */ |
| 6547 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6548 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6549 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6550 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6551 | } |
| 6552 | |
| 6553 | if (args[2][0] == '*') { |
| 6554 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6555 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6556 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6557 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6558 | if (!appctx->ctx.cli.p0) |
| 6559 | 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] | 6560 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6561 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6562 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6563 | } |
| 6564 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6565 | 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] | 6566 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6567 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6568 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6569 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6570 | /* 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] | 6571 | if (!*args[3] || !*args[4]) |
| 6572 | 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] | 6573 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6574 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6575 | if (!ref) |
| 6576 | 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] | 6577 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6578 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6579 | if (ret < 0) |
| 6580 | 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] | 6581 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6582 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6583 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 6584 | 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] | 6585 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6586 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6587 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6588 | #endif |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 6589 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6590 | 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] | 6591 | { |
| 6592 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 6593 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6594 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 6595 | |
| 6596 | if (!payload) |
| 6597 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6598 | |
| 6599 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6600 | if (!*payload) |
| 6601 | 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] | 6602 | |
| 6603 | /* remove \r and \n from the payload */ |
| 6604 | for (i = 0, j = 0; payload[i]; i++) { |
| 6605 | if (payload[i] == '\r' || payload[i] == '\n') |
| 6606 | continue; |
| 6607 | payload[j++] = payload[i]; |
| 6608 | } |
| 6609 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6610 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6611 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6612 | if (ret < 0) |
| 6613 | 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] | 6614 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6615 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6616 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6617 | if (err) |
| 6618 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 6619 | else |
| 6620 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6621 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6622 | |
| 6623 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6624 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6625 | 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] | 6626 | #endif |
| 6627 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 6628 | } |
| 6629 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6630 | /* register cli keywords */ |
| 6631 | static struct cli_kw_list cli_kws = {{ },{ |
| 6632 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6633 | { { "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] | 6634 | { { "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] | 6635 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6636 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6637 | { { NULL }, NULL, NULL, NULL } |
| 6638 | }}; |
| 6639 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6640 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6641 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6642 | /* transport-layer operations for SSL sockets */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6643 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6644 | .snd_buf = ssl_sock_from_buf, |
| 6645 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6646 | .subscribe = ssl_subscribe, |
| 6647 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6648 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6649 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6650 | .rcv_pipe = NULL, |
| 6651 | .snd_pipe = NULL, |
| 6652 | .shutr = NULL, |
| 6653 | .shutw = ssl_sock_shutw, |
| 6654 | .close = ssl_sock_close, |
| 6655 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6656 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6657 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 6658 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 6659 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6660 | .get_alpn = ssl_sock_get_alpn, |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6661 | .takeover = ssl_takeover, |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6662 | .set_idle = ssl_set_idle, |
| 6663 | .set_used = ssl_set_used, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 6664 | .name = "SSL", |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6665 | .show_fd = ssl_sock_show_fd, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6666 | }; |
| 6667 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6668 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 6669 | struct session *sess, struct stream *s, int flags) |
| 6670 | { |
| 6671 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6672 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6673 | |
| 6674 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6675 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6676 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6677 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6678 | 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] | 6679 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6680 | s->req.flags |= CF_READ_NULL; |
| 6681 | return ACT_RET_YIELD; |
| 6682 | } |
| 6683 | } |
| 6684 | return (ACT_RET_CONT); |
| 6685 | } |
| 6686 | |
| 6687 | 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) |
| 6688 | { |
| 6689 | rule->action_ptr = ssl_action_wait_for_hs; |
| 6690 | |
| 6691 | return ACT_RET_PRS_OK; |
| 6692 | } |
| 6693 | |
| 6694 | static struct action_kw_list http_req_actions = {ILH, { |
| 6695 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 6696 | { /* END */ } |
| 6697 | }}; |
| 6698 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6699 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 6700 | |
Ilya Shipitsin | 49bab75 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 6701 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6702 | |
| 6703 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6704 | { |
| 6705 | if (ptr) { |
| 6706 | chunk_destroy(ptr); |
| 6707 | free(ptr); |
| 6708 | } |
| 6709 | } |
| 6710 | |
| 6711 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 6712 | |
| 6713 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 6714 | static void ssl_sock_ocsp_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6715 | { |
| 6716 | struct ocsp_cbk_arg *ocsp_arg; |
| 6717 | |
| 6718 | if (ptr) { |
| 6719 | ocsp_arg = ptr; |
| 6720 | |
| 6721 | if (ocsp_arg->is_single) { |
| 6722 | ssl_sock_free_ocsp(ocsp_arg->s_ocsp); |
| 6723 | ocsp_arg->s_ocsp = NULL; |
| 6724 | } else { |
| 6725 | int i; |
| 6726 | |
| 6727 | for (i = 0; i < SSL_SOCK_NUM_KEYTYPES; i++) { |
| 6728 | ssl_sock_free_ocsp(ocsp_arg->m_ocsp[i]); |
| 6729 | ocsp_arg->m_ocsp[i] = NULL; |
| 6730 | } |
| 6731 | } |
| 6732 | free(ocsp_arg); |
| 6733 | } |
| 6734 | } |
| 6735 | #endif |
| 6736 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6737 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6738 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6739 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6740 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 6741 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 6742 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 6743 | static void ssl_sock_keylog_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6744 | { |
| 6745 | struct ssl_keylog *keylog; |
| 6746 | |
| 6747 | if (!ptr) |
| 6748 | return; |
| 6749 | |
| 6750 | keylog = ptr; |
| 6751 | |
| 6752 | pool_free(pool_head_ssl_keylog_str, keylog->client_random); |
| 6753 | pool_free(pool_head_ssl_keylog_str, keylog->client_early_traffic_secret); |
| 6754 | pool_free(pool_head_ssl_keylog_str, keylog->client_handshake_traffic_secret); |
| 6755 | pool_free(pool_head_ssl_keylog_str, keylog->server_handshake_traffic_secret); |
| 6756 | pool_free(pool_head_ssl_keylog_str, keylog->client_traffic_secret_0); |
| 6757 | pool_free(pool_head_ssl_keylog_str, keylog->server_traffic_secret_0); |
| 6758 | pool_free(pool_head_ssl_keylog_str, keylog->exporter_secret); |
| 6759 | pool_free(pool_head_ssl_keylog_str, keylog->early_exporter_secret); |
| 6760 | |
| 6761 | pool_free(pool_head_ssl_keylog, ptr); |
| 6762 | } |
| 6763 | #endif |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6764 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6765 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6766 | static void __ssl_sock_init(void) |
| 6767 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6768 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6769 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6770 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6771 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6772 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6773 | if (global_ssl.listen_default_ciphers) |
| 6774 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 6775 | if (global_ssl.connect_default_ciphers) |
| 6776 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 6777 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6778 | if (global_ssl.listen_default_ciphersuites) |
| 6779 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 6780 | if (global_ssl.connect_default_ciphersuites) |
| 6781 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 6782 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 6783 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 6784 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 6785 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6786 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6787 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6788 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6789 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6790 | n = sk_SSL_COMP_num(cm); |
| 6791 | while (n--) { |
| 6792 | (void) sk_SSL_COMP_pop(cm); |
| 6793 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6794 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6795 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6796 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6797 | ssl_locking_init(); |
| 6798 | #endif |
Ilya Shipitsin | 49bab75 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 6799 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6800 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 6801 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 6802 | |
| 6803 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 6804 | ocsp_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_ocsp_free_func); |
| 6805 | #endif |
| 6806 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 6807 | 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] | 6808 | ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 6809 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 6810 | ssl_keylog_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_keylog_free_func); |
| 6811 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6812 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6813 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6814 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6815 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 6816 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6817 | hap_register_post_check(tlskeys_finalize_config); |
| 6818 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6819 | |
| 6820 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 6821 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 6822 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6823 | hap_register_post_deinit(ssl_free_global_issuers); |
| 6824 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6825 | #ifndef OPENSSL_NO_DH |
| 6826 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 6827 | hap_register_post_deinit(ssl_free_dh); |
| 6828 | #endif |
| 6829 | #ifndef OPENSSL_NO_ENGINE |
| 6830 | hap_register_post_deinit(ssl_free_engines); |
| 6831 | #endif |
| 6832 | /* Load SSL string for the verbose & debug mode. */ |
| 6833 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6834 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 6835 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 6836 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 6837 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 6838 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 6839 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 6840 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 6841 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 6842 | |
| 6843 | HA_SPIN_INIT(&ckch_lock); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 6844 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 6845 | /* Try to register dedicated SSL/TLS protocol message callbacks for |
| 6846 | * heartbleed attack (CVE-2014-0160) and clienthello. |
| 6847 | */ |
| 6848 | hap_register_post_check(ssl_sock_register_msg_callbacks); |
| 6849 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 6850 | /* Try to free all callbacks that were registered by using |
| 6851 | * ssl_sock_register_msg_callback(). |
| 6852 | */ |
| 6853 | hap_register_post_deinit(ssl_sock_unregister_msg_callbacks); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6854 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 6855 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6856 | /* Compute and register the version string */ |
| 6857 | static void ssl_register_build_options() |
| 6858 | { |
| 6859 | char *ptr = NULL; |
| 6860 | int i; |
| 6861 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6862 | memprintf(&ptr, "Built with OpenSSL version : " |
| 6863 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 6864 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6865 | #else /* OPENSSL_IS_BORINGSSL */ |
| 6866 | OPENSSL_VERSION_TEXT |
| 6867 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6868 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 6869 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6870 | #endif |
| 6871 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 6872 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6873 | "no (library version too old)" |
| 6874 | #elif defined(OPENSSL_NO_TLSEXT) |
| 6875 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 6876 | #else |
| 6877 | "yes" |
| 6878 | #endif |
| 6879 | "", ptr); |
| 6880 | |
| 6881 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 6882 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 6883 | "yes" |
| 6884 | #else |
| 6885 | #ifdef OPENSSL_NO_TLSEXT |
| 6886 | "no (because of OPENSSL_NO_TLSEXT)" |
| 6887 | #else |
| 6888 | "no (version might be too old, 0.9.8f min needed)" |
| 6889 | #endif |
| 6890 | #endif |
| 6891 | "", ptr); |
| 6892 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 6893 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 6894 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 6895 | if (methodVersions[i].option) |
| 6896 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 6897 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6898 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6899 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6900 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6901 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 6902 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6903 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6904 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6905 | void ssl_free_engines(void) { |
| 6906 | struct ssl_engine_list *wl, *wlb; |
| 6907 | /* free up engine list */ |
| 6908 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 6909 | ENGINE_finish(wl->e); |
| 6910 | ENGINE_free(wl->e); |
| 6911 | LIST_DEL(&wl->list); |
| 6912 | free(wl); |
| 6913 | } |
| 6914 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6915 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6916 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6917 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6918 | void ssl_free_dh(void) { |
| 6919 | if (local_dh_1024) { |
| 6920 | DH_free(local_dh_1024); |
| 6921 | local_dh_1024 = NULL; |
| 6922 | } |
| 6923 | if (local_dh_2048) { |
| 6924 | DH_free(local_dh_2048); |
| 6925 | local_dh_2048 = NULL; |
| 6926 | } |
| 6927 | if (local_dh_4096) { |
| 6928 | DH_free(local_dh_4096); |
| 6929 | local_dh_4096 = NULL; |
| 6930 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 6931 | if (global_dh) { |
| 6932 | DH_free(global_dh); |
| 6933 | global_dh = NULL; |
| 6934 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6935 | } |
| 6936 | #endif |
| 6937 | |
| 6938 | __attribute__((destructor)) |
| 6939 | static void __ssl_sock_deinit(void) |
| 6940 | { |
| 6941 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6942 | if (ssl_ctx_lru_tree) { |
| 6943 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 6944 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6945 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6946 | #endif |
| 6947 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6948 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6949 | ERR_remove_state(0); |
| 6950 | ERR_free_strings(); |
| 6951 | |
| 6952 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6953 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6954 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6955 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6956 | CRYPTO_cleanup_all_ex_data(); |
| 6957 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6958 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6959 | } |
| 6960 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6961 | /* |
| 6962 | * Local variables: |
| 6963 | * c-indent-level: 8 |
| 6964 | * c-basic-offset: 8 |
| 6965 | * End: |
| 6966 | */ |