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 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 303 | int ssl_store_load_locations_file(char *path) |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 304 | { |
| 305 | if (ssl_store_get0_locations_file(path) == NULL) { |
| 306 | struct cafile_entry *ca_e; |
| 307 | X509_STORE *store = X509_STORE_new(); |
| 308 | if (X509_STORE_load_locations(store, path, NULL)) { |
| 309 | int pathlen; |
| 310 | pathlen = strlen(path); |
| 311 | ca_e = calloc(1, sizeof(*ca_e) + pathlen + 1); |
| 312 | if (ca_e) { |
| 313 | memcpy(ca_e->path, path, pathlen + 1); |
| 314 | ca_e->ca_store = store; |
| 315 | ebst_insert(&cafile_tree, &ca_e->node); |
| 316 | return 1; |
| 317 | } |
| 318 | } |
| 319 | X509_STORE_free(store); |
| 320 | return 0; |
| 321 | } |
| 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 326 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 327 | { |
| 328 | X509_STORE *store; |
| 329 | store = ssl_store_get0_locations_file(path); |
| 330 | if (store_ctx && store) { |
| 331 | int i; |
| 332 | X509_OBJECT *obj; |
| 333 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 334 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 335 | obj = sk_X509_OBJECT_value(objs, i); |
| 336 | switch (X509_OBJECT_get_type(obj)) { |
| 337 | case X509_LU_X509: |
| 338 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 339 | break; |
| 340 | case X509_LU_CRL: |
| 341 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 342 | break; |
| 343 | default: |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | return 1; |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 352 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 353 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 354 | { |
| 355 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 356 | return ssl_set_cert_crl_file(store_ctx, path); |
| 357 | } |
| 358 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 359 | /* |
| 360 | Extract CA_list from CA_file already in tree. |
| 361 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 362 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 363 | */ |
| 364 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 365 | { |
| 366 | struct ebmb_node *eb; |
| 367 | struct cafile_entry *ca_e; |
| 368 | |
| 369 | eb = ebst_lookup(&cafile_tree, path); |
| 370 | if (!eb) |
| 371 | return NULL; |
| 372 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 373 | |
| 374 | if (ca_e->ca_list == NULL) { |
| 375 | int i; |
| 376 | unsigned long key; |
| 377 | struct eb_root ca_name_tree = EB_ROOT; |
| 378 | struct eb64_node *node, *back; |
| 379 | struct { |
| 380 | struct eb64_node node; |
| 381 | X509_NAME *xname; |
| 382 | } *ca_name; |
| 383 | STACK_OF(X509_OBJECT) *objs; |
| 384 | STACK_OF(X509_NAME) *skn; |
| 385 | X509 *x; |
| 386 | X509_NAME *xn; |
| 387 | |
| 388 | skn = sk_X509_NAME_new_null(); |
| 389 | /* take x509 from cafile_tree */ |
| 390 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 391 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 392 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 393 | if (!x) |
| 394 | continue; |
| 395 | xn = X509_get_subject_name(x); |
| 396 | if (!xn) |
| 397 | continue; |
| 398 | /* Check for duplicates. */ |
| 399 | key = X509_NAME_hash(xn); |
| 400 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 401 | node && ca_name == NULL; |
| 402 | node = eb64_next(node)) { |
| 403 | ca_name = container_of(node, typeof(*ca_name), node); |
| 404 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 405 | ca_name = NULL; |
| 406 | } |
| 407 | /* find a duplicate */ |
| 408 | if (ca_name) |
| 409 | continue; |
| 410 | ca_name = calloc(1, sizeof *ca_name); |
| 411 | xn = X509_NAME_dup(xn); |
| 412 | if (!ca_name || |
| 413 | !xn || |
| 414 | !sk_X509_NAME_push(skn, xn)) { |
| 415 | free(ca_name); |
| 416 | X509_NAME_free(xn); |
| 417 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 418 | sk_X509_NAME_free(skn); |
| 419 | skn = NULL; |
| 420 | break; |
| 421 | } |
| 422 | ca_name->node.key = key; |
| 423 | ca_name->xname = xn; |
| 424 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 425 | } |
| 426 | ca_e->ca_list = skn; |
| 427 | /* remove temporary ca_name tree */ |
| 428 | node = eb64_first(&ca_name_tree); |
| 429 | while (node) { |
| 430 | ca_name = container_of(node, typeof(*ca_name), node); |
| 431 | back = eb64_next(node); |
| 432 | eb64_delete(node); |
| 433 | free(ca_name); |
| 434 | node = back; |
| 435 | } |
| 436 | } |
| 437 | return ca_e->ca_list; |
| 438 | } |
| 439 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 440 | struct pool_head *pool_head_ssl_capture = NULL; |
William Lallemand | 15e1694 | 2020-05-15 00:25:08 +0200 | [diff] [blame] | 441 | int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 442 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 443 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 444 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 445 | int ssl_keylog_index = -1; |
| 446 | struct pool_head *pool_head_ssl_keylog = NULL; |
| 447 | struct pool_head *pool_head_ssl_keylog_str = NULL; |
| 448 | #endif |
| 449 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 450 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 451 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 452 | #endif |
| 453 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 454 | #ifndef OPENSSL_NO_ENGINE |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 455 | unsigned int openssl_engines_initialized; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 456 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 457 | struct ssl_engine_list { |
| 458 | struct list list; |
| 459 | ENGINE *e; |
| 460 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 461 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 462 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 463 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 464 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 465 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 466 | static DH *local_dh_1024 = NULL; |
| 467 | static DH *local_dh_2048 = NULL; |
| 468 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 469 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 470 | #endif /* OPENSSL_NO_DH */ |
| 471 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 472 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 473 | /* X509V3 Extensions that will be added on generated certificates */ |
| 474 | #define X509V3_EXT_SIZE 5 |
| 475 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 476 | "basicConstraints", |
| 477 | "nsComment", |
| 478 | "subjectKeyIdentifier", |
| 479 | "authorityKeyIdentifier", |
| 480 | "keyUsage", |
| 481 | }; |
| 482 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 483 | "CA:FALSE", |
| 484 | "\"OpenSSL Generated Certificate\"", |
| 485 | "hash", |
| 486 | "keyid,issuer:always", |
| 487 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 488 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 489 | /* LRU cache to store generated certificate */ |
| 490 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 491 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 492 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 493 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 494 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 495 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 496 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 497 | /* The order here matters for picking a default context, |
| 498 | * keep the most common keytype at the bottom of the list |
| 499 | */ |
| 500 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 501 | "dsa", |
| 502 | "ecdsa", |
| 503 | "rsa" |
| 504 | }; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 505 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 506 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 507 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 508 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 509 | /* Dedicated callback functions for heartbeat and clienthello. |
| 510 | */ |
| 511 | #ifdef TLS1_RT_HEARTBEAT |
| 512 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 513 | int content_type, const void *buf, size_t len, |
| 514 | SSL *ssl); |
| 515 | #endif |
| 516 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 517 | int content_type, const void *buf, size_t len, |
| 518 | SSL *ssl); |
| 519 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 520 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 521 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 522 | int content_type, const void *buf, size_t len, |
| 523 | SSL *ssl); |
| 524 | #endif |
| 525 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 526 | /* List head of all registered SSL/TLS protocol message callbacks. */ |
| 527 | struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks); |
| 528 | |
| 529 | /* Registers the function <func> in order to be called on SSL/TLS protocol |
| 530 | * message processing. It will return 0 if the function <func> is not set |
| 531 | * or if it fails to allocate memory. |
| 532 | */ |
| 533 | int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func) |
| 534 | { |
| 535 | struct ssl_sock_msg_callback *cbk; |
| 536 | |
| 537 | if (!func) |
| 538 | return 0; |
| 539 | |
| 540 | cbk = calloc(1, sizeof(*cbk)); |
| 541 | if (!cbk) { |
| 542 | ha_alert("out of memory in ssl_sock_register_msg_callback().\n"); |
| 543 | return 0; |
| 544 | } |
| 545 | |
| 546 | cbk->func = func; |
| 547 | |
| 548 | LIST_ADDQ(&ssl_sock_msg_callbacks, &cbk->list); |
| 549 | |
| 550 | return 1; |
| 551 | } |
| 552 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 553 | /* Used to register dedicated SSL/TLS protocol message callbacks. |
| 554 | */ |
| 555 | static int ssl_sock_register_msg_callbacks(void) |
| 556 | { |
| 557 | #ifdef TLS1_RT_HEARTBEAT |
| 558 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat)) |
| 559 | return ERR_ABORT; |
| 560 | #endif |
| 561 | if (global_ssl.capture_cipherlist > 0) { |
| 562 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello)) |
| 563 | return ERR_ABORT; |
| 564 | } |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 565 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 566 | if (global_ssl.keylog > 0) { |
| 567 | if (!ssl_sock_register_msg_callback(ssl_init_keylog)) |
| 568 | return ERR_ABORT; |
| 569 | } |
| 570 | #endif |
| 571 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 572 | return 0; |
| 573 | } |
| 574 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 575 | /* Used to free all SSL/TLS protocol message callbacks that were |
| 576 | * registered by using ssl_sock_register_msg_callback(). |
| 577 | */ |
| 578 | static void ssl_sock_unregister_msg_callbacks(void) |
| 579 | { |
| 580 | struct ssl_sock_msg_callback *cbk, *cbkback; |
| 581 | |
| 582 | list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) { |
| 583 | LIST_DEL(&cbk->list); |
| 584 | free(cbk); |
| 585 | } |
| 586 | } |
| 587 | |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 588 | SSL *ssl_sock_get_ssl_object(struct connection *conn) |
| 589 | { |
| 590 | if (!ssl_sock_is_ssl(conn)) |
| 591 | return NULL; |
| 592 | |
| 593 | return ((struct ssl_sock_ctx *)(conn->xprt_ctx))->ssl; |
| 594 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 595 | /* |
| 596 | * This function gives the detail of the SSL error. It is used only |
| 597 | * if the debug mode and the verbose mode are activated. It dump all |
| 598 | * the SSL error until the stack was empty. |
| 599 | */ |
| 600 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 601 | { |
| 602 | unsigned long ret; |
| 603 | |
| 604 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 605 | while(1) { |
| 606 | ret = ERR_get_error(); |
| 607 | if (ret == 0) |
| 608 | return; |
Willy Tarreau | 07451ac | 2021-03-02 19:32:39 +0100 | [diff] [blame] | 609 | fprintf(stderr, "fd[%#x] OpenSSL error[0x%lx] %s: %s\n", |
| 610 | conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 611 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 612 | } |
| 613 | } |
| 614 | } |
| 615 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 616 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 617 | #ifndef OPENSSL_NO_ENGINE |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 618 | 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] | 619 | { |
| 620 | int err_code = ERR_ABORT; |
| 621 | ENGINE *engine; |
| 622 | struct ssl_engine_list *el; |
| 623 | |
| 624 | /* grab the structural reference to the engine */ |
| 625 | engine = ENGINE_by_id(engine_id); |
| 626 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 627 | 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] | 628 | goto fail_get; |
| 629 | } |
| 630 | |
| 631 | if (!ENGINE_init(engine)) { |
| 632 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 633 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 634 | goto fail_init; |
| 635 | } |
| 636 | |
| 637 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 638 | 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] | 639 | goto fail_set_method; |
| 640 | } |
| 641 | |
| 642 | el = calloc(1, sizeof(*el)); |
| 643 | el->e = engine; |
| 644 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 645 | nb_engines++; |
| 646 | if (global_ssl.async) |
| 647 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 648 | return 0; |
| 649 | |
| 650 | fail_set_method: |
| 651 | /* release the functional reference from ENGINE_init() */ |
| 652 | ENGINE_finish(engine); |
| 653 | |
| 654 | fail_init: |
| 655 | /* release the structural reference from ENGINE_by_id() */ |
| 656 | ENGINE_free(engine); |
| 657 | |
| 658 | fail_get: |
| 659 | return err_code; |
| 660 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 661 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 662 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 663 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 664 | /* |
| 665 | * openssl async fd handler |
| 666 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 667 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 668 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 669 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 670 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 671 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 672 | * to poll this fd until it is requested |
| 673 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 674 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 675 | fd_cant_recv(fd); |
| 676 | |
| 677 | /* crypto engine is available, let's notify the associated |
| 678 | * connection that it can pursue its processing. |
| 679 | */ |
Olivier Houchard | a459826 | 2020-09-15 22:16:02 +0200 | [diff] [blame] | 680 | tasklet_wakeup(ctx->wait_event.tasklet); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 681 | } |
| 682 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 683 | /* |
| 684 | * openssl async delayed SSL_free handler |
| 685 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 686 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 687 | { |
| 688 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 689 | OSSL_ASYNC_FD all_fd[32]; |
| 690 | size_t num_all_fds = 0; |
| 691 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 692 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 693 | /* We suppose that the async job for a same SSL * |
| 694 | * are serialized. So if we are awake it is |
| 695 | * because the running job has just finished |
| 696 | * and we can remove all async fds safely |
| 697 | */ |
| 698 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 699 | if (num_all_fds > 32) { |
| 700 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 701 | return; |
| 702 | } |
| 703 | |
| 704 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 705 | for (i=0 ; i < num_all_fds ; i++) |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 706 | fd_stop_both(all_fd[i]); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 707 | |
| 708 | /* 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] | 709 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 710 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 711 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 712 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 713 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 714 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 715 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 716 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 717 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 718 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 719 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 720 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 721 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 722 | size_t num_add_fds = 0; |
| 723 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 724 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 725 | |
| 726 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 727 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 728 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 729 | 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] | 730 | return; |
| 731 | } |
| 732 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 733 | 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] | 734 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 735 | /* We remove unused fds from the fdtab */ |
| 736 | for (i=0 ; i < num_del_fds ; i++) |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 737 | fd_stop_both(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 738 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 739 | /* We add new fds to the fdtab */ |
| 740 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 741 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 742 | } |
| 743 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 744 | num_add_fds = 0; |
| 745 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 746 | if (num_add_fds > 32) { |
| 747 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 748 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 749 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 750 | |
| 751 | /* We activate the polling for all known async fds */ |
| 752 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 753 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 754 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 755 | /* To ensure that the fd cache won't be used |
| 756 | * We'll prefer to catch a real RD event |
| 757 | * because handling an EAGAIN on this fd will |
| 758 | * result in a context switch and also |
| 759 | * some engines uses a fd in blocking mode. |
| 760 | */ |
| 761 | fd_cant_recv(add_fd[i]); |
| 762 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 763 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 764 | } |
| 765 | #endif |
| 766 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 767 | #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] | 768 | /* |
| 769 | * This function returns the number of seconds elapsed |
| 770 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 771 | * date presented un ASN1_GENERALIZEDTIME. |
| 772 | * |
| 773 | * In parsing error case, it returns -1. |
| 774 | */ |
| 775 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 776 | { |
| 777 | long epoch; |
| 778 | char *p, *end; |
| 779 | const unsigned short month_offset[12] = { |
| 780 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 781 | }; |
| 782 | int year, month; |
| 783 | |
| 784 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 785 | |
| 786 | p = (char *)d->data; |
| 787 | end = p + d->length; |
| 788 | |
| 789 | if (end - p < 4) return -1; |
| 790 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 791 | p += 4; |
| 792 | if (end - p < 2) return -1; |
| 793 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 794 | if (month < 1 || month > 12) return -1; |
| 795 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 796 | We consider leap years and the current month (<marsh or not) */ |
| 797 | epoch = ( ((year - 1970) * 365) |
| 798 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 799 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 800 | + month_offset[month-1] |
| 801 | ) * 24 * 60 * 60; |
| 802 | p += 2; |
| 803 | if (end - p < 2) return -1; |
| 804 | /* Add the number of seconds of completed days of current month */ |
| 805 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 806 | p += 2; |
| 807 | if (end - p < 2) return -1; |
| 808 | /* Add the completed hours of the current day */ |
| 809 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 810 | p += 2; |
| 811 | if (end - p < 2) return -1; |
| 812 | /* Add the completed minutes of the current hour */ |
| 813 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 814 | p += 2; |
| 815 | if (p == end) return -1; |
| 816 | /* Test if there is available seconds */ |
| 817 | if (p[0] < '0' || p[0] > '9') |
| 818 | goto nosec; |
| 819 | if (end - p < 2) return -1; |
| 820 | /* Add the seconds of the current minute */ |
| 821 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 822 | p += 2; |
| 823 | if (p == end) return -1; |
| 824 | /* Ignore seconds float part if present */ |
| 825 | if (p[0] == '.') { |
| 826 | do { |
| 827 | if (++p == end) return -1; |
| 828 | } while (p[0] >= '0' && p[0] <= '9'); |
| 829 | } |
| 830 | |
| 831 | nosec: |
| 832 | if (p[0] == 'Z') { |
| 833 | if (end - p != 1) return -1; |
| 834 | return epoch; |
| 835 | } |
| 836 | else if (p[0] == '+') { |
| 837 | if (end - p != 5) return -1; |
| 838 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 839 | 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] | 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 | |
| 847 | return -1; |
| 848 | } |
| 849 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 850 | /* |
| 851 | * struct alignment works here such that the key.key is the same as key_data |
| 852 | * Do not change the placement of key_data |
| 853 | */ |
| 854 | struct certificate_ocsp { |
| 855 | struct ebmb_node key; |
| 856 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 857 | struct buffer response; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 858 | int refcount; |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 859 | long expire; |
| 860 | }; |
| 861 | |
| 862 | struct ocsp_cbk_arg { |
| 863 | int is_single; |
| 864 | int single_kt; |
| 865 | union { |
| 866 | struct certificate_ocsp *s_ocsp; |
| 867 | /* |
| 868 | * m_ocsp will have multiple entries dependent on key type |
| 869 | * Entry 0 - DSA |
| 870 | * Entry 1 - ECDSA |
| 871 | * Entry 2 - RSA |
| 872 | */ |
| 873 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 874 | }; |
| 875 | }; |
| 876 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 877 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 878 | |
| 879 | /* This function starts to check if the OCSP response (in DER format) contained |
| 880 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 881 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 882 | * contained in the OCSP Response and exits on error if no match. |
| 883 | * If it's a valid OCSP Response: |
| 884 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 885 | * pointed by 'ocsp'. |
| 886 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 887 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 888 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 889 | * already present in the container, it will be overwritten. |
| 890 | * |
| 891 | * Note: OCSP response containing more than one OCSP Single response is not |
| 892 | * considered valid. |
| 893 | * |
| 894 | * Returns 0 on success, 1 in error case. |
| 895 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 896 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 897 | struct certificate_ocsp *ocsp, |
| 898 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 899 | { |
| 900 | OCSP_RESPONSE *resp; |
| 901 | OCSP_BASICRESP *bs = NULL; |
| 902 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 903 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 904 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 905 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 906 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 907 | int reason; |
| 908 | int ret = 1; |
| 909 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 910 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 911 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 912 | if (!resp) { |
| 913 | memprintf(err, "Unable to parse OCSP response"); |
| 914 | goto out; |
| 915 | } |
| 916 | |
| 917 | rc = OCSP_response_status(resp); |
| 918 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 919 | memprintf(err, "OCSP response status not successful"); |
| 920 | goto out; |
| 921 | } |
| 922 | |
| 923 | bs = OCSP_response_get1_basic(resp); |
| 924 | if (!bs) { |
| 925 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 926 | goto out; |
| 927 | } |
| 928 | |
| 929 | count_sr = OCSP_resp_count(bs); |
| 930 | if (count_sr > 1) { |
| 931 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 932 | goto out; |
| 933 | } |
| 934 | |
| 935 | sr = OCSP_resp_get0(bs, 0); |
| 936 | if (!sr) { |
| 937 | memprintf(err, "Failed to get OCSP single response"); |
| 938 | goto out; |
| 939 | } |
| 940 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 941 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 942 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 943 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 944 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 945 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 946 | goto out; |
| 947 | } |
| 948 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 949 | if (!nextupd) { |
| 950 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 951 | goto out; |
| 952 | } |
| 953 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 954 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 955 | if (!rc) { |
| 956 | memprintf(err, "OCSP single response: no longer valid."); |
| 957 | goto out; |
| 958 | } |
| 959 | |
| 960 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 961 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 962 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 963 | goto out; |
| 964 | } |
| 965 | } |
| 966 | |
| 967 | if (!ocsp) { |
| 968 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 969 | unsigned char *p; |
| 970 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 971 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 972 | if (!rc) { |
| 973 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 974 | goto out; |
| 975 | } |
| 976 | |
| 977 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 978 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 979 | goto out; |
| 980 | } |
| 981 | |
| 982 | p = key; |
| 983 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 984 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 985 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 986 | if (!ocsp) { |
| 987 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 988 | goto out; |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | /* According to comments on "chunk_dup", the |
| 993 | previous chunk buffer will be freed */ |
| 994 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 995 | memprintf(err, "OCSP response: Memory allocation error"); |
| 996 | goto out; |
| 997 | } |
| 998 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 999 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 1000 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1001 | ret = 0; |
| 1002 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 1003 | ERR_clear_error(); |
| 1004 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1005 | if (bs) |
| 1006 | OCSP_BASICRESP_free(bs); |
| 1007 | |
| 1008 | if (resp) |
| 1009 | OCSP_RESPONSE_free(resp); |
| 1010 | |
| 1011 | return ret; |
| 1012 | } |
| 1013 | /* |
| 1014 | * External function use to update the OCSP response in the OCSP response's |
| 1015 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 1016 | * to update in DER format. |
| 1017 | * |
| 1018 | * Returns 0 on success, 1 in error case. |
| 1019 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1020 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1021 | { |
| 1022 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1023 | } |
| 1024 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1025 | #endif |
| 1026 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1027 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 1028 | 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) |
| 1029 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1030 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1031 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1032 | struct connection *conn; |
| 1033 | int head; |
| 1034 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1035 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1036 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1037 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1038 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1039 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1040 | |
| 1041 | keys = ref->tlskeys; |
| 1042 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1043 | |
| 1044 | if (enc) { |
| 1045 | memcpy(key_name, keys[head].name, 16); |
| 1046 | |
| 1047 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1048 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1049 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1050 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1051 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1052 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1053 | goto end; |
| 1054 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1055 | 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] | 1056 | ret = 1; |
| 1057 | } |
| 1058 | else if (ref->key_size_bits == 256 ) { |
| 1059 | |
| 1060 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1061 | goto end; |
| 1062 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1063 | 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] | 1064 | ret = 1; |
| 1065 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1066 | } else { |
| 1067 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1068 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1069 | goto found; |
| 1070 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1071 | ret = 0; |
| 1072 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1073 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1074 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1075 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1076 | 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] | 1077 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1078 | goto end; |
| 1079 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1080 | ret = i ? 2 : 1; |
| 1081 | } |
| 1082 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1083 | 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] | 1084 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1085 | goto end; |
| 1086 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1087 | ret = i ? 2 : 1; |
| 1088 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1089 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1090 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1091 | end: |
| 1092 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1093 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1097 | { |
| 1098 | struct tls_keys_ref *ref; |
| 1099 | |
| 1100 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1101 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1102 | return ref; |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | |
| 1106 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1107 | { |
| 1108 | struct tls_keys_ref *ref; |
| 1109 | |
| 1110 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1111 | if (ref->unique_id == unique_id) |
| 1112 | return ref; |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1116 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1117 | * match existing ones, this function returns -1 |
| 1118 | * else it returns 0 on success. |
| 1119 | */ |
| 1120 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1121 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1122 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1123 | if (ref->key_size_bits == 128) { |
| 1124 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1125 | return -1; |
| 1126 | } |
| 1127 | else if (ref->key_size_bits == 256) { |
| 1128 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1129 | return -1; |
| 1130 | } |
| 1131 | else |
| 1132 | return -1; |
| 1133 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1134 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1135 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1136 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1137 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1138 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1139 | |
| 1140 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1143 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1144 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1145 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1146 | |
| 1147 | if(!ref) { |
| 1148 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1149 | return 1; |
| 1150 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1151 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1152 | memprintf(err, "Invalid key size"); |
| 1153 | return 1; |
| 1154 | } |
| 1155 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1156 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1157 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1158 | |
| 1159 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1160 | * automatic ids. It's called just after the basic checks. It returns |
| 1161 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1162 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1163 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1164 | { |
| 1165 | int i = 0; |
| 1166 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1167 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1168 | |
| 1169 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1170 | if (ref->unique_id == -1) { |
| 1171 | /* Look for the first free id. */ |
| 1172 | while (1) { |
| 1173 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1174 | if (ref2->unique_id == i) { |
| 1175 | i++; |
| 1176 | break; |
| 1177 | } |
| 1178 | } |
| 1179 | if (&ref2->list == &tlskeys_reference) |
| 1180 | break; |
| 1181 | } |
| 1182 | |
| 1183 | /* Uses the unique id and increment it for the next entry. */ |
| 1184 | ref->unique_id = i; |
| 1185 | i++; |
| 1186 | } |
| 1187 | } |
| 1188 | |
| 1189 | /* This sort the reference list by id. */ |
| 1190 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1191 | LIST_DEL(&ref->list); |
| 1192 | list_for_each_entry(ref3, &tkr, list) { |
| 1193 | if (ref->unique_id < ref3->unique_id) { |
| 1194 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1195 | break; |
| 1196 | } |
| 1197 | } |
| 1198 | if (&ref3->list == &tkr) |
| 1199 | LIST_ADDQ(&tkr, &ref->list); |
| 1200 | } |
| 1201 | |
| 1202 | /* swap root */ |
| 1203 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1204 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1205 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1206 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1207 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1208 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1209 | #ifndef OPENSSL_NO_OCSP |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1210 | int ocsp_ex_index = -1; |
| 1211 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1212 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1213 | { |
| 1214 | switch (evp_keytype) { |
| 1215 | case EVP_PKEY_RSA: |
| 1216 | return 2; |
| 1217 | case EVP_PKEY_DSA: |
| 1218 | return 0; |
| 1219 | case EVP_PKEY_EC: |
| 1220 | return 1; |
| 1221 | } |
| 1222 | |
| 1223 | return -1; |
| 1224 | } |
| 1225 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1226 | /* |
| 1227 | * Callback used to set OCSP status extension content in server hello. |
| 1228 | */ |
| 1229 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1230 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1231 | struct certificate_ocsp *ocsp; |
| 1232 | struct ocsp_cbk_arg *ocsp_arg; |
| 1233 | char *ssl_buf; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1234 | SSL_CTX *ctx; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1235 | EVP_PKEY *ssl_pkey; |
| 1236 | int key_type; |
| 1237 | int index; |
| 1238 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1239 | ctx = SSL_get_SSL_CTX(ssl); |
| 1240 | if (!ctx) |
| 1241 | return SSL_TLSEXT_ERR_NOACK; |
| 1242 | |
| 1243 | ocsp_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
| 1244 | if (!ocsp_arg) |
| 1245 | return SSL_TLSEXT_ERR_NOACK; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1246 | |
| 1247 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1248 | if (!ssl_pkey) |
| 1249 | return SSL_TLSEXT_ERR_NOACK; |
| 1250 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1251 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1252 | |
| 1253 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1254 | ocsp = ocsp_arg->s_ocsp; |
| 1255 | else { |
| 1256 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1257 | * the certificate type |
| 1258 | */ |
| 1259 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1260 | |
| 1261 | if (index < 0) |
| 1262 | return SSL_TLSEXT_ERR_NOACK; |
| 1263 | |
| 1264 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1265 | |
| 1266 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1267 | |
| 1268 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1269 | !ocsp->response.area || |
| 1270 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1271 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1272 | return SSL_TLSEXT_ERR_NOACK; |
| 1273 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1274 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1275 | if (!ssl_buf) |
| 1276 | return SSL_TLSEXT_ERR_NOACK; |
| 1277 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1278 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1279 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1280 | |
| 1281 | return SSL_TLSEXT_ERR_OK; |
| 1282 | } |
| 1283 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1284 | #endif |
| 1285 | |
Ilya Shipitsin | b3201a3 | 2020-10-18 09:11:50 +0500 | [diff] [blame] | 1286 | #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] | 1287 | |
| 1288 | |
| 1289 | /* |
| 1290 | * Decrease the refcount of the struct ocsp_response and frees it if it's not |
| 1291 | * used anymore. Also removes it from the tree if free'd. |
| 1292 | */ |
| 1293 | static void ssl_sock_free_ocsp(struct certificate_ocsp *ocsp) |
| 1294 | { |
| 1295 | if (!ocsp) |
| 1296 | return; |
| 1297 | |
| 1298 | ocsp->refcount--; |
| 1299 | if (ocsp->refcount <= 0) { |
| 1300 | ebmb_delete(&ocsp->key); |
| 1301 | chunk_destroy(&ocsp->response); |
| 1302 | free(ocsp); |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1307 | /* |
| 1308 | * 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] | 1309 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1310 | * status extension, the issuer's certificate is mandatory. It should be |
| 1311 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1312 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1313 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1314 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1315 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1316 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1317 | * |
| 1318 | * 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] | 1319 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1320 | */ |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1321 | 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] | 1322 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1323 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1324 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1325 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1326 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1327 | char *warn = NULL; |
| 1328 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1329 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1330 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1331 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1332 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1333 | if (!x) |
| 1334 | goto out; |
| 1335 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1336 | issuer = ckch->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1337 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1338 | if (chain) { |
| 1339 | /* check if one of the certificate of the chain is the issuer */ |
| 1340 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1341 | X509 *ti = sk_X509_value(chain, i); |
| 1342 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1343 | issuer = ti; |
| 1344 | break; |
| 1345 | } |
| 1346 | } |
| 1347 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1348 | if (!issuer) |
| 1349 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1350 | |
| 1351 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1352 | if (!cid) |
| 1353 | goto out; |
| 1354 | |
| 1355 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1356 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1357 | goto out; |
| 1358 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1359 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1360 | if (!ocsp) |
| 1361 | goto out; |
| 1362 | |
| 1363 | p = ocsp->key_data; |
| 1364 | i2d_OCSP_CERTID(cid, &p); |
| 1365 | |
| 1366 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1367 | if (iocsp == ocsp) |
| 1368 | ocsp = NULL; |
| 1369 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1370 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1371 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1372 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1373 | #endif |
| 1374 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1375 | |
| 1376 | if (!callback) { |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1377 | struct ocsp_cbk_arg *cb_arg; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1378 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1379 | |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1380 | cb_arg = calloc(1, sizeof(*cb_arg)); |
| 1381 | if (!cb_arg) |
| 1382 | goto out; |
| 1383 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1384 | cb_arg->is_single = 1; |
| 1385 | cb_arg->s_ocsp = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1386 | iocsp->refcount++; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1387 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1388 | pkey = X509_get_pubkey(x); |
| 1389 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1390 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1391 | |
| 1392 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1393 | 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 */ |
| 1394 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1395 | } else { |
| 1396 | /* |
| 1397 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1398 | * Update that cb_arg with the new cert's staple |
| 1399 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1400 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1401 | struct certificate_ocsp *tmp_ocsp; |
| 1402 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1403 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1404 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1405 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1406 | cb_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1407 | |
| 1408 | /* |
| 1409 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1410 | * the order of operations below matter, take care when changing it |
| 1411 | */ |
| 1412 | tmp_ocsp = cb_arg->s_ocsp; |
| 1413 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1414 | cb_arg->s_ocsp = NULL; |
| 1415 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1416 | cb_arg->is_single = 0; |
| 1417 | cb_arg->single_kt = 0; |
| 1418 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1419 | pkey = X509_get_pubkey(x); |
| 1420 | key_type = EVP_PKEY_base_id(pkey); |
| 1421 | EVP_PKEY_free(pkey); |
| 1422 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1423 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1424 | if (index >= 0 && !cb_arg->m_ocsp[index]) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1425 | cb_arg->m_ocsp[index] = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1426 | iocsp->refcount++; |
| 1427 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1428 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1429 | |
| 1430 | ret = 0; |
| 1431 | |
| 1432 | warn = NULL; |
William Lallemand | 86e4d63 | 2020-08-07 00:44:32 +0200 | [diff] [blame] | 1433 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, iocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1434 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1435 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1439 | if (cid) |
| 1440 | OCSP_CERTID_free(cid); |
| 1441 | |
| 1442 | if (ocsp) |
| 1443 | free(ocsp); |
| 1444 | |
| 1445 | if (warn) |
| 1446 | free(warn); |
| 1447 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1448 | return ret; |
| 1449 | } |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 1450 | #endif |
| 1451 | |
| 1452 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1453 | 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] | 1454 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1455 | 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] | 1456 | } |
| 1457 | #endif |
| 1458 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1459 | |
Ilya Shipitsin | e4f38f9 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 1460 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1461 | |
| 1462 | #define CT_EXTENSION_TYPE 18 |
| 1463 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1464 | int sctl_ex_index = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1465 | |
| 1466 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1467 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1468 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1469 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1470 | *out = (unsigned char *) sctl->area; |
| 1471 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1472 | |
| 1473 | return 1; |
| 1474 | } |
| 1475 | |
| 1476 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1477 | { |
| 1478 | return 1; |
| 1479 | } |
| 1480 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1481 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1482 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1483 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1484 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1485 | 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] | 1486 | goto out; |
| 1487 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1488 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1489 | |
| 1490 | ret = 0; |
| 1491 | |
| 1492 | out: |
| 1493 | return ret; |
| 1494 | } |
| 1495 | |
| 1496 | #endif |
| 1497 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1498 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1499 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1500 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1501 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1502 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1503 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1504 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1505 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1506 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1507 | * change this to #if and do not assign a default value to this macro! |
| 1508 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1509 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1510 | /* Disable renegotiation (CVE-2009-3555) */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 1511 | 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] | 1512 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1513 | conn->err_code = CO_ER_SSL_RENEG; |
| 1514 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1515 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1516 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1517 | |
| 1518 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1519 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1520 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1521 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1522 | consider that the buffering was activated, |
| 1523 | so we rise the output buffer size from 4k |
| 1524 | to 16k */ |
| 1525 | write_bio = SSL_get_wbio(ssl); |
| 1526 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1527 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1528 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1529 | } |
| 1530 | } |
| 1531 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1532 | } |
| 1533 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1534 | /* Callback is called for each certificate of the chain during a verify |
| 1535 | ok is set to 1 if preverify detect no error on current certificate. |
| 1536 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1537 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1538 | { |
| 1539 | SSL *ssl; |
| 1540 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1541 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1542 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1543 | |
| 1544 | 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] | 1545 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1546 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1547 | ctx = conn->xprt_ctx; |
| 1548 | |
| 1549 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1550 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1551 | if (ok) /* no errors */ |
| 1552 | return ok; |
| 1553 | |
| 1554 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1555 | err = X509_STORE_CTX_get_error(x_store); |
| 1556 | |
| 1557 | /* check if CA error needs to be ignored */ |
| 1558 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1559 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1560 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1561 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1562 | } |
| 1563 | |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1564 | 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] | 1565 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1566 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1567 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1568 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1569 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1570 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1571 | return 0; |
| 1572 | } |
| 1573 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1574 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1575 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1576 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1577 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 731248f | 2020-02-04 14:02:02 +0100 | [diff] [blame] | 1578 | 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] | 1579 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1580 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1581 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1582 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1583 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1584 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1585 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1586 | } |
| 1587 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1588 | #ifdef TLS1_RT_HEARTBEAT |
| 1589 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 1590 | int content_type, const void *buf, size_t len, |
| 1591 | SSL *ssl) |
| 1592 | { |
| 1593 | /* test heartbeat received (write_p is set to 0 |
| 1594 | for a received record) */ |
| 1595 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
| 1596 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 1597 | const unsigned char *p = buf; |
| 1598 | unsigned int payload; |
| 1599 | |
| 1600 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
| 1601 | |
| 1602 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1603 | if (*p != TLS1_HB_REQUEST) |
| 1604 | return; |
| 1605 | |
| 1606 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
| 1607 | goto kill_it; |
| 1608 | |
| 1609 | payload = (p[1] * 256) + p[2]; |
| 1610 | if (3 + payload + 16 <= len) |
| 1611 | return; /* OK no problem */ |
| 1612 | kill_it: |
| 1613 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1614 | * advertised payload is larger than the advertised packet |
| 1615 | * length, so we have garbage in the buffer between the |
| 1616 | * payload and the end of the buffer (p+len). We can't know |
| 1617 | * if the SSL stack is patched, and we don't know if we can |
| 1618 | * safely wipe out the area between p+3+len and payload. |
| 1619 | * So instead, we prevent the response from being sent by |
| 1620 | * setting the max_send_fragment to 0 and we report an SSL |
| 1621 | * error, which will kill this connection. It will be reported |
| 1622 | * above as SSL_ERROR_SSL while an other handshake failure with |
| 1623 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1624 | */ |
| 1625 | ssl->max_send_fragment = 0; |
| 1626 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1627 | } |
| 1628 | } |
| 1629 | #endif |
| 1630 | |
| 1631 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 1632 | int content_type, const void *buf, size_t len, |
| 1633 | SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1634 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1635 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1636 | unsigned char *msg; |
| 1637 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1638 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1639 | |
| 1640 | /* This function is called for "from client" and "to server" |
| 1641 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1642 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1643 | */ |
| 1644 | |
| 1645 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1646 | * otherwise it is set to 1. |
| 1647 | */ |
| 1648 | if (write_p != 0) |
| 1649 | return; |
| 1650 | |
| 1651 | /* content_type contains the type of message received or sent |
| 1652 | * according with the SSL/TLS protocol spec. This message is |
| 1653 | * encoded with one byte. The value 256 (two bytes) is used |
| 1654 | * for designing the SSL/TLS record layer. According with the |
| 1655 | * rfc6101, the expected message (other than 256) are: |
| 1656 | * - change_cipher_spec(20) |
| 1657 | * - alert(21) |
| 1658 | * - handshake(22) |
| 1659 | * - application_data(23) |
| 1660 | * - (255) |
| 1661 | * We are interessed by the handshake and specially the client |
| 1662 | * hello. |
| 1663 | */ |
| 1664 | if (content_type != 22) |
| 1665 | return; |
| 1666 | |
| 1667 | /* The message length is at least 4 bytes, containing the |
| 1668 | * message type and the message length. |
| 1669 | */ |
| 1670 | if (len < 4) |
| 1671 | return; |
| 1672 | |
| 1673 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1674 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1675 | * - hello_request(0) |
| 1676 | * - client_hello(1) |
| 1677 | * - server_hello(2) |
| 1678 | * - certificate(11) |
| 1679 | * - server_key_exchange (12) |
| 1680 | * - certificate_request(13) |
| 1681 | * - server_hello_done(14) |
| 1682 | * We are interested by the client hello. |
| 1683 | */ |
| 1684 | msg = (unsigned char *)buf; |
| 1685 | if (msg[0] != 1) |
| 1686 | return; |
| 1687 | |
| 1688 | /* Next three bytes are the length of the message. The total length |
| 1689 | * must be this decoded length + 4. If the length given as argument |
| 1690 | * is not the same, we abort the protocol dissector. |
| 1691 | */ |
| 1692 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1693 | if (len < rec_len + 4) |
| 1694 | return; |
| 1695 | msg += 4; |
| 1696 | end = msg + rec_len; |
| 1697 | if (end < msg) |
| 1698 | return; |
| 1699 | |
| 1700 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1701 | * 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] | 1702 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1703 | */ |
| 1704 | msg += 1 + 1 + 4 + 28; |
| 1705 | if (msg > end) |
| 1706 | return; |
| 1707 | |
| 1708 | /* Next, is session id: |
| 1709 | * if present, we have to jump by length + 1 for the size information |
| 1710 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1711 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1712 | if (msg[0] > 0) |
| 1713 | msg += msg[0]; |
| 1714 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1715 | if (msg > end) |
| 1716 | return; |
| 1717 | |
| 1718 | /* Next two bytes are the ciphersuite length. */ |
| 1719 | if (msg + 2 > end) |
| 1720 | return; |
| 1721 | rec_len = (msg[0] << 8) + msg[1]; |
| 1722 | msg += 2; |
| 1723 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1724 | return; |
| 1725 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1726 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1727 | if (!capture) |
| 1728 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1729 | /* Compute the xxh64 of the ciphersuite. */ |
| 1730 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1731 | |
| 1732 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1733 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1734 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1735 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1736 | |
| 1737 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1738 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1739 | |
| 1740 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 1741 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1742 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 1743 | int content_type, const void *buf, size_t len, |
| 1744 | SSL *ssl) |
| 1745 | { |
| 1746 | struct ssl_keylog *keylog; |
| 1747 | |
| 1748 | if (SSL_get_ex_data(ssl, ssl_keylog_index)) |
| 1749 | return; |
| 1750 | |
| 1751 | keylog = pool_alloc(pool_head_ssl_keylog); |
| 1752 | if (!keylog) |
| 1753 | return; |
| 1754 | |
| 1755 | memset(keylog, 0, sizeof(*keylog)); |
| 1756 | |
| 1757 | if (!SSL_set_ex_data(ssl, ssl_keylog_index, keylog)) { |
| 1758 | pool_free(pool_head_ssl_keylog, keylog); |
| 1759 | return; |
| 1760 | } |
| 1761 | } |
| 1762 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1763 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1764 | /* Callback is called for ssl protocol analyse */ |
| 1765 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1766 | { |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1767 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1768 | struct ssl_sock_msg_callback *cbk; |
| 1769 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1770 | /* Try to call all callback functions that were registered by using |
| 1771 | * ssl_sock_register_msg_callback(). |
| 1772 | */ |
| 1773 | list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) { |
| 1774 | cbk->func(conn, write_p, version, content_type, buf, len, ssl); |
| 1775 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1776 | } |
| 1777 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1778 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1779 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1780 | const unsigned char *in, unsigned int inlen, |
| 1781 | void *arg) |
| 1782 | { |
| 1783 | struct server *srv = arg; |
| 1784 | |
| 1785 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1786 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1787 | return SSL_TLSEXT_ERR_OK; |
| 1788 | return SSL_TLSEXT_ERR_NOACK; |
| 1789 | } |
| 1790 | #endif |
| 1791 | |
| 1792 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1793 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1794 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1795 | */ |
| 1796 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1797 | unsigned int *len, void *arg) |
| 1798 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1799 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1800 | |
| 1801 | *data = (const unsigned char *)conf->npn_str; |
| 1802 | *len = conf->npn_len; |
| 1803 | return SSL_TLSEXT_ERR_OK; |
| 1804 | } |
| 1805 | #endif |
| 1806 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1807 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1808 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1809 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1810 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1811 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1812 | unsigned char *outlen, |
| 1813 | const unsigned char *server, |
| 1814 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1815 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1816 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1817 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1818 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1819 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1820 | return SSL_TLSEXT_ERR_NOACK; |
| 1821 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1822 | return SSL_TLSEXT_ERR_OK; |
| 1823 | } |
| 1824 | #endif |
| 1825 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1826 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1827 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1828 | |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1829 | /* Configure a DNS SAN extenion on a certificate. */ |
| 1830 | int ssl_sock_add_san_ext(X509V3_CTX* ctx, X509* cert, const char *servername) { |
| 1831 | int failure = 0; |
| 1832 | X509_EXTENSION *san_ext = NULL; |
| 1833 | CONF *conf = NULL; |
| 1834 | struct buffer *san_name = get_trash_chunk(); |
| 1835 | |
| 1836 | conf = NCONF_new(NULL); |
| 1837 | if (!conf) { |
| 1838 | failure = 1; |
| 1839 | goto cleanup; |
| 1840 | } |
| 1841 | |
| 1842 | /* Build an extension based on the DNS entry above */ |
| 1843 | chunk_appendf(san_name, "DNS:%s", servername); |
| 1844 | san_ext = X509V3_EXT_nconf_nid(conf, ctx, NID_subject_alt_name, san_name->area); |
| 1845 | if (!san_ext) { |
| 1846 | failure = 1; |
| 1847 | goto cleanup; |
| 1848 | } |
| 1849 | |
| 1850 | /* Add the extension */ |
| 1851 | if (!X509_add_ext(cert, san_ext, -1 /* Add to end */)) { |
| 1852 | failure = 1; |
| 1853 | goto cleanup; |
| 1854 | } |
| 1855 | |
| 1856 | /* Success */ |
| 1857 | failure = 0; |
| 1858 | |
| 1859 | cleanup: |
| 1860 | if (NULL != san_ext) X509_EXTENSION_free(san_ext); |
| 1861 | if (NULL != conf) NCONF_free(conf); |
| 1862 | |
| 1863 | return failure; |
| 1864 | } |
| 1865 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1866 | /* Create a X509 certificate with the specified servername and serial. This |
| 1867 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1868 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1869 | 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] | 1870 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 1871 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
| 1872 | EVP_PKEY *capkey = bind_conf->ca_sign_ckch->key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1873 | SSL_CTX *ssl_ctx = NULL; |
| 1874 | X509 *newcrt = NULL; |
| 1875 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1876 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1877 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1878 | X509_NAME *name; |
| 1879 | const EVP_MD *digest; |
| 1880 | X509V3_CTX ctx; |
| 1881 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1882 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1883 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1884 | /* Get the private key of the default certificate and use it */ |
Ilya Shipitsin | 842976d | 2020-12-19 03:12:12 +0500 | [diff] [blame] | 1885 | #ifdef HAVE_SSL_CTX_get0_privatekey |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1886 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1887 | #else |
| 1888 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1889 | if (tmp_ssl) |
| 1890 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1891 | #endif |
| 1892 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1893 | goto mkcert_error; |
| 1894 | |
| 1895 | /* Create the certificate */ |
| 1896 | if (!(newcrt = X509_new())) |
| 1897 | goto mkcert_error; |
| 1898 | |
| 1899 | /* Set version number for the certificate (X509v3) and the serial |
| 1900 | * number */ |
| 1901 | if (X509_set_version(newcrt, 2L) != 1) |
| 1902 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 1903 | 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] | 1904 | |
| 1905 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1906 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1907 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1908 | goto mkcert_error; |
| 1909 | |
| 1910 | /* set public key in the certificate */ |
| 1911 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1912 | goto mkcert_error; |
| 1913 | |
| 1914 | /* Set issuer name from the CA */ |
| 1915 | if (!(name = X509_get_subject_name(cacert))) |
| 1916 | goto mkcert_error; |
| 1917 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1918 | goto mkcert_error; |
| 1919 | |
| 1920 | /* Set the subject name using the same, but the CN */ |
| 1921 | name = X509_NAME_dup(name); |
| 1922 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1923 | (const unsigned char *)servername, |
| 1924 | -1, -1, 0) != 1) { |
| 1925 | X509_NAME_free(name); |
| 1926 | goto mkcert_error; |
| 1927 | } |
| 1928 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1929 | X509_NAME_free(name); |
| 1930 | goto mkcert_error; |
| 1931 | } |
| 1932 | X509_NAME_free(name); |
| 1933 | |
| 1934 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1935 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1936 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1937 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1938 | X509_EXTENSION *ext; |
| 1939 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1940 | 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] | 1941 | goto mkcert_error; |
| 1942 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1943 | X509_EXTENSION_free(ext); |
| 1944 | goto mkcert_error; |
| 1945 | } |
| 1946 | X509_EXTENSION_free(ext); |
| 1947 | } |
| 1948 | |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1949 | /* Add SAN extension */ |
| 1950 | if (ssl_sock_add_san_ext(&ctx, newcrt, servername)) { |
| 1951 | goto mkcert_error; |
| 1952 | } |
| 1953 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1954 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1955 | |
| 1956 | key_type = EVP_PKEY_base_id(capkey); |
| 1957 | |
| 1958 | if (key_type == EVP_PKEY_DSA) |
| 1959 | digest = EVP_sha1(); |
| 1960 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1961 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1962 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1963 | digest = EVP_sha256(); |
| 1964 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 1965 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1966 | int nid; |
| 1967 | |
| 1968 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1969 | goto mkcert_error; |
| 1970 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1971 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1972 | #else |
| 1973 | goto mkcert_error; |
| 1974 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1975 | } |
| 1976 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1977 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1978 | goto mkcert_error; |
| 1979 | |
| 1980 | /* Create and set the new SSL_CTX */ |
| 1981 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1982 | goto mkcert_error; |
| 1983 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1984 | goto mkcert_error; |
| 1985 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1986 | goto mkcert_error; |
| 1987 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1988 | goto mkcert_error; |
| 1989 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 1990 | /* Build chaining the CA cert and the rest of the chain, keep these order */ |
| 1991 | #if defined(SSL_CTX_add1_chain_cert) |
| 1992 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, bind_conf->ca_sign_ckch->cert)) { |
| 1993 | goto mkcert_error; |
| 1994 | } |
| 1995 | |
| 1996 | if (bind_conf->ca_sign_ckch->chain) { |
| 1997 | for (i = 0; i < sk_X509_num(bind_conf->ca_sign_ckch->chain); i++) { |
| 1998 | X509 *chain_cert = sk_X509_value(bind_conf->ca_sign_ckch->chain, i); |
| 1999 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, chain_cert)) { |
| 2000 | goto mkcert_error; |
| 2001 | } |
| 2002 | } |
| 2003 | } |
| 2004 | #endif |
| 2005 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2006 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2007 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2008 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2009 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2010 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2011 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2012 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2013 | 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] | 2014 | EC_KEY *ecc; |
| 2015 | int nid; |
| 2016 | |
| 2017 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2018 | goto end; |
| 2019 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2020 | goto end; |
| 2021 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2022 | EC_KEY_free(ecc); |
| 2023 | } |
| 2024 | #endif |
| 2025 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2026 | return ssl_ctx; |
| 2027 | |
| 2028 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2029 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2030 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2031 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2032 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2033 | return NULL; |
| 2034 | } |
| 2035 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2036 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2037 | 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] | 2038 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 2039 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2040 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2041 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 2042 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2043 | } |
| 2044 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2045 | /* 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] | 2046 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2047 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2048 | 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] | 2049 | { |
| 2050 | struct lru64 *lru = NULL; |
| 2051 | |
| 2052 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2053 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2054 | 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] | 2055 | if (lru && lru->domain) { |
| 2056 | if (ssl) |
| 2057 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2058 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2059 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2060 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2061 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2062 | } |
| 2063 | return NULL; |
| 2064 | } |
| 2065 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2066 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2067 | * function is not thread-safe, it should only be used to check if a certificate |
| 2068 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2069 | * thread). It is kept for backward compatibility. */ |
| 2070 | SSL_CTX * |
| 2071 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2072 | { |
| 2073 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2074 | } |
| 2075 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2076 | /* Set a certificate int the LRU cache used to store generated |
| 2077 | * certificate. Return 0 on success, otherwise -1 */ |
| 2078 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2079 | 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] | 2080 | { |
| 2081 | struct lru64 *lru = NULL; |
| 2082 | |
| 2083 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2084 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2085 | 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] | 2086 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2087 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2088 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2089 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2090 | if (lru->domain && lru->data) |
| 2091 | lru->free((SSL_CTX *)lru->data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2092 | 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] | 2093 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2094 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2095 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2096 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2097 | } |
| 2098 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2099 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2100 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2101 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2102 | { |
| 2103 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2104 | } |
| 2105 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2106 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2107 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2108 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2109 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2110 | 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] | 2111 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2112 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2113 | SSL_CTX *ssl_ctx = NULL; |
| 2114 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2115 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2116 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2117 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2118 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2119 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2120 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2121 | if (lru && lru->domain) |
| 2122 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2123 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2124 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2125 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2126 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2127 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2128 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2129 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2130 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2131 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2132 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2133 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2134 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2135 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2136 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2137 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2138 | return 0; |
| 2139 | } |
| 2140 | static int |
| 2141 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2142 | { |
| 2143 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2144 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2145 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2146 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2147 | 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] | 2148 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2149 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2150 | } |
| 2151 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2152 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2153 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2154 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2155 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2156 | |
| 2157 | 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] | 2158 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2159 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2160 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2161 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2162 | #endif |
| 2163 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2164 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2165 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2166 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2167 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2168 | 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] | 2169 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2170 | 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] | 2171 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2172 | #endif |
| 2173 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2174 | 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] | 2175 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2176 | 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] | 2177 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2178 | #endif |
| 2179 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2180 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2181 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2182 | /* Unusable in this context. */ |
| 2183 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2184 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2185 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2186 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2187 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2188 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2189 | |
| 2190 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2191 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2192 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2193 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2194 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2195 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2196 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2197 | } |
| 2198 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2199 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2200 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2201 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2202 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2203 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2204 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2205 | } |
| 2206 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2207 | 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] | 2208 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2209 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2210 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2211 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2212 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2213 | } |
| 2214 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2215 | 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] | 2216 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2217 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2218 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2219 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2220 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2221 | } |
| 2222 | 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] | 2223 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2224 | 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] | 2225 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2226 | #endif |
| 2227 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2228 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2229 | #if SSL_OP_NO_TLSv1_3 |
| 2230 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2231 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2232 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2233 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2234 | #endif |
| 2235 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2236 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2237 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 2238 | struct methodVersions methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2239 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2240 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2241 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2242 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2243 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2244 | {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] | 2245 | }; |
| 2246 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2247 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2248 | { |
| 2249 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2250 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2251 | SSL_set_SSL_CTX(ssl, ctx); |
| 2252 | } |
| 2253 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2254 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2255 | |
| 2256 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2257 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2258 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2259 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2260 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2261 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2262 | return SSL_TLSEXT_ERR_OK; |
| 2263 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2264 | } |
| 2265 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2266 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2267 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2268 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2269 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2270 | #else |
| 2271 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2272 | { |
| 2273 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2274 | struct connection *conn; |
| 2275 | struct bind_conf *s; |
| 2276 | const uint8_t *extension_data; |
| 2277 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2278 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2279 | |
| 2280 | char *wildp = NULL; |
| 2281 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2282 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2283 | 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] | 2284 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2285 | int i; |
| 2286 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2287 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2288 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2289 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2290 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2291 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2292 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2293 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2294 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2295 | #else |
| 2296 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2297 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2298 | /* |
| 2299 | * The server_name extension was given too much extensibility when it |
| 2300 | * was written, so parsing the normal case is a bit complex. |
| 2301 | */ |
| 2302 | size_t len; |
| 2303 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2304 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2305 | /* Extract the length of the supplied list of names. */ |
| 2306 | len = (*extension_data++) << 8; |
| 2307 | len |= *extension_data++; |
| 2308 | if (len + 2 != extension_len) |
| 2309 | goto abort; |
| 2310 | /* |
| 2311 | * The list in practice only has a single element, so we only consider |
| 2312 | * the first one. |
| 2313 | */ |
| 2314 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2315 | goto abort; |
| 2316 | extension_len = len - 1; |
| 2317 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2318 | if (extension_len <= 2) |
| 2319 | goto abort; |
| 2320 | len = (*extension_data++) << 8; |
| 2321 | len |= *extension_data++; |
| 2322 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2323 | || memchr(extension_data, 0, len) != NULL) |
| 2324 | goto abort; |
| 2325 | servername = extension_data; |
| 2326 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2327 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2328 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2329 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2330 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2331 | } |
| 2332 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2333 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2334 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2335 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2336 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2337 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2338 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2339 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2340 | goto abort; |
| 2341 | } |
| 2342 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2343 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2344 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2345 | 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] | 2346 | #else |
| 2347 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2348 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2349 | uint8_t sign; |
| 2350 | size_t len; |
| 2351 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2352 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2353 | len = (*extension_data++) << 8; |
| 2354 | len |= *extension_data++; |
| 2355 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2356 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2357 | if (len % 2 != 0) |
| 2358 | goto abort; |
| 2359 | for (; len > 0; len -= 2) { |
| 2360 | extension_data++; /* hash */ |
| 2361 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2362 | switch (sign) { |
| 2363 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2364 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2365 | break; |
| 2366 | case TLSEXT_signature_ecdsa: |
| 2367 | has_ecdsa_sig = 1; |
| 2368 | break; |
| 2369 | default: |
| 2370 | continue; |
| 2371 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2372 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2373 | break; |
| 2374 | } |
| 2375 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2376 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2377 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2378 | } |
| 2379 | 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] | 2380 | const SSL_CIPHER *cipher; |
| 2381 | size_t len; |
| 2382 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2383 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2384 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2385 | len = ctx->cipher_suites_len; |
| 2386 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2387 | #else |
| 2388 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2389 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2390 | if (len % 2 != 0) |
| 2391 | goto abort; |
| 2392 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2393 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2394 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2395 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2396 | #else |
| 2397 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2398 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2399 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2400 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2401 | break; |
| 2402 | } |
| 2403 | } |
| 2404 | } |
| 2405 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2406 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2407 | trash.area[i] = tolower(servername[i]); |
| 2408 | if (!wildp && (trash.area[i] == '.')) |
| 2409 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2410 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2411 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2412 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2413 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2414 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2415 | /* Look for an ECDSA, RSA and DSA certificate, first in the single |
| 2416 | * name and if not found in the wildcard */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2417 | for (i = 0; i < 2; i++) { |
| 2418 | if (i == 0) /* lookup in full qualified names */ |
| 2419 | node = ebst_lookup(&s->sni_ctx, trash.area); |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2420 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2421 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2422 | else |
| 2423 | break; |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2424 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2425 | for (n = node; n; n = ebmb_next_dup(n)) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2426 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2427 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2428 | if (!container_of(n, struct sni_ctx, name)->neg) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2429 | struct sni_ctx *sni, *sni_tmp; |
| 2430 | int skip = 0; |
| 2431 | |
| 2432 | if (i == 1 && wildp) { /* wildcard */ |
| 2433 | /* If this is a wildcard, look for an exclusion on the same crt-list line */ |
| 2434 | sni = container_of(n, struct sni_ctx, name); |
| 2435 | list_for_each_entry(sni_tmp, &sni->ckch_inst->sni_ctx, by_ckch_inst) { |
| 2436 | if (sni_tmp->neg && (!strcmp((const char *)sni_tmp->name.key, trash.area))) { |
| 2437 | skip = 1; |
| 2438 | break; |
| 2439 | } |
| 2440 | } |
| 2441 | if (skip) |
| 2442 | continue; |
| 2443 | } |
| 2444 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2445 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2446 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2447 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2448 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2449 | break; |
| 2450 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2451 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2452 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2453 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2454 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2455 | if (!node_anonymous) |
| 2456 | node_anonymous = n; |
| 2457 | break; |
| 2458 | } |
| 2459 | } |
| 2460 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2461 | } |
| 2462 | /* Once the certificates are found, select them depending on what is |
| 2463 | * supported in the client and by key_signature priority order: EDSA > |
| 2464 | * RSA > DSA */ |
William Lallemand | 5b1d1f6 | 2020-08-14 15:30:13 +0200 | [diff] [blame] | 2465 | if (has_ecdsa_sig && node_ecdsa) |
| 2466 | node = node_ecdsa; |
| 2467 | else if (has_rsa_sig && node_rsa) |
| 2468 | node = node_rsa; |
| 2469 | else if (node_anonymous) |
| 2470 | node = node_anonymous; |
| 2471 | else if (node_ecdsa) |
| 2472 | node = node_ecdsa; /* no ecdsa signature case (< TLSv1.2) */ |
| 2473 | else |
| 2474 | node = node_rsa; /* no rsa signature case (far far away) */ |
| 2475 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2476 | if (node) { |
| 2477 | /* switch ctx */ |
| 2478 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2479 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 2480 | if (conf) { |
| 2481 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2482 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2483 | if (conf->early_data) |
| 2484 | allow_early = 1; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2485 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2486 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
| 2487 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2488 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2489 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2490 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2491 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2492 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2493 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2494 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2495 | } |
| 2496 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2497 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2498 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2499 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2500 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2501 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2502 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2503 | allow_early: |
| 2504 | #ifdef OPENSSL_IS_BORINGSSL |
| 2505 | if (allow_early) |
| 2506 | SSL_set_early_data_enabled(ssl, 1); |
| 2507 | #else |
| 2508 | if (!allow_early) |
| 2509 | SSL_set_max_early_data(ssl, 0); |
| 2510 | #endif |
| 2511 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2512 | abort: |
| 2513 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2514 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2515 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2516 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2517 | #else |
| 2518 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2519 | return 0; |
| 2520 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2521 | } |
| 2522 | |
| 2523 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2524 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2525 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2526 | * warning when no match is found, which implies the default (first) cert |
| 2527 | * will keep being used. |
| 2528 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2529 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2530 | { |
| 2531 | const char *servername; |
| 2532 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2533 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2534 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2535 | int i; |
| 2536 | (void)al; /* shut gcc stupid warning */ |
| 2537 | |
| 2538 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2539 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2540 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2541 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2542 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2543 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2544 | if (s->strict_sni) |
| 2545 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2546 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2547 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2548 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2549 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2550 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2551 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2552 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2553 | if (!servername[i]) |
| 2554 | break; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 2555 | trash.area[i] = tolower((unsigned char)servername[i]); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2556 | if (!wildp && (trash.area[i] == '.')) |
| 2557 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2558 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2559 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2560 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2561 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2562 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2563 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2564 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2565 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2566 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2567 | node = n; |
| 2568 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2569 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2570 | } |
| 2571 | if (!node && wildp) { |
| 2572 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2573 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2574 | /* lookup a not neg filter */ |
| 2575 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2576 | node = n; |
| 2577 | break; |
| 2578 | } |
| 2579 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2580 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2581 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2582 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2583 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2584 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2585 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2586 | return SSL_TLSEXT_ERR_OK; |
| 2587 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2588 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2589 | if (s->strict_sni) { |
| 2590 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2591 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2592 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2593 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2594 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2595 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2596 | } |
| 2597 | |
| 2598 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2599 | 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] | 2600 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2601 | return SSL_TLSEXT_ERR_OK; |
| 2602 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2603 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2604 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2605 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2606 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2607 | |
| 2608 | static DH * ssl_get_dh_1024(void) |
| 2609 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2610 | static unsigned char dh1024_p[]={ |
| 2611 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2612 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2613 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2614 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2615 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2616 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2617 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2618 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2619 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2620 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2621 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2622 | }; |
| 2623 | static unsigned char dh1024_g[]={ |
| 2624 | 0x02, |
| 2625 | }; |
| 2626 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2627 | BIGNUM *p; |
| 2628 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2629 | DH *dh = DH_new(); |
| 2630 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2631 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2632 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2633 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2634 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2635 | DH_free(dh); |
| 2636 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2637 | } else { |
| 2638 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2639 | } |
| 2640 | } |
| 2641 | return dh; |
| 2642 | } |
| 2643 | |
| 2644 | static DH *ssl_get_dh_2048(void) |
| 2645 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2646 | static unsigned char dh2048_p[]={ |
| 2647 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2648 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2649 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2650 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2651 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2652 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2653 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2654 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2655 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2656 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2657 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2658 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2659 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2660 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2661 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2662 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2663 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2664 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2665 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2666 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2667 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2668 | 0xB7,0x1F,0x77,0xF3, |
| 2669 | }; |
| 2670 | static unsigned char dh2048_g[]={ |
| 2671 | 0x02, |
| 2672 | }; |
| 2673 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2674 | BIGNUM *p; |
| 2675 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2676 | DH *dh = DH_new(); |
| 2677 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2678 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2679 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2680 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2681 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2682 | DH_free(dh); |
| 2683 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2684 | } else { |
| 2685 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2686 | } |
| 2687 | } |
| 2688 | return dh; |
| 2689 | } |
| 2690 | |
| 2691 | static DH *ssl_get_dh_4096(void) |
| 2692 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2693 | static unsigned char dh4096_p[]={ |
| 2694 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2695 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2696 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2697 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2698 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2699 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2700 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2701 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2702 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2703 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2704 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2705 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2706 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2707 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2708 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2709 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2710 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2711 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2712 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2713 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2714 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2715 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2716 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2717 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2718 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2719 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2720 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2721 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2722 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2723 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2724 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2725 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2726 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2727 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2728 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2729 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2730 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2731 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2732 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2733 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2734 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2735 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2736 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2737 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2738 | static unsigned char dh4096_g[]={ |
| 2739 | 0x02, |
| 2740 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2741 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2742 | BIGNUM *p; |
| 2743 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2744 | DH *dh = DH_new(); |
| 2745 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2746 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2747 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2748 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2749 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2750 | DH_free(dh); |
| 2751 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2752 | } else { |
| 2753 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2754 | } |
| 2755 | } |
| 2756 | return dh; |
| 2757 | } |
| 2758 | |
| 2759 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2760 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2761 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2762 | { |
| 2763 | DH *dh = NULL; |
| 2764 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2765 | int type; |
| 2766 | |
| 2767 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2768 | |
| 2769 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2770 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2771 | */ |
| 2772 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2773 | keylen = EVP_PKEY_bits(pkey); |
| 2774 | } |
| 2775 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2776 | if (keylen > global_ssl.default_dh_param) { |
| 2777 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2778 | } |
| 2779 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2780 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2781 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2782 | } |
| 2783 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2784 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2785 | } |
| 2786 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2787 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2788 | } |
| 2789 | |
| 2790 | return dh; |
| 2791 | } |
| 2792 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2793 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2794 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2795 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2796 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2797 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2798 | if (in == NULL) |
| 2799 | goto end; |
| 2800 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2801 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2802 | goto end; |
| 2803 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2804 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2805 | |
| 2806 | end: |
| 2807 | if (in) |
| 2808 | BIO_free(in); |
| 2809 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2810 | ERR_clear_error(); |
| 2811 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2812 | return dh; |
| 2813 | } |
| 2814 | |
| 2815 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2816 | { |
| 2817 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2818 | |
| 2819 | if (global_dh) { |
| 2820 | return 0; |
| 2821 | } |
| 2822 | |
| 2823 | return -1; |
| 2824 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2825 | #endif |
| 2826 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2827 | /* 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] | 2828 | 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] | 2829 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2830 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2831 | { |
| 2832 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2833 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2834 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2835 | if (*name == '!') { |
| 2836 | neg = 1; |
| 2837 | name++; |
| 2838 | } |
| 2839 | if (*name == '*') { |
| 2840 | wild = 1; |
| 2841 | name++; |
| 2842 | } |
| 2843 | /* !* filter is a nop */ |
| 2844 | if (neg && wild) |
| 2845 | return order; |
| 2846 | if (*name) { |
| 2847 | int j, len; |
| 2848 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2849 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 2850 | trash.area[j] = tolower((unsigned char)name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2851 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2852 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2853 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2854 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2855 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2856 | if (!sc) |
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 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2859 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2860 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2861 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2862 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2863 | sc->order = order++; |
| 2864 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2865 | sc->wild = wild; |
| 2866 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 2867 | sc->ckch_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2868 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2869 | } |
| 2870 | return order; |
| 2871 | } |
| 2872 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2873 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2874 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 2875 | * This function can't return an error. |
| 2876 | * |
| 2877 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 2878 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 2879 | 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] | 2880 | { |
| 2881 | |
| 2882 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 2883 | struct ebmb_node *node; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2884 | int def = 0; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2885 | |
| 2886 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 2887 | |
| 2888 | /* ignore if sc0 was already inserted in a tree */ |
| 2889 | if (sc0->name.node.leaf_p) |
| 2890 | continue; |
| 2891 | |
| 2892 | /* Check for duplicates. */ |
| 2893 | if (sc0->wild) |
| 2894 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 2895 | else |
| 2896 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 2897 | |
| 2898 | for (; node; node = ebmb_next_dup(node)) { |
| 2899 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 2900 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 2901 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 2902 | /* it's a duplicate, we should remove and free it */ |
| 2903 | LIST_DEL(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2904 | SSL_CTX_free(sc0->ctx); |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2905 | free(sc0); |
| 2906 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 2907 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2908 | } |
| 2909 | } |
| 2910 | |
| 2911 | /* if duplicate, ignore the insertion */ |
| 2912 | if (!sc0) |
| 2913 | continue; |
| 2914 | |
| 2915 | if (sc0->wild) |
| 2916 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 2917 | else |
| 2918 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2919 | |
| 2920 | /* replace the default_ctx if required with the first ctx */ |
| 2921 | if (ckch_inst->is_default && !def) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 2922 | SSL_CTX_free(bind_conf->default_ctx); |
| 2923 | SSL_CTX_up_ref(sc0->ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2924 | bind_conf->default_ctx = sc0->ctx; |
| 2925 | def = 1; |
| 2926 | } |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2927 | } |
| 2928 | } |
| 2929 | |
| 2930 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2931 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2932 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2933 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2934 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 2935 | /* tree of crtlist (crt-list/directory) */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 2936 | struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2937 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2938 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2939 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2940 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 2941 | * DH parameter available in ckchs nor in global, the default |
| 2942 | * DH parameters are applied on the SSL_CTX. |
| 2943 | * Returns a bitfield containing the flags: |
| 2944 | * ERR_FATAL in any fatal error case |
| 2945 | * ERR_ALERT if a reason of the error is availabine in err |
| 2946 | * ERR_WARN if a warning is available into err |
| 2947 | * The value 0 means there is no error nor warning and |
| 2948 | * the operation succeed. |
| 2949 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2950 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2951 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 2952 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2953 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2954 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2955 | DH *dh = NULL; |
| 2956 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 2957 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2958 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2959 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 2960 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 2961 | err && *err ? *err : "", path); |
| 2962 | #if defined(SSL_CTX_set_dh_auto) |
| 2963 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2964 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2965 | err && *err ? *err : ""); |
| 2966 | #else |
| 2967 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2968 | err && *err ? *err : ""); |
| 2969 | #endif |
| 2970 | ret |= ERR_WARN; |
| 2971 | goto end; |
| 2972 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2973 | |
| 2974 | if (ssl_dh_ptr_index >= 0) { |
| 2975 | /* store a pointer to the DH params to avoid complaining about |
| 2976 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2977 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2978 | } |
| 2979 | } |
| 2980 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2981 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 2982 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 2983 | err && *err ? *err : "", path); |
| 2984 | #if defined(SSL_CTX_set_dh_auto) |
| 2985 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2986 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2987 | err && *err ? *err : ""); |
| 2988 | #else |
| 2989 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2990 | err && *err ? *err : ""); |
| 2991 | #endif |
| 2992 | ret |= ERR_WARN; |
| 2993 | goto end; |
| 2994 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2995 | } |
| 2996 | else { |
| 2997 | /* Clear openssl global errors stack */ |
| 2998 | ERR_clear_error(); |
| 2999 | |
Willy Tarreau | 58ee231 | 2020-11-05 19:38:05 +0100 | [diff] [blame] | 3000 | if (global_ssl.default_dh_param && global_ssl.default_dh_param <= 1024) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3001 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3002 | if (local_dh_1024 == NULL) |
| 3003 | local_dh_1024 = ssl_get_dh_1024(); |
| 3004 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3005 | if (local_dh_1024 == NULL) { |
| 3006 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3007 | err && *err ? *err : "", path); |
| 3008 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3009 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3010 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3011 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3012 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 3013 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3014 | err && *err ? *err : "", path); |
| 3015 | #if defined(SSL_CTX_set_dh_auto) |
| 3016 | SSL_CTX_set_dh_auto(ctx, 1); |
| 3017 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 3018 | err && *err ? *err : ""); |
| 3019 | #else |
| 3020 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3021 | err && *err ? *err : ""); |
| 3022 | #endif |
| 3023 | ret |= ERR_WARN; |
| 3024 | goto end; |
| 3025 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3026 | } |
| 3027 | else { |
| 3028 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 3029 | } |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3030 | } |
| 3031 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3032 | end: |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3033 | ERR_clear_error(); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3034 | return ret; |
| 3035 | } |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3036 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3037 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3038 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3039 | * Returns a bitfield containing the flags: |
| 3040 | * ERR_FATAL in any fatal error case |
| 3041 | * ERR_ALERT if the reason of the error is available in err |
| 3042 | * ERR_WARN if a warning is available into err |
| 3043 | * The value 0 means there is no error nor warning and |
| 3044 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3045 | */ |
| 3046 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3047 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3048 | int errcode = 0; |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3049 | STACK_OF(X509) *find_chain = NULL; |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3050 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3051 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3052 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3053 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3054 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3055 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3056 | } |
| 3057 | |
| 3058 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3059 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3060 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3061 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3062 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3063 | } |
| 3064 | |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3065 | if (ckch->chain) { |
| 3066 | find_chain = ckch->chain; |
| 3067 | } else { |
| 3068 | /* Find Certificate Chain in global */ |
| 3069 | struct issuer_chain *issuer; |
Emmanuel Hocdet | ef87e0a | 2020-03-23 11:29:11 +0100 | [diff] [blame] | 3070 | issuer = ssl_get0_issuer_chain(ckch->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3071 | if (issuer) |
| 3072 | find_chain = issuer->chain; |
| 3073 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3074 | |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3075 | if (!find_chain) { |
| 3076 | /* always put a null chain stack in the SSL_CTX so it does not |
| 3077 | * try to build the chain from the verify store */ |
| 3078 | find_chain = sk_X509_new_null(); |
| 3079 | } |
| 3080 | |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3081 | /* 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] | 3082 | #ifdef SSL_CTX_set1_chain |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3083 | if (!SSL_CTX_set1_chain(ctx, find_chain)) { |
| 3084 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3085 | err && *err ? *err : "", path); |
| 3086 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3087 | goto end; |
| 3088 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3089 | #else |
| 3090 | { /* legacy compat (< openssl 1.0.2) */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3091 | X509 *ca; |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3092 | STACK_OF(X509) *chain; |
| 3093 | chain = X509_chain_up_ref(find_chain); |
| 3094 | while ((ca = sk_X509_shift(chain))) |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3095 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3096 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3097 | err && *err ? *err : "", path); |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3098 | X509_free(ca); |
| 3099 | sk_X509_pop_free(chain, X509_free); |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3100 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3101 | goto end; |
| 3102 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3103 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3104 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3105 | |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3106 | #ifdef SSL_CTX_build_cert_chain |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3107 | /* remove the Root CA from the SSL_CTX if the option is activated */ |
| 3108 | if (global_ssl.skip_self_issued_ca) { |
| 3109 | if (!SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT|SSL_BUILD_CHAIN_FLAG_UNTRUSTED|SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)) { |
| 3110 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3111 | err && *err ? *err : "", path); |
| 3112 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3113 | goto end; |
| 3114 | } |
| 3115 | } |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3116 | #endif |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3117 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3118 | #ifndef OPENSSL_NO_DH |
| 3119 | /* store a NULL pointer to indicate we have not yet loaded |
| 3120 | a custom DH param file */ |
| 3121 | if (ssl_dh_ptr_index >= 0) { |
| 3122 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3123 | } |
| 3124 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3125 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 3126 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3127 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3128 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3129 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3130 | } |
| 3131 | #endif |
| 3132 | |
Ilya Shipitsin | e4f38f9 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 3133 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3134 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 3135 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 3136 | 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] | 3137 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3138 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3139 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3140 | } |
| 3141 | } |
| 3142 | #endif |
| 3143 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 3144 | #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] | 3145 | /* Load OCSP Info into context */ |
| 3146 | if (ckch->ocsp_response) { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 3147 | if (ssl_sock_load_ocsp(ctx, ckch, find_chain) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3148 | 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", |
| 3149 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3150 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3151 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3152 | } |
| 3153 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3154 | #endif |
| 3155 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3156 | end: |
| 3157 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3158 | } |
| 3159 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3160 | /* |
| 3161 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3162 | * |
| 3163 | * Returns a bitfield containing the flags: |
| 3164 | * ERR_FATAL in any fatal error case |
| 3165 | * ERR_ALERT if the reason of the error is available in err |
| 3166 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3167 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3168 | 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] | 3169 | 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] | 3170 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3171 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3172 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3173 | int order = 0; |
| 3174 | X509_NAME *xname; |
| 3175 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3176 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3177 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3178 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3179 | STACK_OF(GENERAL_NAME) *names; |
| 3180 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3181 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3182 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3183 | int errcode = 0; |
| 3184 | |
| 3185 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3186 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3187 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3188 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3189 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3190 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3191 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3192 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3193 | if (!ctx) { |
| 3194 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3195 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3196 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3197 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3198 | } |
| 3199 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3200 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 3201 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3202 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3203 | |
| 3204 | ckch_inst = ckch_inst_new(); |
| 3205 | if (!ckch_inst) { |
| 3206 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3207 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3208 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3209 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3210 | } |
| 3211 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3212 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3213 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3214 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3215 | switch(EVP_PKEY_base_id(pkey)) { |
| 3216 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3217 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3218 | break; |
| 3219 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3220 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3221 | break; |
| 3222 | case EVP_PKEY_DSA: |
| 3223 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3224 | break; |
| 3225 | } |
| 3226 | EVP_PKEY_free(pkey); |
| 3227 | } |
| 3228 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3229 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3230 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3231 | 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] | 3232 | if (order < 0) { |
| 3233 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3234 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3235 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3236 | } |
| 3237 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3238 | } |
| 3239 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3240 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3241 | 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] | 3242 | if (names) { |
| 3243 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3244 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3245 | if (name->type == GEN_DNS) { |
| 3246 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3247 | 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] | 3248 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3249 | if (order < 0) { |
| 3250 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3251 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3252 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3253 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3254 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3255 | } |
| 3256 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3257 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3258 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3259 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3260 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3261 | i = -1; |
| 3262 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3263 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3264 | ASN1_STRING *value; |
| 3265 | |
| 3266 | value = X509_NAME_ENTRY_get_data(entry); |
| 3267 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3268 | 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] | 3269 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3270 | if (order < 0) { |
| 3271 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3272 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3273 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3274 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3275 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3276 | } |
| 3277 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3278 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3279 | * the tree, so it will be discovered and cleaned in time. |
| 3280 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3281 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3282 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3283 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3284 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3285 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3286 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3287 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3288 | } |
| 3289 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3290 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3291 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3292 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3293 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3294 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3295 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3296 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3297 | /* everything succeed, the ckch instance can be used */ |
| 3298 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3299 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3300 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3301 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3302 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3303 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3304 | *ckchi = ckch_inst; |
| 3305 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3306 | |
| 3307 | error: |
| 3308 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3309 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3310 | if (ckch_inst->is_default) |
| 3311 | SSL_CTX_free(ctx); |
| 3312 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3313 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3314 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3315 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3316 | SSL_CTX_free(ctx); |
| 3317 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3318 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3319 | } |
| 3320 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 3321 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3322 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 3323 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3324 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3325 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3326 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3327 | |
| 3328 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | e7eb1fe | 2020-09-16 16:17:51 +0200 | [diff] [blame] | 3329 | 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] | 3330 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3331 | if (errcode & ERR_CODE) |
| 3332 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3333 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3334 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3335 | |
| 3336 | /* succeed, add the instance to the ckch_store's list of instance */ |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3337 | LIST_ADDQ(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3338 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3339 | } |
| 3340 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3341 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3342 | |
| 3343 | |
| 3344 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3345 | * done once. Zero is returned if the operation fails. No error is returned |
| 3346 | * if the random is said as not implemented, because we expect that openssl |
| 3347 | * will use another method once needed. |
| 3348 | */ |
| 3349 | static int ssl_initialize_random() |
| 3350 | { |
| 3351 | unsigned char random; |
| 3352 | static int random_initialized = 0; |
| 3353 | |
| 3354 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3355 | random_initialized = 1; |
| 3356 | |
| 3357 | return random_initialized; |
| 3358 | } |
| 3359 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3360 | /* Load a crt-list file, this is done in 2 parts: |
| 3361 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 3362 | * - generate the instances by iterating on entries in the crtlist struct |
| 3363 | * |
| 3364 | * Nothing is locked there, this function is used in the configuration parser. |
| 3365 | * |
| 3366 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3367 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3368 | 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] | 3369 | { |
| 3370 | struct crtlist *crtlist = NULL; |
| 3371 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3372 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3373 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3374 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3375 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3376 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3377 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 3378 | if (!bind_conf_node) { |
| 3379 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 3380 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3381 | goto error; |
| 3382 | } |
| 3383 | bind_conf_node->next = NULL; |
| 3384 | bind_conf_node->bind_conf = bind_conf; |
| 3385 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3386 | /* strip trailing slashes, including first one */ |
| 3387 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 3388 | *end = 0; |
| 3389 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3390 | /* look for an existing crtlist or create one */ |
| 3391 | eb = ebst_lookup(&crtlists_tree, file); |
| 3392 | if (eb) { |
| 3393 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 3394 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3395 | /* load a crt-list OR a directory */ |
| 3396 | if (dir) |
| 3397 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 3398 | else |
| 3399 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 3400 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3401 | if (!(cfgerr & ERR_CODE)) |
| 3402 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 3403 | } |
| 3404 | |
| 3405 | if (cfgerr & ERR_CODE) { |
| 3406 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3407 | goto error; |
| 3408 | } |
| 3409 | |
| 3410 | /* generates ckch instance from the crtlist_entry */ |
| 3411 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3412 | struct ckch_store *store; |
| 3413 | struct ckch_inst *ckch_inst = NULL; |
| 3414 | |
| 3415 | store = entry->node.key; |
| 3416 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 3417 | if (cfgerr & ERR_CODE) { |
| 3418 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 3419 | goto error; |
| 3420 | } |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3421 | LIST_ADDQ(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 3422 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3423 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3424 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3425 | /* add the bind_conf to the list */ |
| 3426 | bind_conf_node->next = crtlist->bind_conf; |
| 3427 | crtlist->bind_conf = bind_conf_node; |
| 3428 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3429 | return cfgerr; |
| 3430 | error: |
| 3431 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3432 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3433 | struct ckch_inst *inst, *s_inst; |
| 3434 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3435 | lastentry = entry; /* which entry we tried to generate last */ |
| 3436 | if (lastentry) { |
| 3437 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3438 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 3439 | break; |
| 3440 | |
| 3441 | 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] | 3442 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3443 | /* this was not generated for this bind_conf, skip */ |
| 3444 | if (inst->bind_conf != bind_conf) |
| 3445 | continue; |
| 3446 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3447 | /* free the sni_ctx and instance */ |
| 3448 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3449 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3450 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3451 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3452 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3453 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3454 | return cfgerr; |
| 3455 | } |
| 3456 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3457 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 3458 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 3459 | { |
| 3460 | struct stat buf; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3461 | int cfgerr = 0; |
| 3462 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3463 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3464 | int found = 0; /* did we found a file to load ? */ |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3465 | |
| 3466 | if ((ckchs = ckchs_lookup(path))) { |
| 3467 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3468 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3469 | found++; |
| 3470 | } else if (stat(path, &buf) == 0) { |
| 3471 | found++; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3472 | if (S_ISDIR(buf.st_mode) == 0) { |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3473 | ckchs = ckchs_load_cert_file(path, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3474 | if (!ckchs) |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3475 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3476 | 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] | 3477 | } else { |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3478 | 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] | 3479 | } |
| 3480 | } else { |
| 3481 | /* stat failed, could be a bundle */ |
| 3482 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3483 | char fp[MAXPATHLEN+1] = {0}; |
| 3484 | int n = 0; |
| 3485 | |
| 3486 | /* Load all possible certs and keys in separate ckch_store */ |
| 3487 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3488 | struct stat buf; |
| 3489 | int ret; |
| 3490 | |
| 3491 | ret = snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3492 | if (ret > sizeof(fp)) |
| 3493 | continue; |
| 3494 | |
| 3495 | if ((ckchs = ckchs_lookup(fp))) { |
| 3496 | 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] | 3497 | found++; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3498 | } else { |
| 3499 | if (stat(fp, &buf) == 0) { |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3500 | found++; |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3501 | ckchs = ckchs_load_cert_file(fp, err); |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3502 | if (!ckchs) |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3503 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3504 | cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3505 | } |
| 3506 | } |
| 3507 | } |
William Lallemand | 00b831e | 2020-12-04 15:45:02 +0100 | [diff] [blame] | 3508 | #if HA_OPENSSL_VERSION_NUMBER < 0x10101000L |
| 3509 | if (found) { |
| 3510 | memprintf(err, "%sCan't load '%s'. Loading a multi certificates bundle requires OpenSSL >= 1.1.1\n", |
| 3511 | err && *err ? *err : "", path); |
| 3512 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3513 | } |
| 3514 | #endif |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3515 | } |
| 3516 | } |
William Lallemand | 7efbaf1 | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3517 | if (!found) { |
| 3518 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3519 | err && *err ? *err : "", path, strerror(errno)); |
| 3520 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3521 | } |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3522 | |
| 3523 | return cfgerr; |
| 3524 | } |
| 3525 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3526 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3527 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3528 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3529 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3530 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3531 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3532 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3533 | SSL_OP_NO_SSLv2 | |
| 3534 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3535 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3536 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3537 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3538 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3539 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3540 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3541 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3542 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3543 | SSL_MODE_RELEASE_BUFFERS | |
| 3544 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3545 | 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] | 3546 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3547 | int flags = MC_SSL_O_ALL; |
| 3548 | int cfgerr = 0; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 3549 | const int default_min_ver = CONF_TLSV12; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3550 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3551 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3552 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3553 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3554 | 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] | 3555 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3556 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3557 | 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] | 3558 | else |
| 3559 | flags = conf_ssl_methods->flags; |
| 3560 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3561 | min = conf_ssl_methods->min; |
| 3562 | max = conf_ssl_methods->max; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 3563 | |
| 3564 | /* default minimum is TLSV12, */ |
| 3565 | if (!min) { |
| 3566 | if (!max || (max >= default_min_ver)) { |
| 3567 | min = default_min_ver; |
| 3568 | } else { |
| 3569 | 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). " |
| 3570 | "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n", |
| 3571 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name); |
| 3572 | min = max; |
| 3573 | } |
| 3574 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3575 | /* 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] | 3576 | if (min) |
| 3577 | flags |= (methodVersions[min].flag - 1); |
| 3578 | if (max) |
| 3579 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3580 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3581 | min = max = CONF_TLSV_NONE; |
| 3582 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3583 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3584 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3585 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3586 | if (min) { |
| 3587 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3588 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3589 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3590 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3591 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3592 | hole = 0; |
| 3593 | } |
| 3594 | max = i; |
| 3595 | } |
| 3596 | else { |
| 3597 | min = max = i; |
| 3598 | } |
| 3599 | } |
| 3600 | else { |
| 3601 | if (min) |
| 3602 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3603 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3604 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3605 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3606 | 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] | 3607 | cfgerr += 1; |
| 3608 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3609 | /* save real min/max in bind_conf */ |
| 3610 | conf_ssl_methods->min = min; |
| 3611 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3612 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3613 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3614 | /* 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] | 3615 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3616 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3617 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3618 | else |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 3619 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) { |
| 3620 | /* clear every version flags in case SSL_CTX_new() |
| 3621 | * returns an SSL_CTX with disabled versions */ |
| 3622 | SSL_CTX_clear_options(ctx, methodVersions[i].option); |
| 3623 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3624 | if (flags & methodVersions[i].flag) |
| 3625 | options |= methodVersions[i].option; |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 3626 | |
| 3627 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3628 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3629 | /* 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] | 3630 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3631 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3632 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3633 | |
| 3634 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3635 | options |= SSL_OP_NO_TICKET; |
| 3636 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3637 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 3638 | |
| 3639 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 3640 | options |= SSL_OP_NO_RENEGOTIATION; |
| 3641 | #endif |
| 3642 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3643 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3644 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3645 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3646 | if (global_ssl.async) |
| 3647 | mode |= SSL_MODE_ASYNC; |
| 3648 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3649 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3650 | if (global_ssl.life_time) |
| 3651 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3652 | |
| 3653 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3654 | #ifdef OPENSSL_IS_BORINGSSL |
| 3655 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3656 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Ilya Shipitsin | e9ff899 | 2020-01-19 12:20:14 +0500 | [diff] [blame] | 3657 | #elif defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 3658 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 3659 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3660 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3661 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3662 | #else |
| 3663 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3664 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3665 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3666 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3667 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3668 | } |
| 3669 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3670 | |
| 3671 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3672 | { |
| 3673 | if (first == block) { |
| 3674 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3675 | if (first->len > 0) |
| 3676 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | /* return first block from sh_ssl_sess */ |
| 3681 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3682 | { |
| 3683 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3684 | |
| 3685 | } |
| 3686 | |
| 3687 | /* store a session into the cache |
| 3688 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3689 | * data: asn1 encoded session |
| 3690 | * data_len: asn1 encoded session length |
| 3691 | * Returns 1 id session was stored (else 0) |
| 3692 | */ |
| 3693 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3694 | { |
| 3695 | struct shared_block *first; |
| 3696 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3697 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3698 | 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] | 3699 | if (!first) { |
| 3700 | /* Could not retrieve enough free blocks to store that session */ |
| 3701 | return 0; |
| 3702 | } |
| 3703 | |
| 3704 | /* STORE the key in the first elem */ |
| 3705 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3706 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3707 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3708 | |
| 3709 | /* it returns the already existing node |
| 3710 | or current node if none, never returns null */ |
| 3711 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 3712 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 3713 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 3714 | /* release the reserved row */ |
| 3715 | shctx_row_dec_hot(ssl_shctx, first); |
| 3716 | /* replace the previous session already in the tree */ |
| 3717 | sh_ssl_sess = oldsh_ssl_sess; |
| 3718 | /* ignore the previous session data, only use the header */ |
| 3719 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 3720 | shctx_row_inc_hot(ssl_shctx, first); |
| 3721 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3722 | } |
| 3723 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3724 | 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] | 3725 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3726 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 3727 | } |
| 3728 | |
| 3729 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3730 | |
| 3731 | return 1; |
| 3732 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3733 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3734 | /* SSL callback used when a new session is created while connecting to a server */ |
| 3735 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 3736 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 3737 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3738 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3739 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 3740 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3741 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3742 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 3743 | int len; |
| 3744 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3745 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3746 | len = i2d_SSL_SESSION(sess, NULL); |
| 3747 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 3748 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 3749 | } else { |
| 3750 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3751 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 3752 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 3753 | } |
| 3754 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 3755 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 3756 | &ptr); |
| 3757 | } |
| 3758 | } else { |
| 3759 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 3760 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 3761 | } |
| 3762 | |
| 3763 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 3764 | } |
| 3765 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 3766 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3767 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3768 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3769 | { |
| 3770 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 3771 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 3772 | unsigned char *p; |
| 3773 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3774 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3775 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3776 | |
| 3777 | /* 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] | 3778 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3779 | * note: SSL_SESSION_set1_id is using |
| 3780 | * a memcpy so we need to use a different pointer |
| 3781 | * than sid_data or sid_ctx_data to avoid valgrind |
| 3782 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3783 | */ |
| 3784 | |
| 3785 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3786 | |
| 3787 | /* copy value in an other buffer */ |
| 3788 | memcpy(encid, sid_data, sid_length); |
| 3789 | |
| 3790 | /* pad with 0 */ |
| 3791 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 3792 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 3793 | |
| 3794 | /* force length to zero to avoid ASN1 encoding */ |
| 3795 | SSL_SESSION_set1_id(sess, encid, 0); |
| 3796 | |
| 3797 | /* force length to zero to avoid ASN1 encoding */ |
| 3798 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3799 | |
| 3800 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 3801 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 3802 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 3803 | goto err; |
| 3804 | |
| 3805 | p = encsess; |
| 3806 | |
| 3807 | /* process ASN1 session encoding before the lock */ |
| 3808 | i2d_SSL_SESSION(sess, &p); |
| 3809 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3810 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3811 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3812 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3813 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3814 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3815 | err: |
| 3816 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 3817 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 3818 | 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] | 3819 | |
| 3820 | return 0; /* do not increment session reference count */ |
| 3821 | } |
| 3822 | |
| 3823 | /* 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] | 3824 | 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] | 3825 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3826 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3827 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 3828 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3829 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3830 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3831 | |
| 3832 | global.shctx_lookups++; |
| 3833 | |
| 3834 | /* allow the session to be freed automatically by openssl */ |
| 3835 | *do_copy = 0; |
| 3836 | |
| 3837 | /* tree key is zeros padded sessionid */ |
| 3838 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3839 | memcpy(tmpkey, key, key_len); |
| 3840 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 3841 | key = tmpkey; |
| 3842 | } |
| 3843 | |
| 3844 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3845 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3846 | |
| 3847 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3848 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 3849 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3850 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3851 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3852 | global.shctx_misses++; |
| 3853 | return NULL; |
| 3854 | } |
| 3855 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3856 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 3857 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3858 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3859 | 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] | 3860 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3861 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3862 | |
| 3863 | /* decode ASN1 session */ |
| 3864 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3865 | 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] | 3866 | /* Reset session id and session id contenxt */ |
| 3867 | if (sess) { |
| 3868 | SSL_SESSION_set1_id(sess, key, key_len); |
| 3869 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 3870 | } |
| 3871 | |
| 3872 | return sess; |
| 3873 | } |
| 3874 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3875 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3876 | /* 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] | 3877 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3878 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3879 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3880 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 3881 | unsigned int sid_length; |
| 3882 | const unsigned char *sid_data; |
| 3883 | (void)ctx; |
| 3884 | |
| 3885 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 3886 | /* tree key is zeros padded sessionid */ |
| 3887 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 3888 | memcpy(tmpkey, sid_data, sid_length); |
| 3889 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 3890 | sid_data = tmpkey; |
| 3891 | } |
| 3892 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3893 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3894 | |
| 3895 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3896 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 3897 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3898 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3899 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 3903 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | /* Set session cache mode to server and disable openssl internal cache. |
| 3907 | * Set shared cache callbacks on an ssl context. |
| 3908 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3909 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3910 | { |
| 3911 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 3912 | |
| 3913 | if (!ssl_shctx) { |
| 3914 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 3915 | return; |
| 3916 | } |
| 3917 | |
| 3918 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 3919 | SSL_SESS_CACHE_NO_INTERNAL | |
| 3920 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 3921 | |
| 3922 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3923 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 3924 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 3925 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 3926 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 3927 | |
| 3928 | /* |
| 3929 | * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format |
| 3930 | * |
| 3931 | * The format is: |
| 3932 | * * <Label> <space> <ClientRandom> <space> <Secret> |
| 3933 | * We only need to copy the secret as there is a sample fetch for the ClientRandom |
| 3934 | */ |
| 3935 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 3936 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 3937 | void SSL_CTX_keylog(const SSL *ssl, const char *line) |
| 3938 | { |
| 3939 | struct ssl_keylog *keylog; |
| 3940 | char *lastarg = NULL; |
| 3941 | char *dst = NULL; |
| 3942 | |
| 3943 | keylog = SSL_get_ex_data(ssl, ssl_keylog_index); |
| 3944 | if (!keylog) |
| 3945 | return; |
| 3946 | |
| 3947 | lastarg = strrchr(line, ' '); |
| 3948 | if (lastarg == NULL || ++lastarg == NULL) |
| 3949 | return; |
| 3950 | |
| 3951 | dst = pool_alloc(pool_head_ssl_keylog_str); |
| 3952 | if (!dst) |
| 3953 | return; |
| 3954 | |
| 3955 | strncpy(dst, lastarg, SSL_KEYLOG_MAX_SECRET_SIZE-1); |
| 3956 | dst[SSL_KEYLOG_MAX_SECRET_SIZE-1] = '\0'; |
| 3957 | |
| 3958 | if (strncmp(line, "CLIENT_RANDOM ", strlen("CLIENT RANDOM ")) == 0) { |
| 3959 | if (keylog->client_random) |
| 3960 | goto error; |
| 3961 | keylog->client_random = dst; |
| 3962 | |
| 3963 | } else if (strncmp(line, "CLIENT_EARLY_TRAFFIC_SECRET ", strlen("CLIENT_EARLY_TRAFFIC_SECRET ")) == 0) { |
| 3964 | if (keylog->client_early_traffic_secret) |
| 3965 | goto error; |
| 3966 | keylog->client_early_traffic_secret = dst; |
| 3967 | |
| 3968 | } else if (strncmp(line, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ", strlen("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 3969 | if(keylog->client_handshake_traffic_secret) |
| 3970 | goto error; |
| 3971 | keylog->client_handshake_traffic_secret = dst; |
| 3972 | |
| 3973 | } else if (strncmp(line, "SERVER_HANDSHAKE_TRAFFIC_SECRET ", strlen("SERVER_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 3974 | if (keylog->server_handshake_traffic_secret) |
| 3975 | goto error; |
| 3976 | keylog->server_handshake_traffic_secret = dst; |
| 3977 | |
| 3978 | } else if (strncmp(line, "CLIENT_TRAFFIC_SECRET_0 ", strlen("CLIENT_TRAFFIC_SECRET_0 ")) == 0) { |
| 3979 | if (keylog->client_traffic_secret_0) |
| 3980 | goto error; |
| 3981 | keylog->client_traffic_secret_0 = dst; |
| 3982 | |
| 3983 | } else if (strncmp(line, "SERVER_TRAFFIC_SECRET_0 ", strlen("SERVER_TRAFFIC_SECRET_0 ")) == 0) { |
| 3984 | if (keylog->server_traffic_secret_0) |
| 3985 | goto error; |
| 3986 | keylog->server_traffic_secret_0 = dst; |
| 3987 | |
| 3988 | } else if (strncmp(line, "EARLY_EXPORTER_SECRET ", strlen("EARLY_EXPORTER_SECRET ")) == 0) { |
| 3989 | if (keylog->early_exporter_secret) |
| 3990 | goto error; |
| 3991 | keylog->early_exporter_secret = dst; |
| 3992 | |
| 3993 | } else if (strncmp(line, "EXPORTER_SECRET ", strlen("EXPORTER_SECRET ")) == 0) { |
| 3994 | if (keylog->exporter_secret) |
| 3995 | goto error; |
| 3996 | keylog->exporter_secret = dst; |
| 3997 | } else { |
| 3998 | goto error; |
| 3999 | } |
| 4000 | |
| 4001 | return; |
| 4002 | |
| 4003 | error: |
| 4004 | pool_free(pool_head_ssl_keylog_str, dst); |
| 4005 | |
| 4006 | return; |
| 4007 | } |
| 4008 | #endif |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4009 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4010 | /* |
| 4011 | * This function applies the SSL configuration on a SSL_CTX |
| 4012 | * It returns an error code and fills the <err> buffer |
| 4013 | */ |
| 4014 | 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] | 4015 | { |
| 4016 | struct proxy *curproxy = bind_conf->frontend; |
| 4017 | int cfgerr = 0; |
| 4018 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4019 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4020 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4021 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4022 | const char *conf_ciphersuites; |
| 4023 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4024 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4025 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4026 | if (ssl_conf) { |
| 4027 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4028 | int i, min, max; |
| 4029 | int flags = MC_SSL_O_ALL; |
| 4030 | |
| 4031 | /* 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] | 4032 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4033 | 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] | 4034 | if (min) |
| 4035 | flags |= (methodVersions[min].flag - 1); |
| 4036 | if (max) |
| 4037 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4038 | min = max = CONF_TLSV_NONE; |
| 4039 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4040 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4041 | if (min) |
| 4042 | max = i; |
| 4043 | else |
| 4044 | min = max = i; |
| 4045 | } |
| 4046 | /* save real min/max */ |
| 4047 | conf_ssl_methods->min = min; |
| 4048 | conf_ssl_methods->max = max; |
| 4049 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4050 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4051 | 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] | 4052 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4053 | } |
| 4054 | } |
| 4055 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4056 | 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] | 4057 | case SSL_SOCK_VERIFY_NONE: |
| 4058 | verify = SSL_VERIFY_NONE; |
| 4059 | break; |
| 4060 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4061 | verify = SSL_VERIFY_PEER; |
| 4062 | break; |
| 4063 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4064 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4065 | break; |
| 4066 | } |
| 4067 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4068 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4069 | 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] | 4070 | 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] | 4071 | 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] | 4072 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4073 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4074 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4075 | 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] | 4076 | 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] | 4077 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4078 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4079 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 4080 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 4081 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4082 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4083 | } |
| 4084 | 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] | 4085 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 4086 | 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] | 4087 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4088 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4089 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4090 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4091 | 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] | 4092 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4093 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4094 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4095 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4096 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4097 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4098 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4099 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4100 | 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] | 4101 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4102 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4103 | else { |
| 4104 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4105 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4106 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4107 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4108 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4109 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4110 | #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] | 4111 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4112 | 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] | 4113 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4114 | 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] | 4115 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4116 | } |
| 4117 | } |
| 4118 | #endif |
| 4119 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4120 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4121 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4122 | if (conf_ciphers && |
| 4123 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4124 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4125 | 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] | 4126 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4127 | } |
| 4128 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4129 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4130 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4131 | if (conf_ciphersuites && |
| 4132 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4133 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4134 | 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] | 4135 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4136 | } |
| 4137 | #endif |
| 4138 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4139 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4140 | /* If tune.ssl.default-dh-param has not been set, |
| 4141 | neither has ssl-default-dh-file and no static DH |
| 4142 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4143 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4144 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4145 | (ssl_dh_ptr_index == -1 || |
| 4146 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Willy Tarreau | 3ba77d2 | 2020-05-08 09:31:18 +0200 | [diff] [blame] | 4147 | /* default to dh-param 2048 */ |
| 4148 | global_ssl.default_dh_param = 2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4149 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4150 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4151 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4152 | if (local_dh_1024 == NULL) { |
| 4153 | local_dh_1024 = ssl_get_dh_1024(); |
| 4154 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4155 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4156 | if (local_dh_2048 == NULL) { |
| 4157 | local_dh_2048 = ssl_get_dh_2048(); |
| 4158 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4159 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4160 | if (local_dh_4096 == NULL) { |
| 4161 | local_dh_4096 = ssl_get_dh_4096(); |
| 4162 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4163 | } |
| 4164 | } |
| 4165 | } |
| 4166 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4167 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4168 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Ilya Shipitsin | bcc2560 | 2021-02-08 16:55:06 +0500 | [diff] [blame] | 4169 | #ifdef SSL_CTRL_SET_MSG_CALLBACK |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4170 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4171 | #endif |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 4172 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4173 | SSL_CTX_set_keylog_callback(ctx, SSL_CTX_keylog); |
| 4174 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4175 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4176 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4177 | ssl_conf_cur = NULL; |
| 4178 | if (ssl_conf && ssl_conf->npn_str) |
| 4179 | ssl_conf_cur = ssl_conf; |
| 4180 | else if (bind_conf->ssl_conf.npn_str) |
| 4181 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4182 | if (ssl_conf_cur) |
| 4183 | 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] | 4184 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4185 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4186 | ssl_conf_cur = NULL; |
| 4187 | if (ssl_conf && ssl_conf->alpn_str) |
| 4188 | ssl_conf_cur = ssl_conf; |
| 4189 | else if (bind_conf->ssl_conf.alpn_str) |
| 4190 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4191 | if (ssl_conf_cur) |
| 4192 | 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] | 4193 | #endif |
Ilya Shipitsin | 0aa8c29 | 2020-11-04 00:39:07 +0500 | [diff] [blame] | 4194 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4195 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4196 | if (conf_curves) { |
| 4197 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4198 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4199 | 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] | 4200 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4201 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4202 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4203 | } |
| 4204 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4205 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4206 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4207 | int i; |
| 4208 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4209 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4210 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4211 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4212 | NULL); |
| 4213 | |
| 4214 | if (ecdhe == NULL) { |
Eric Salama | 3c8bde8 | 2019-11-20 11:33:40 +0100 | [diff] [blame] | 4215 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4216 | return cfgerr; |
| 4217 | } |
| 4218 | #else |
| 4219 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4220 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4221 | ECDHE_DEFAULT_CURVE); |
| 4222 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4223 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4224 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4225 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4226 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4227 | 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] | 4228 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4229 | } |
| 4230 | else { |
| 4231 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4232 | EC_KEY_free(ecdh); |
| 4233 | } |
| 4234 | } |
| 4235 | #endif |
| 4236 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4237 | return cfgerr; |
| 4238 | } |
| 4239 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4240 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4241 | { |
| 4242 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4243 | size_t prefixlen, suffixlen; |
| 4244 | |
| 4245 | /* Trivial case */ |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4246 | if (strcasecmp(pattern, hostname) == 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4247 | return 1; |
| 4248 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4249 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4250 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4251 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4252 | pattern_wildcard = NULL; |
| 4253 | pattern_left_label_end = pattern; |
| 4254 | while (*pattern_left_label_end != '.') { |
| 4255 | switch (*pattern_left_label_end) { |
| 4256 | case 0: |
| 4257 | /* End of label not found */ |
| 4258 | return 0; |
| 4259 | case '*': |
| 4260 | /* If there is more than one wildcards */ |
| 4261 | if (pattern_wildcard) |
| 4262 | return 0; |
| 4263 | pattern_wildcard = pattern_left_label_end; |
| 4264 | break; |
| 4265 | } |
| 4266 | pattern_left_label_end++; |
| 4267 | } |
| 4268 | |
| 4269 | /* If it's not trivial and there is no wildcard, it can't |
| 4270 | * match */ |
| 4271 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4272 | return 0; |
| 4273 | |
| 4274 | /* Make sure all labels match except the leftmost */ |
| 4275 | hostname_left_label_end = strchr(hostname, '.'); |
| 4276 | if (!hostname_left_label_end |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4277 | || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4278 | return 0; |
| 4279 | |
| 4280 | /* Make sure the leftmost label of the hostname is long enough |
| 4281 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4282 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4283 | return 0; |
| 4284 | |
| 4285 | /* Finally compare the string on either side of the |
| 4286 | * wildcard */ |
| 4287 | prefixlen = pattern_wildcard - pattern; |
| 4288 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4289 | if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0)) |
| 4290 | || (suffixlen && (strncasecmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4291 | return 0; |
| 4292 | |
| 4293 | return 1; |
| 4294 | } |
| 4295 | |
| 4296 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4297 | { |
| 4298 | SSL *ssl; |
| 4299 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4300 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4301 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4302 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4303 | |
| 4304 | int depth; |
| 4305 | X509 *cert; |
| 4306 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4307 | int i; |
| 4308 | X509_NAME *cert_subject; |
| 4309 | char *str; |
| 4310 | |
| 4311 | if (ok == 0) |
| 4312 | return ok; |
| 4313 | |
| 4314 | 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] | 4315 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4316 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4317 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4318 | /* We're checking if the provided hostnames match the desired one. The |
| 4319 | * desired hostname comes from the SNI we presented if any, or if not |
| 4320 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4321 | * directive. If neither is set, we don't care about the name so the |
| 4322 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4323 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4324 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4325 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4326 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4327 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4328 | if (!servername) |
| 4329 | return ok; |
| 4330 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4331 | |
| 4332 | /* We only need to verify the CN on the actual server cert, |
| 4333 | * not the indirect CAs */ |
| 4334 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4335 | if (depth != 0) |
| 4336 | return ok; |
| 4337 | |
| 4338 | /* At this point, the cert is *not* OK unless we can find a |
| 4339 | * hostname match */ |
| 4340 | ok = 0; |
| 4341 | |
| 4342 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4343 | /* It seems like this might happen if verify peer isn't set */ |
| 4344 | if (!cert) |
| 4345 | return ok; |
| 4346 | |
| 4347 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4348 | if (alt_names) { |
| 4349 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4350 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4351 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4352 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4353 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4354 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4355 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4356 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4357 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4358 | OPENSSL_free(str); |
| 4359 | } |
| 4360 | } |
| 4361 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4362 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4363 | } |
| 4364 | |
| 4365 | cert_subject = X509_get_subject_name(cert); |
| 4366 | i = -1; |
| 4367 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4368 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4369 | ASN1_STRING *value; |
| 4370 | value = X509_NAME_ENTRY_get_data(entry); |
| 4371 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4372 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4373 | OPENSSL_free(str); |
| 4374 | } |
| 4375 | } |
| 4376 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4377 | /* report the mismatch and indicate if SNI was used or not */ |
| 4378 | if (!ok && !conn->err_code) |
| 4379 | 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] | 4380 | return ok; |
| 4381 | } |
| 4382 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4383 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4384 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4385 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4386 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4387 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4388 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4389 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4390 | SSL_OP_NO_SSLv2 | |
| 4391 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4392 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4393 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4394 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4395 | SSL_MODE_RELEASE_BUFFERS | |
| 4396 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4397 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4398 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4399 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4400 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4401 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4402 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4403 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4404 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4405 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4406 | cfgerr++; |
| 4407 | } |
| 4408 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4409 | /* Automatic memory computations need to know we use SSL there */ |
| 4410 | global.ssl_used_backend = 1; |
| 4411 | |
| 4412 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4413 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4414 | 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] | 4415 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4416 | curproxy->id, srv->id, |
| 4417 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4418 | cfgerr++; |
| 4419 | return cfgerr; |
| 4420 | } |
| 4421 | } |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 4422 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4423 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4424 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4425 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4426 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4427 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4428 | proxy_type_str(curproxy), curproxy->id, |
| 4429 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4430 | cfgerr++; |
| 4431 | return cfgerr; |
| 4432 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4433 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4434 | 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] | 4435 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4436 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4437 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4438 | else |
| 4439 | flags = conf_ssl_methods->flags; |
| 4440 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4441 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4442 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4443 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4444 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4445 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4446 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4447 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4448 | min = max = CONF_TLSV_NONE; |
| 4449 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4450 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4451 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4452 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4453 | if (min) { |
| 4454 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4455 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4456 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4457 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4458 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4459 | hole = 0; |
| 4460 | } |
| 4461 | max = i; |
| 4462 | } |
| 4463 | else { |
| 4464 | min = max = i; |
| 4465 | } |
| 4466 | } |
| 4467 | else { |
| 4468 | if (min) |
| 4469 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4470 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4471 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4472 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4473 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4474 | cfgerr += 1; |
| 4475 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4476 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4477 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4478 | /* 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] | 4479 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4480 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4481 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4482 | else |
| 4483 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4484 | if (flags & methodVersions[i].flag) |
| 4485 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4486 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4487 | /* 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] | 4488 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4489 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4490 | #endif |
| 4491 | |
| 4492 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4493 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4494 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4495 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4496 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4497 | if (global_ssl.async) |
| 4498 | mode |= SSL_MODE_ASYNC; |
| 4499 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4500 | SSL_CTX_set_mode(ctx, mode); |
| 4501 | srv->ssl_ctx.ctx = ctx; |
| 4502 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4503 | if (srv->ssl_ctx.client_crt) { |
| 4504 | 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] | 4505 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4506 | proxy_type_str(curproxy), curproxy->id, |
| 4507 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4508 | cfgerr++; |
| 4509 | } |
| 4510 | 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] | 4511 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4512 | proxy_type_str(curproxy), curproxy->id, |
| 4513 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4514 | cfgerr++; |
| 4515 | } |
| 4516 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4517 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4518 | proxy_type_str(curproxy), curproxy->id, |
| 4519 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4520 | cfgerr++; |
| 4521 | } |
| 4522 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4523 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4524 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4525 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4526 | switch (srv->ssl_ctx.verify) { |
| 4527 | case SSL_SOCK_VERIFY_NONE: |
| 4528 | verify = SSL_VERIFY_NONE; |
| 4529 | break; |
| 4530 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4531 | verify = SSL_VERIFY_PEER; |
| 4532 | break; |
| 4533 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4534 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4535 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4536 | (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] | 4537 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4538 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4539 | /* set CAfile to verify */ |
| 4540 | if (!ssl_set_verify_locations_file(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file)) { |
| 4541 | 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] | 4542 | curproxy->id, srv->id, |
| 4543 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4544 | cfgerr++; |
| 4545 | } |
| 4546 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4547 | else { |
| 4548 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4549 | 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", |
| 4550 | curproxy->id, srv->id, |
| 4551 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4552 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4553 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4554 | curproxy->id, srv->id, |
| 4555 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4556 | cfgerr++; |
| 4557 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4558 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4559 | if (srv->ssl_ctx.crl_file) { |
| 4560 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4561 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4562 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4563 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4564 | curproxy->id, srv->id, |
| 4565 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4566 | cfgerr++; |
| 4567 | } |
| 4568 | else { |
| 4569 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4570 | } |
| 4571 | } |
| 4572 | #endif |
| 4573 | } |
| 4574 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4575 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4576 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4577 | 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] | 4578 | if (srv->ssl_ctx.ciphers && |
| 4579 | !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] | 4580 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4581 | curproxy->id, srv->id, |
| 4582 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4583 | cfgerr++; |
| 4584 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4585 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4586 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4587 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 4588 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4589 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 4590 | curproxy->id, srv->id, |
| 4591 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 4592 | cfgerr++; |
| 4593 | } |
| 4594 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4595 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 4596 | if (srv->ssl_ctx.npn_str) |
| 4597 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 4598 | #endif |
| 4599 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4600 | if (srv->ssl_ctx.alpn_str) |
| 4601 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 4602 | #endif |
| 4603 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4604 | |
| 4605 | return cfgerr; |
| 4606 | } |
| 4607 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4608 | /* 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] | 4609 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4610 | * encountered. |
| 4611 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4612 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4613 | { |
| 4614 | struct ebmb_node *node; |
| 4615 | struct sni_ctx *sni; |
| 4616 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4617 | int errcode = 0; |
| 4618 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4619 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4620 | /* Automatic memory computations need to know we use SSL there */ |
| 4621 | global.ssl_used_frontend = 1; |
| 4622 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4623 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4624 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4625 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4626 | err++; |
| 4627 | } |
| 4628 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4629 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4630 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4631 | /* It should not be necessary to call this function, but it's |
| 4632 | necessary first to check and move all initialisation related |
| 4633 | to initial_ctx in ssl_sock_initial_ctx. */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4634 | 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] | 4635 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4636 | if (bind_conf->default_ctx) |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4637 | 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] | 4638 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4639 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4640 | while (node) { |
| 4641 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4642 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4643 | /* only initialize the CTX on its first occurrence and |
| 4644 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4645 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4646 | node = ebmb_next(node); |
| 4647 | } |
| 4648 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4649 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4650 | while (node) { |
| 4651 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4652 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4653 | /* only initialize the CTX on its first occurrence and |
| 4654 | if it is not the default_ctx */ |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4655 | errcode |= ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx, &errmsg); |
| 4656 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4657 | node = ebmb_next(node); |
| 4658 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4659 | |
| 4660 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 4661 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4662 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 4663 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4664 | err++; |
| 4665 | } |
| 4666 | |
| 4667 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4668 | return err; |
| 4669 | } |
| 4670 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4671 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4672 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4673 | * alerts are directly emitted since the rest of the stack does it below. |
| 4674 | */ |
| 4675 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4676 | { |
| 4677 | struct proxy *px = bind_conf->frontend; |
| 4678 | int alloc_ctx; |
| 4679 | int err; |
| 4680 | |
| 4681 | if (!bind_conf->is_ssl) { |
| 4682 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4683 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4684 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4685 | } |
| 4686 | return 0; |
| 4687 | } |
| 4688 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4689 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4690 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4691 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4692 | } |
| 4693 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4694 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4695 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4696 | return -1; |
| 4697 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4698 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4699 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4700 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 4701 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4702 | sizeof(*sh_ssl_sess_tree), |
| 4703 | ((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] | 4704 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4705 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4706 | 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"); |
| 4707 | else |
| 4708 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4709 | return -1; |
| 4710 | } |
| 4711 | /* free block callback */ |
| 4712 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4713 | /* init the root tree within the extra space */ |
| 4714 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4715 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4716 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4717 | err = 0; |
| 4718 | /* initialize all certificate contexts */ |
| 4719 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4720 | |
| 4721 | /* initialize CA variables if the certificates generation is enabled */ |
| 4722 | err += ssl_sock_load_ca(bind_conf); |
| 4723 | |
| 4724 | return -err; |
| 4725 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4726 | |
| 4727 | /* release ssl context allocated for servers. */ |
| 4728 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4729 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4730 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4731 | if (srv->ssl_ctx.alpn_str) |
| 4732 | free(srv->ssl_ctx.alpn_str); |
| 4733 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 4734 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4735 | if (srv->ssl_ctx.npn_str) |
| 4736 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 4737 | #endif |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 4738 | if (srv->ssl_ctx.reused_sess) { |
| 4739 | int i; |
| 4740 | |
| 4741 | for (i = 0; i < global.nbthread; i++) |
| 4742 | free(srv->ssl_ctx.reused_sess[i].ptr); |
| 4743 | free(srv->ssl_ctx.reused_sess); |
| 4744 | } |
| 4745 | |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4746 | if (srv->ssl_ctx.ctx) |
| 4747 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4748 | } |
| 4749 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4750 | /* 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] | 4751 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4752 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4753 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4754 | { |
| 4755 | struct ebmb_node *node, *back; |
| 4756 | struct sni_ctx *sni; |
| 4757 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4758 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4759 | while (node) { |
| 4760 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4761 | back = ebmb_next(node); |
| 4762 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4763 | SSL_CTX_free(sni->ctx); |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 4764 | LIST_DEL(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4765 | free(sni); |
| 4766 | node = back; |
| 4767 | } |
| 4768 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4769 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4770 | while (node) { |
| 4771 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4772 | back = ebmb_next(node); |
| 4773 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4774 | SSL_CTX_free(sni->ctx); |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 4775 | LIST_DEL(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4776 | free(sni); |
| 4777 | node = back; |
| 4778 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 4779 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4780 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4781 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4782 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4783 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4784 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4785 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 4786 | |
| 4787 | |
| 4788 | void ssl_sock_deinit() |
| 4789 | { |
| 4790 | crtlist_deinit(); /* must be free'd before the ckchs */ |
| 4791 | ckch_deinit(); |
| 4792 | } |
| 4793 | REGISTER_POST_DEINIT(ssl_sock_deinit); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4794 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4795 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 4796 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 4797 | { |
| 4798 | ssl_sock_free_ca(bind_conf); |
| 4799 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4800 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4801 | free(bind_conf->ca_sign_file); |
| 4802 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 4803 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4804 | free(bind_conf->keys_ref->filename); |
| 4805 | free(bind_conf->keys_ref->tlskeys); |
| 4806 | LIST_DEL(&bind_conf->keys_ref->list); |
| 4807 | free(bind_conf->keys_ref); |
| 4808 | } |
| 4809 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4810 | bind_conf->ca_sign_pass = NULL; |
| 4811 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4812 | } |
| 4813 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4814 | /* Load CA cert file and private key used to generate certificates */ |
| 4815 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4816 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4817 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4818 | struct proxy *px = bind_conf->frontend; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4819 | struct cert_key_and_chain *ckch = NULL; |
| 4820 | int ret = 0; |
| 4821 | char *err = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4822 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 4823 | if (!bind_conf->generate_certs) |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4824 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4825 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 4826 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4827 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4828 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4829 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4830 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4831 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 4832 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 4833 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4834 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4835 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 4836 | "no CA certificate File configured at [%s:%d].\n", |
| 4837 | px->id, bind_conf->file, bind_conf->line); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4838 | goto failed; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4839 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4840 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4841 | /* Allocate cert structure */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 4842 | ckch = calloc(1, sizeof(*ckch)); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4843 | if (!ckch) { |
| 4844 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain allocation failure\n", |
| 4845 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 4846 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4847 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4848 | |
| 4849 | /* Try to parse file */ |
| 4850 | if (ssl_sock_load_files_into_ckch(bind_conf->ca_sign_file, ckch, &err)) { |
| 4851 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain loading failed: %s\n", |
| 4852 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line, err); |
| 4853 | if (err) free(err); |
| 4854 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4855 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4856 | |
| 4857 | /* Fail if missing cert or pkey */ |
| 4858 | if ((!ckch->cert) || (!ckch->key)) { |
| 4859 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain missing certificate or private key\n", |
| 4860 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 4861 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4862 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4863 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4864 | /* Final assignment to bind */ |
| 4865 | bind_conf->ca_sign_ckch = ckch; |
| 4866 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4867 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4868 | failed: |
| 4869 | if (ckch) { |
| 4870 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 4871 | free(ckch); |
| 4872 | } |
| 4873 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 4874 | bind_conf->generate_certs = 0; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4875 | ret++; |
| 4876 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4877 | } |
| 4878 | |
| 4879 | /* Release CA cert and private key used to generate certificated */ |
| 4880 | void |
| 4881 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 4882 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 4883 | if (bind_conf->ca_sign_ckch) { |
| 4884 | ssl_sock_free_cert_key_and_chain_contents(bind_conf->ca_sign_ckch); |
| 4885 | free(bind_conf->ca_sign_ckch); |
| 4886 | bind_conf->ca_sign_ckch = NULL; |
| 4887 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 4888 | } |
| 4889 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4890 | /* |
| 4891 | * This function is called if SSL * context is not yet allocated. The function |
| 4892 | * is designed to be called before any other data-layer operation and sets the |
| 4893 | * handshake flag on the connection. It is safe to call it multiple times. |
| 4894 | * It returns 0 on success and -1 in error case. |
| 4895 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4896 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4897 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4898 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4899 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 4900 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4901 | return 0; |
| 4902 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 4903 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 4904 | return 0; |
| 4905 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4906 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 4907 | if (!ctx) { |
| 4908 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4909 | return -1; |
| 4910 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4911 | ctx->wait_event.tasklet = tasklet_new(); |
| 4912 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4913 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 4914 | pool_free(ssl_sock_ctx_pool, ctx); |
| 4915 | return -1; |
| 4916 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 4917 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 4918 | ctx->wait_event.tasklet->context = ctx; |
Willy Tarreau | b421f9d | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 4919 | 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] | 4920 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 4921 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 4922 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4923 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 4924 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 4925 | ctx->xprt_st = 0; |
| 4926 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4927 | |
| 4928 | /* Only work with sockets for now, this should be adapted when we'll |
| 4929 | * add QUIC support. |
| 4930 | */ |
| 4931 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 4932 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4933 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 4934 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 4935 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4936 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4937 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 4938 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 4939 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4940 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 4941 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4942 | /* If it is in client mode initiate SSL session |
| 4943 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 4944 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4945 | int may_retry = 1; |
| 4946 | |
| 4947 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4948 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4949 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 4950 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4951 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4952 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4953 | goto retry_connect; |
| 4954 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4955 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4956 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 4957 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4958 | ctx->bio = BIO_new(ha_meth); |
| 4959 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 4960 | SSL_free(ctx->ssl); |
| 4961 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4962 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4963 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4964 | goto retry_connect; |
| 4965 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4966 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4967 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4968 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4969 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 4970 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4971 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4972 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4973 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 4974 | SSL_free(ctx->ssl); |
| 4975 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4976 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4977 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 4978 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 4979 | goto retry_connect; |
| 4980 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4981 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4982 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4983 | } |
| 4984 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4985 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4986 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 4987 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 4988 | 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] | 4989 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4990 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4991 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 4992 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4993 | } else if (sess) { |
| 4994 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 4995 | } |
| 4996 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4997 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4998 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 4999 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5000 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5001 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5002 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5003 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5004 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5005 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5006 | return 0; |
| 5007 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5008 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5009 | int may_retry = 1; |
| 5010 | |
| 5011 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5012 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5013 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 5014 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5015 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5016 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5017 | goto retry_accept; |
| 5018 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5019 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5020 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5021 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5022 | ctx->bio = BIO_new(ha_meth); |
| 5023 | if (!ctx->bio) { |
Olivier Houchard | efe5e8e | 2020-01-24 15:17:38 +0100 | [diff] [blame] | 5024 | SSL_free(ctx->ssl); |
| 5025 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5026 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5027 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5028 | goto retry_accept; |
| 5029 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5030 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5031 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5032 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5033 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5034 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5035 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5036 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5037 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5038 | SSL_free(ctx->ssl); |
| 5039 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5040 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5041 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5042 | goto retry_accept; |
| 5043 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5044 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5045 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5046 | } |
| 5047 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5048 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 5049 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) { |
| 5050 | b_alloc(&ctx->early_buf); |
| 5051 | SSL_set_max_early_data(ctx->ssl, |
| 5052 | /* Only allow early data if we managed to allocate |
| 5053 | * a buffer. |
| 5054 | */ |
| 5055 | (!b_is_null(&ctx->early_buf)) ? |
| 5056 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 5057 | } |
| 5058 | #endif |
| 5059 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5060 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5061 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5062 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5063 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5064 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Willy Tarreau | 5c6176d | 2021-02-05 10:24:54 +0100 | [diff] [blame] | 5065 | if (__objt_listener(conn->target)->bind_conf->ssl_conf.early_data) |
Willy Tarreau | 5930fb2 | 2021-02-03 11:21:38 +0100 | [diff] [blame] | 5066 | conn->flags |= CO_FL_EARLY_SSL_HS; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5067 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5068 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5069 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5070 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5071 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5072 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5073 | tasklet_wakeup(ctx->wait_event.tasklet); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5074 | return 0; |
| 5075 | } |
| 5076 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5077 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5078 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5079 | if (ctx && ctx->wait_event.tasklet) |
| 5080 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5081 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5082 | return -1; |
| 5083 | } |
| 5084 | |
| 5085 | |
| 5086 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5087 | * updates the FD status if it wants some polling before being called again. |
| 5088 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5089 | * otherwise it returns non-zero and removes itself from the connection's |
| 5090 | * flags (the bit is provided in <flag> by the caller). |
| 5091 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5092 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5093 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5094 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5095 | int ret; |
Willy Tarreau | d287140 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5096 | socklen_t lskerr; |
| 5097 | int skerr; |
| 5098 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5099 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5100 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5101 | return 0; |
| 5102 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5103 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5104 | goto out_error; |
| 5105 | |
Willy Tarreau | d287140 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5106 | /* don't start calculating a handshake on a dead connection */ |
| 5107 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) |
| 5108 | goto out_error; |
| 5109 | |
| 5110 | /* FIXME/WT: for now we don't have a clear way to inspect the connection |
| 5111 | * status from the lower layers, so let's check the FD directly. Ideally |
| 5112 | * the xprt layers should provide some status indicating their knowledge |
| 5113 | * of shutdowns or error. |
| 5114 | */ |
| 5115 | skerr = 0; |
| 5116 | lskerr = sizeof(skerr); |
| 5117 | if ((getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) < 0) || |
| 5118 | skerr != 0) |
| 5119 | goto out_error; |
| 5120 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5121 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5122 | /* |
| 5123 | * Check if we have early data. If we do, we have to read them |
| 5124 | * before SSL_do_handshake() is called, And there's no way to |
| 5125 | * detect early data, except to try to read them |
| 5126 | */ |
| 5127 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5128 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5129 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5130 | while (1) { |
| 5131 | ret = SSL_read_early_data(ctx->ssl, |
| 5132 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 5133 | &read_data); |
| 5134 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5135 | goto check_error; |
| 5136 | if (read_data > 0) { |
| 5137 | conn->flags |= CO_FL_EARLY_DATA; |
| 5138 | b_add(&ctx->early_buf, read_data); |
| 5139 | } |
| 5140 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5141 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5142 | if (!b_data(&ctx->early_buf)) |
| 5143 | b_free(&ctx->early_buf); |
| 5144 | break; |
| 5145 | } |
| 5146 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5147 | } |
| 5148 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5149 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5150 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5151 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5152 | * the reneg handshake. |
| 5153 | * Here we use SSL_peek as a workaround for reneg. |
| 5154 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5155 | 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] | 5156 | char c; |
| 5157 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5158 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5159 | if (ret <= 0) { |
| 5160 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5161 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5162 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5163 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5164 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5165 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5166 | 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] | 5167 | return 0; |
| 5168 | } |
| 5169 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5170 | /* handshake may have been completed but we have |
| 5171 | * no more data to read. |
| 5172 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5173 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5174 | ret = 1; |
| 5175 | goto reneg_ok; |
| 5176 | } |
| 5177 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5178 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5179 | 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] | 5180 | return 0; |
| 5181 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5182 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5183 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5184 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5185 | return 0; |
| 5186 | } |
| 5187 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5188 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5189 | /* if errno is null, then connection was successfully established */ |
| 5190 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5191 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5192 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5193 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5194 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5195 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5196 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5197 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5198 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5199 | /* 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] | 5200 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5201 | empty_handshake = state == TLS_ST_BEFORE; |
| 5202 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5203 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5204 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5205 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5206 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5207 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5208 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5209 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5210 | else |
| 5211 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5212 | } |
| 5213 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5214 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5215 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5216 | else |
| 5217 | conn->err_code = CO_ER_SSL_ABORT; |
| 5218 | } |
| 5219 | } |
| 5220 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5221 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5222 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5223 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5224 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5225 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5226 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5227 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5228 | goto out_error; |
| 5229 | } |
| 5230 | else { |
| 5231 | /* Fail on all other handshake errors */ |
| 5232 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5233 | * buffer, causing an RST to be emitted upon close() on |
| 5234 | * TCP sockets. We first try to drain possibly pending |
| 5235 | * data to avoid this as much as possible. |
| 5236 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5237 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5238 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5239 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5240 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5241 | goto out_error; |
| 5242 | } |
| 5243 | } |
| 5244 | /* read some data: consider handshake completed */ |
| 5245 | goto reneg_ok; |
| 5246 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5247 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5248 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5249 | if (ret != 1) { |
| 5250 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5251 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5252 | |
| 5253 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5254 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5255 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5256 | 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] | 5257 | return 0; |
| 5258 | } |
| 5259 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5260 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5261 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5262 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5263 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5264 | return 0; |
| 5265 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5266 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5267 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5268 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5269 | return 0; |
| 5270 | } |
| 5271 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5272 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5273 | /* if errno is null, then connection was successfully established */ |
| 5274 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5275 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5276 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5277 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5278 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5279 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5280 | #else |
| 5281 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5282 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5283 | /* 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] | 5284 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5285 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5286 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5287 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5288 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5289 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5290 | if (empty_handshake) { |
| 5291 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5292 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5293 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5294 | else |
| 5295 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5296 | } |
| 5297 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5298 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5299 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5300 | else |
| 5301 | conn->err_code = CO_ER_SSL_ABORT; |
| 5302 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5303 | } |
| 5304 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5305 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5306 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5307 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5308 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5309 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5310 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5311 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5312 | goto out_error; |
| 5313 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5314 | else { |
| 5315 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5316 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5317 | * buffer, causing an RST to be emitted upon close() on |
| 5318 | * TCP sockets. We first try to drain possibly pending |
| 5319 | * data to avoid this as much as possible. |
| 5320 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5321 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5322 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5323 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5324 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5325 | goto out_error; |
| 5326 | } |
| 5327 | } |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5328 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5329 | else { |
| 5330 | /* |
| 5331 | * If the server refused the early data, we have to send a |
| 5332 | * 425 to the client, as we no longer have the data to sent |
| 5333 | * them again. |
| 5334 | */ |
| 5335 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5336 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5337 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5338 | goto out_error; |
| 5339 | } |
| 5340 | } |
| 5341 | } |
| 5342 | #endif |
| 5343 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5344 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5345 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5346 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5347 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5348 | /* ASYNC engine API doesn't support moving read/write |
| 5349 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5350 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5351 | */ |
| 5352 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5353 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5354 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5355 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5356 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5357 | if (objt_server(conn->target)) { |
| 5358 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5359 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5360 | 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] | 5361 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5362 | else { |
| 5363 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5364 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5365 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5366 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5367 | } |
| 5368 | |
| 5369 | /* The connection is now established at both layers, it's time to leave */ |
| 5370 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5371 | return 1; |
| 5372 | |
| 5373 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5374 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5375 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5376 | ERR_clear_error(); |
| 5377 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5378 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5379 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5380 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5381 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5382 | } |
| 5383 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5384 | /* Fail on all other handshake errors */ |
| 5385 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5386 | if (!conn->err_code) |
| 5387 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5388 | return 0; |
| 5389 | } |
| 5390 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5391 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 5392 | * event subscriber <es> is not allowed to change from a previous call as long |
| 5393 | * as at least one event is still subscribed. The <event_type> must only be a |
| 5394 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 5395 | * unless the transport layer was already released. |
| 5396 | */ |
| 5397 | 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] | 5398 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5399 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5400 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 5401 | if (!ctx) |
| 5402 | return -1; |
| 5403 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5404 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5405 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5406 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5407 | ctx->subs = es; |
| 5408 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5409 | |
| 5410 | /* we may have to subscribe to lower layers for new events */ |
| 5411 | event_type &= ~ctx->wait_event.events; |
| 5412 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 5413 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5414 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5415 | } |
| 5416 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5417 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 5418 | * The <es> pointer is not allowed to differ from the one passed to the |
| 5419 | * subscribe() call. It always returns zero. |
| 5420 | */ |
| 5421 | 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] | 5422 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5423 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5424 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5425 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5426 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5427 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 5428 | es->events &= ~event_type; |
| 5429 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5430 | ctx->subs = NULL; |
| 5431 | |
| 5432 | /* If we subscribed, and we're not doing the handshake, |
| 5433 | * then we subscribed because the upper layer asked for it, |
| 5434 | * as the upper layer is no longer interested, we can |
| 5435 | * unsubscribe too. |
| 5436 | */ |
| 5437 | event_type &= ctx->wait_event.events; |
| 5438 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 5439 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5440 | |
| 5441 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5442 | } |
| 5443 | |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5444 | /* The connection has been taken over, so destroy the old tasklet and create |
| 5445 | * a new one. The original thread ID must be passed into orig_tid |
| 5446 | * It should be called with the takeover lock for the old thread held. |
| 5447 | * Returns 0 on success, and -1 on failure |
| 5448 | */ |
| 5449 | static int ssl_takeover(struct connection *conn, void *xprt_ctx, int orig_tid) |
| 5450 | { |
| 5451 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5452 | struct tasklet *tl = tasklet_new(); |
| 5453 | |
| 5454 | if (!tl) |
| 5455 | return -1; |
| 5456 | |
| 5457 | ctx->wait_event.tasklet->context = NULL; |
| 5458 | tasklet_wakeup_on(ctx->wait_event.tasklet, orig_tid); |
| 5459 | ctx->wait_event.tasklet = tl; |
| 5460 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 5461 | ctx->wait_event.tasklet->context = ctx; |
| 5462 | return 0; |
| 5463 | } |
| 5464 | |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 5465 | /* notify the next xprt that the connection is about to become idle and that it |
| 5466 | * may be stolen at any time after the function returns and that any tasklet in |
| 5467 | * the chain must be careful before dereferencing its context. |
| 5468 | */ |
| 5469 | static void ssl_set_idle(struct connection *conn, void *xprt_ctx) |
| 5470 | { |
| 5471 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5472 | |
| 5473 | if (!ctx || !ctx->wait_event.tasklet) |
| 5474 | return; |
| 5475 | |
| 5476 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 5477 | if (ctx->xprt) |
| 5478 | xprt_set_idle(conn, ctx->xprt, ctx->xprt_ctx); |
| 5479 | } |
| 5480 | |
| 5481 | /* notify the next xprt that the connection is not idle anymore and that it may |
| 5482 | * not be stolen before the next xprt_set_idle(). |
| 5483 | */ |
| 5484 | static void ssl_set_used(struct connection *conn, void *xprt_ctx) |
| 5485 | { |
| 5486 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5487 | |
| 5488 | if (!ctx || !ctx->wait_event.tasklet) |
| 5489 | return; |
| 5490 | |
| 5491 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 5492 | if (ctx->xprt) |
| 5493 | xprt_set_used(conn, ctx->xprt, ctx->xprt_ctx); |
| 5494 | } |
| 5495 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 5496 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 5497 | * Returns 0 on success, and non-zero on failure. |
| 5498 | */ |
| 5499 | 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) |
| 5500 | { |
| 5501 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5502 | |
| 5503 | if (oldxprt_ops != NULL) |
| 5504 | *oldxprt_ops = ctx->xprt; |
| 5505 | if (oldxprt_ctx != NULL) |
| 5506 | *oldxprt_ctx = ctx->xprt_ctx; |
| 5507 | ctx->xprt = toadd_ops; |
| 5508 | ctx->xprt_ctx = toadd_ctx; |
| 5509 | return 0; |
| 5510 | } |
| 5511 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 5512 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 5513 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 5514 | * XPRT. |
| 5515 | */ |
| 5516 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 5517 | { |
| 5518 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5519 | |
| 5520 | if (ctx->xprt_ctx == toremove_ctx) { |
| 5521 | ctx->xprt_ctx = newctx; |
| 5522 | ctx->xprt = newops; |
| 5523 | return 0; |
| 5524 | } |
| 5525 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 5526 | } |
| 5527 | |
Willy Tarreau | 9a5a0e0 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 5528 | 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] | 5529 | { |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5530 | struct tasklet *tl = (struct tasklet *)t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5531 | struct ssl_sock_ctx *ctx = context; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5532 | struct connection *conn; |
| 5533 | int conn_in_list; |
| 5534 | int ret = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5535 | |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 5536 | if (state & TASK_F_USR1) { |
| 5537 | /* the tasklet was idling on an idle connection, it might have |
| 5538 | * been stolen, let's be careful! |
| 5539 | */ |
| 5540 | HA_SPIN_LOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 5541 | if (tl->context == NULL) { |
| 5542 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
| 5543 | tasklet_free(tl); |
| 5544 | return NULL; |
| 5545 | } |
| 5546 | conn = ctx->conn; |
| 5547 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 5548 | if (conn_in_list) |
| 5549 | MT_LIST_DEL(&conn->list); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5550 | HA_SPIN_UNLOCK(OTHER_LOCK, &idle_conns[tid].takeover_lock); |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 5551 | } else { |
| 5552 | conn = ctx->conn; |
| 5553 | conn_in_list = 0; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5554 | } |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 5555 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5556 | /* First if we're doing an handshake, try that */ |
Willy Tarreau | b421f9d | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5557 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5558 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
Willy Tarreau | b421f9d | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5559 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 5560 | /* handshake completed, leave the bulk queue */ |
Willy Tarreau | a0c6da1 | 2021-03-09 17:58:02 +0100 | [diff] [blame] | 5561 | _HA_ATOMIC_AND(&tl->state, ~TASK_HEAVY); |
Willy Tarreau | b421f9d | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5562 | } |
| 5563 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5564 | /* If we had an error, or the handshake is done and I/O is available, |
| 5565 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5566 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5567 | * we can't be sure conn_fd_handler() will be called again. |
| 5568 | */ |
| 5569 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 5570 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5571 | int woke = 0; |
| 5572 | |
| 5573 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5574 | if (ctx->subs) { |
| 5575 | tasklet_wakeup(ctx->subs->tasklet); |
| 5576 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5577 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5578 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5579 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5580 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5581 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5582 | * upper layers know. If we have no mux, create it, |
| 5583 | * and once we have a mux, call its wake method if we didn't |
| 5584 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5585 | */ |
| 5586 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 5587 | if (!ctx->conn->mux) |
| 5588 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5589 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5590 | ret = ctx->conn->mux->wake(ctx->conn); |
| 5591 | goto leave; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5592 | } |
| 5593 | } |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5594 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5595 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5596 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 5597 | ctx->subs->events & SUB_RETRY_RECV) { |
| 5598 | tasklet_wakeup(ctx->subs->tasklet); |
| 5599 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 5600 | if (!ctx->subs->events) |
| 5601 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5602 | } |
| 5603 | #endif |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5604 | leave: |
| 5605 | if (!ret && conn_in_list) { |
| 5606 | struct server *srv = objt_server(conn->target); |
| 5607 | |
| 5608 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | a9d7b76 | 2020-07-10 08:28:20 +0200 | [diff] [blame] | 5609 | MT_LIST_ADDQ(&srv->safe_conns[tid], &conn->list); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5610 | else |
Willy Tarreau | a9d7b76 | 2020-07-10 08:28:20 +0200 | [diff] [blame] | 5611 | MT_LIST_ADDQ(&srv->idle_conns[tid], &conn->list); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 5612 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5613 | return NULL; |
| 5614 | } |
| 5615 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5616 | /* 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] | 5617 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5618 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5619 | * flags are updated with whatever special event is detected (error, read0, |
| 5620 | * empty). The caller is responsible for taking care of those events and |
| 5621 | * avoiding the call if inappropriate. The function does not call the |
| 5622 | * connection's polling update function, so the caller is responsible for this. |
| 5623 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5624 | 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] | 5625 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5626 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5627 | ssize_t ret; |
| 5628 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5629 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5630 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5631 | goto out_error; |
| 5632 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5633 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 5634 | if (b_data(&ctx->early_buf)) { |
| 5635 | try = b_contig_space(buf); |
| 5636 | if (try > b_data(&ctx->early_buf)) |
| 5637 | try = b_data(&ctx->early_buf); |
| 5638 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 5639 | b_add(buf, try); |
| 5640 | b_del(&ctx->early_buf, try); |
| 5641 | if (b_data(&ctx->early_buf) == 0) |
| 5642 | b_free(&ctx->early_buf); |
| 5643 | return try; |
| 5644 | } |
| 5645 | #endif |
| 5646 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5647 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5648 | /* a handshake was requested */ |
| 5649 | return 0; |
| 5650 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5651 | /* read the largest possible block. For this, we perform only one call |
| 5652 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5653 | * in which case we accept to do it once again. A new attempt is made on |
| 5654 | * EINTR too. |
| 5655 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5656 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5657 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5658 | try = b_contig_space(buf); |
| 5659 | if (!try) |
| 5660 | break; |
| 5661 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5662 | if (try > count) |
| 5663 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5664 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5665 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5666 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5667 | if (conn->flags & CO_FL_ERROR) { |
| 5668 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5669 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5670 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5671 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5672 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5673 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5674 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5675 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5676 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5677 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5678 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5679 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5680 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5681 | 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] | 5682 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5683 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5684 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5685 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5686 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5687 | break; |
| 5688 | } |
| 5689 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5690 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5691 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5692 | SUB_RETRY_RECV, |
| 5693 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5694 | /* handshake is running, and it may need to re-enable read */ |
| 5695 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5696 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5697 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5698 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5699 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5700 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5701 | break; |
| 5702 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5703 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5704 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5705 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5706 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5707 | * stack before shutting down the connection for |
| 5708 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5709 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5710 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5711 | /* otherwise it's a real error */ |
| 5712 | goto out_error; |
| 5713 | } |
| 5714 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5715 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5716 | return done; |
| 5717 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5718 | clear_ssl_error: |
| 5719 | /* Clear openssl global errors stack */ |
| 5720 | ssl_sock_dump_errors(conn); |
| 5721 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5722 | read0: |
| 5723 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5724 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5725 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5726 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5727 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5728 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5729 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5730 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5731 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5732 | } |
| 5733 | |
| 5734 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5735 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5736 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5737 | * 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] | 5738 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5739 | * a second call may be performed. The connection's flags are updated with |
| 5740 | * whatever special event is detected (error, empty). The caller is responsible |
| 5741 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5742 | * 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] | 5743 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5744 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5745 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5746 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5747 | 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] | 5748 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5749 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5750 | ssize_t ret; |
| 5751 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5752 | |
| 5753 | done = 0; |
| 5754 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5755 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5756 | goto out_error; |
| 5757 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5758 | 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] | 5759 | /* a handshake was requested */ |
| 5760 | return 0; |
| 5761 | |
| 5762 | /* send the largest possible block. For this we perform only one call |
| 5763 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5764 | * in which case we accept to do it once again. |
| 5765 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5766 | while (count) { |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5767 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5768 | size_t written_data; |
| 5769 | #endif |
| 5770 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5771 | try = b_contig_data(buf, done); |
| 5772 | if (try > count) |
| 5773 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5774 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5775 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5776 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5777 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5778 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5779 | } |
| 5780 | else { |
| 5781 | /* we need to keep the information about the fact that |
| 5782 | * we're not limiting the upcoming send(), because if it |
| 5783 | * fails, we'll have to retry with at least as many data. |
| 5784 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5785 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5786 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5787 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5788 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5789 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5790 | unsigned int max_early; |
| 5791 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5792 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5793 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5794 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5795 | if (SSL_get0_session(ctx->ssl)) |
| 5796 | 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] | 5797 | else |
| 5798 | max_early = 0; |
| 5799 | } |
| 5800 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5801 | if (try + ctx->sent_early_data > max_early) { |
| 5802 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5803 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5804 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5805 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5806 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5807 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5808 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5809 | 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] | 5810 | if (ret == 1) { |
| 5811 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5812 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5813 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5814 | 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] | 5815 | /* Initiate the handshake, now */ |
| 5816 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 5817 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5818 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5819 | } |
| 5820 | |
| 5821 | } else |
| 5822 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5823 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5824 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5825 | if (conn->flags & CO_FL_ERROR) { |
| 5826 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5827 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5828 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5829 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5830 | /* A send succeeded, so we can consider ourself connected */ |
| 5831 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5832 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5833 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5834 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5835 | } |
| 5836 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5837 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5838 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5839 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5840 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5841 | /* handshake is running, and it may need to re-enable write */ |
| 5842 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5843 | 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] | 5844 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5845 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5846 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5847 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5848 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5849 | break; |
| 5850 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5851 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5852 | break; |
| 5853 | } |
| 5854 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5855 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5856 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5857 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5858 | SUB_RETRY_RECV, |
| 5859 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5860 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5861 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5862 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5863 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5864 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5865 | break; |
| 5866 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5867 | goto out_error; |
| 5868 | } |
| 5869 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5870 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5871 | return done; |
| 5872 | |
| 5873 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5874 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5875 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5876 | ERR_clear_error(); |
| 5877 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5878 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5879 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5880 | } |
| 5881 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5882 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5883 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5884 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5885 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5886 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5887 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5888 | if (ctx->wait_event.events != 0) |
| 5889 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 5890 | ctx->wait_event.events, |
| 5891 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5892 | if (ctx->subs) { |
| 5893 | ctx->subs->events = 0; |
| 5894 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5895 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5896 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 5897 | if (ctx->xprt->close) |
| 5898 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5899 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5900 | if (global_ssl.async) { |
| 5901 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5902 | size_t num_all_fds = 0; |
| 5903 | int i; |
| 5904 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5905 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5906 | if (num_all_fds > 32) { |
| 5907 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5908 | return; |
| 5909 | } |
| 5910 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5911 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5912 | |
| 5913 | /* If an async job is pending, we must try to |
| 5914 | to catch the end using polling before calling |
| 5915 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5916 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5917 | for (i=0 ; i < num_all_fds ; i++) { |
| 5918 | /* switch on an handler designed to |
| 5919 | * handle the SSL_free |
| 5920 | */ |
| 5921 | afd = all_fd[i]; |
| 5922 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5923 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5924 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5925 | /* To ensure that the fd cache won't be used |
| 5926 | * and we'll catch a real RD event. |
| 5927 | */ |
| 5928 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5929 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5930 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5931 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5932 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5933 | return; |
| 5934 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5935 | /* Else we can remove the fds from the fdtab |
| 5936 | * and call SSL_free. |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 5937 | * note: we do a fd_stop_both and not a delete |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5938 | * because the fd is owned by the engine. |
| 5939 | * the engine is responsible to close |
| 5940 | */ |
| 5941 | for (i=0 ; i < num_all_fds ; i++) |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 5942 | fd_stop_both(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5943 | } |
| 5944 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5945 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5946 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5947 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5948 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5949 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5950 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5951 | } |
| 5952 | |
| 5953 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5954 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5955 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5956 | 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] | 5957 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5958 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5959 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 5960 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5961 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5962 | if (!clean) |
| 5963 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5964 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5965 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5966 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5967 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5968 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5969 | ERR_clear_error(); |
| 5970 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5971 | } |
| 5972 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5973 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5974 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 5975 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 5976 | { |
| 5977 | struct ssl_sock_ctx *ctx; |
| 5978 | X509 *crt; |
| 5979 | |
| 5980 | if (!ssl_sock_is_ssl(conn)) |
| 5981 | return 0; |
| 5982 | |
| 5983 | ctx = conn->xprt_ctx; |
| 5984 | |
| 5985 | crt = SSL_get_certificate(ctx->ssl); |
| 5986 | if (!crt) |
| 5987 | return 0; |
| 5988 | |
| 5989 | return cert_get_pkey_algo(crt, out); |
| 5990 | } |
| 5991 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5992 | /* used for ppv2 cert signature (can be used for logging) */ |
| 5993 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 5994 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 5995 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5996 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 5997 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 5998 | X509 *crt; |
| 5999 | |
| 6000 | if (!ssl_sock_is_ssl(conn)) |
| 6001 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6002 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6003 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6004 | if (!crt) |
| 6005 | return NULL; |
| 6006 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6007 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6008 | } |
| 6009 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6010 | /* used for ppv2 authority */ |
| 6011 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6012 | { |
| 6013 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6014 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6015 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6016 | if (!ssl_sock_is_ssl(conn)) |
| 6017 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6018 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6019 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6020 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6021 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6022 | #endif |
| 6023 | } |
| 6024 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6025 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6026 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6027 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6028 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6029 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6030 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6031 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6032 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6033 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6034 | } |
| 6035 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6036 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6037 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 6038 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6039 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6040 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6041 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6042 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6043 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6044 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6045 | } |
| 6046 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6047 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6048 | { |
| 6049 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6050 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6051 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 6052 | if (!ssl_sock_is_ssl(conn)) |
| 6053 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6054 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6055 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6056 | #endif |
| 6057 | } |
| 6058 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6059 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6060 | * to disable SNI. |
| 6061 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6062 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6063 | { |
| 6064 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6065 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6066 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6067 | char *prev_name; |
| 6068 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6069 | if (!ssl_sock_is_ssl(conn)) |
| 6070 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6071 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6072 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6073 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6074 | * new connection will present a new SNI. As an optimization we could |
| 6075 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6076 | * server. |
| 6077 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6078 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6079 | if ((!prev_name && hostname) || |
| 6080 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6081 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6082 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6083 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6084 | #endif |
| 6085 | } |
| 6086 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6087 | /* Extract peer certificate's common name into the chunk dest |
| 6088 | * Returns |
| 6089 | * the len of the extracted common name |
| 6090 | * or 0 if no CN found in DN |
| 6091 | * or -1 on error case (i.e. no peer certificate) |
| 6092 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6093 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6094 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6095 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6096 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6097 | X509 *crt = NULL; |
| 6098 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6099 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6100 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6101 | .area = (char *)&find_cn, |
| 6102 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6103 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6104 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6105 | |
| 6106 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6107 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6108 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6109 | |
| 6110 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6111 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6112 | if (!crt) |
| 6113 | goto out; |
| 6114 | |
| 6115 | name = X509_get_subject_name(crt); |
| 6116 | if (!name) |
| 6117 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6118 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6119 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6120 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6121 | if (crt) |
| 6122 | X509_free(crt); |
| 6123 | |
| 6124 | return result; |
| 6125 | } |
| 6126 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6127 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6128 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6129 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6130 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6131 | X509 *crt = NULL; |
| 6132 | |
| 6133 | if (!ssl_sock_is_ssl(conn)) |
| 6134 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6135 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6136 | |
| 6137 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6138 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6139 | if (!crt) |
| 6140 | return 0; |
| 6141 | |
| 6142 | X509_free(crt); |
| 6143 | return 1; |
| 6144 | } |
| 6145 | |
| 6146 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6147 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6148 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6149 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6150 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6151 | if (!ssl_sock_is_ssl(conn)) |
| 6152 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6153 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6154 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6155 | } |
| 6156 | |
| 6157 | /* returns result from SSL verify */ |
| 6158 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6159 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6160 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6161 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6162 | if (!ssl_sock_is_ssl(conn)) |
| 6163 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6164 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6165 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6166 | } |
| 6167 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6168 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6169 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6170 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6171 | * freed by the caller. NPN is also checked if available since older versions |
| 6172 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6173 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6174 | 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] | 6175 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6176 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6177 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6178 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6179 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6180 | return 0; |
| 6181 | |
| 6182 | *str = NULL; |
| 6183 | |
| 6184 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6185 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6186 | if (*str) |
| 6187 | return 1; |
| 6188 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6189 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6190 | 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] | 6191 | if (*str) |
| 6192 | return 1; |
| 6193 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6194 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6195 | return 0; |
| 6196 | } |
| 6197 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6198 | /* "issuers-chain-path" load chain certificate in global */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6199 | 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] | 6200 | { |
| 6201 | X509 *ca; |
| 6202 | X509_NAME *name = NULL; |
| 6203 | ASN1_OCTET_STRING *skid = NULL; |
| 6204 | STACK_OF(X509) *chain = NULL; |
| 6205 | struct issuer_chain *issuer; |
| 6206 | struct eb64_node *node; |
| 6207 | char *path; |
| 6208 | u64 key; |
| 6209 | int ret = 0; |
| 6210 | |
| 6211 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 6212 | if (chain == NULL) { |
| 6213 | chain = sk_X509_new_null(); |
| 6214 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 6215 | name = X509_get_subject_name(ca); |
| 6216 | } |
| 6217 | if (!sk_X509_push(chain, ca)) { |
| 6218 | X509_free(ca); |
| 6219 | goto end; |
| 6220 | } |
| 6221 | } |
| 6222 | if (!chain) { |
| 6223 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 6224 | goto end; |
| 6225 | } |
| 6226 | if (!skid) { |
| 6227 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 6228 | goto end; |
| 6229 | } |
| 6230 | if (!name) { |
| 6231 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 6232 | goto end; |
| 6233 | } |
| 6234 | key = XXH64(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6235 | 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] | 6236 | issuer = container_of(node, typeof(*issuer), node); |
| 6237 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 6238 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 6239 | goto end; |
| 6240 | } |
| 6241 | } |
| 6242 | issuer = calloc(1, sizeof *issuer); |
| 6243 | path = strdup(fp); |
| 6244 | if (!issuer || !path) { |
| 6245 | free(issuer); |
| 6246 | free(path); |
| 6247 | goto end; |
| 6248 | } |
| 6249 | issuer->node.key = key; |
| 6250 | issuer->path = path; |
| 6251 | issuer->chain = chain; |
| 6252 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6253 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6254 | ret = 1; |
| 6255 | end: |
| 6256 | if (skid) |
| 6257 | ASN1_OCTET_STRING_free(skid); |
| 6258 | if (chain) |
| 6259 | sk_X509_pop_free(chain, X509_free); |
| 6260 | return ret; |
| 6261 | } |
| 6262 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 6263 | struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6264 | { |
| 6265 | AUTHORITY_KEYID *akid; |
| 6266 | struct issuer_chain *issuer = NULL; |
| 6267 | |
| 6268 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
William Lallemand | 829d071 | 2020-11-19 16:24:13 +0100 | [diff] [blame] | 6269 | if (akid && akid->keyid) { |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6270 | struct eb64_node *node; |
| 6271 | u64 hk; |
| 6272 | hk = XXH64(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
| 6273 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 6274 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 6275 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 6276 | issuer = ti; |
| 6277 | break; |
| 6278 | } |
| 6279 | } |
| 6280 | AUTHORITY_KEYID_free(akid); |
| 6281 | } |
| 6282 | return issuer; |
| 6283 | } |
| 6284 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6285 | void ssl_free_global_issuers(void) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6286 | { |
| 6287 | struct eb64_node *node, *back; |
| 6288 | struct issuer_chain *issuer; |
| 6289 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6290 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6291 | while (node) { |
| 6292 | issuer = container_of(node, typeof(*issuer), node); |
| 6293 | back = eb64_next(node); |
| 6294 | eb64_delete(node); |
| 6295 | free(issuer->path); |
| 6296 | sk_X509_pop_free(issuer->chain, X509_free); |
| 6297 | free(issuer); |
| 6298 | node = back; |
| 6299 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6300 | } |
| 6301 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6302 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6303 | static int ssl_check_async_engine_count(void) { |
| 6304 | int err_code = 0; |
| 6305 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6306 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6307 | 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] | 6308 | err_code = ERR_ABORT; |
| 6309 | } |
| 6310 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6311 | } |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6312 | #endif |
| 6313 | |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6314 | /* "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] | 6315 | * 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] | 6316 | */ |
Willy Tarreau | 6cb467e | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 6317 | 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] | 6318 | { |
| 6319 | const struct ssl_sock_ctx *sctx = ctx; |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6320 | int ret = 0; |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6321 | |
| 6322 | if (!sctx) |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6323 | return ret; |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6324 | |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6325 | if (sctx->conn != conn) { |
| 6326 | chunk_appendf(&trash, " xctx.conn=%p(BOGUS)", sctx->conn); |
| 6327 | ret = 1; |
| 6328 | } |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6329 | chunk_appendf(&trash, " xctx.st=%d", sctx->xprt_st); |
| 6330 | |
| 6331 | if (sctx->xprt) { |
| 6332 | chunk_appendf(&trash, " .xprt=%s", sctx->xprt->name); |
| 6333 | if (sctx->xprt_ctx) |
| 6334 | chunk_appendf(&trash, " .xctx=%p", sctx->xprt_ctx); |
| 6335 | } |
| 6336 | |
| 6337 | chunk_appendf(&trash, " .wait.ev=%d", sctx->wait_event.events); |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6338 | |
| 6339 | /* as soon as a shutdown is reported the lower layer unregisters its |
| 6340 | * subscriber, so the situations below are transient and rare enough to |
| 6341 | * be reported as suspicious. In any case they shouldn't last. |
| 6342 | */ |
| 6343 | if ((sctx->wait_event.events & 1) && (conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_ERROR))) |
| 6344 | ret = 1; |
| 6345 | if ((sctx->wait_event.events & 2) && (conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR))) |
| 6346 | ret = 1; |
| 6347 | |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6348 | chunk_appendf(&trash, " .subs=%p", sctx->subs); |
| 6349 | if (sctx->subs) { |
| 6350 | 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] | 6351 | if (sctx->subs->tasklet->calls >= 1000000) |
| 6352 | ret = 1; |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6353 | chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 6354 | sctx->subs->tasklet->calls, |
| 6355 | sctx->subs->tasklet->context); |
| 6356 | resolve_sym_name(&trash, NULL, sctx->subs->tasklet->process); |
| 6357 | chunk_appendf(&trash, ")"); |
| 6358 | } |
| 6359 | chunk_appendf(&trash, " .sent_early=%d", sctx->sent_early_data); |
| 6360 | chunk_appendf(&trash, " .early_in=%d", (int)sctx->early_buf.data); |
Willy Tarreau | 767dedd | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6361 | return ret; |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6362 | } |
| 6363 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6364 | /* This function is used with TLS ticket keys management. It permits to browse |
| 6365 | * each reference. The variable <getnext> must contain the current node, |
| 6366 | * <end> point to the root node. |
| 6367 | */ |
| 6368 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6369 | static inline |
| 6370 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 6371 | { |
| 6372 | struct tls_keys_ref *ref = getnext; |
| 6373 | |
| 6374 | while (1) { |
| 6375 | |
| 6376 | /* Get next list entry. */ |
| 6377 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 6378 | |
| 6379 | /* If the entry is the last of the list, return NULL. */ |
| 6380 | if (&ref->list == end) |
| 6381 | return NULL; |
| 6382 | |
| 6383 | return ref; |
| 6384 | } |
| 6385 | } |
| 6386 | |
| 6387 | static inline |
| 6388 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 6389 | { |
| 6390 | int id; |
| 6391 | char *error; |
| 6392 | |
| 6393 | /* If the reference starts by a '#', this is numeric id. */ |
| 6394 | if (reference[0] == '#') { |
| 6395 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 6396 | id = strtol(reference + 1, &error, 10); |
| 6397 | if (*error != '\0') |
| 6398 | return NULL; |
| 6399 | |
| 6400 | /* Perform the unique id lookup. */ |
| 6401 | return tlskeys_ref_lookupid(id); |
| 6402 | } |
| 6403 | |
| 6404 | /* Perform the string lookup. */ |
| 6405 | return tlskeys_ref_lookup(reference); |
| 6406 | } |
| 6407 | #endif |
| 6408 | |
| 6409 | |
| 6410 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6411 | |
| 6412 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 6413 | |
| 6414 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 6415 | return cli_io_handler_tlskeys_files(appctx); |
| 6416 | } |
| 6417 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6418 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 6419 | * (next index to be dumped), and cli.p0 (next key reference). |
| 6420 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6421 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 6422 | |
| 6423 | struct stream_interface *si = appctx->owner; |
| 6424 | |
| 6425 | switch (appctx->st2) { |
| 6426 | case STAT_ST_INIT: |
| 6427 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 6428 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6429 | * later and restart at the state "STAT_ST_INIT". |
| 6430 | */ |
| 6431 | chunk_reset(&trash); |
| 6432 | |
| 6433 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 6434 | chunk_appendf(&trash, "# id secret\n"); |
| 6435 | else |
| 6436 | chunk_appendf(&trash, "# id (file)\n"); |
| 6437 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6438 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6439 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6440 | return 0; |
| 6441 | } |
| 6442 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6443 | /* Now, we start the browsing of the references lists. |
| 6444 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 6445 | * available field of this pointer is <list>. It is used with the function |
| 6446 | * tlskeys_list_get_next() for retruning the first available entry |
| 6447 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6448 | if (appctx->ctx.cli.p0 == NULL) { |
| 6449 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 6450 | 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] | 6451 | } |
| 6452 | |
| 6453 | appctx->st2 = STAT_ST_LIST; |
| 6454 | /* fall through */ |
| 6455 | |
| 6456 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6457 | while (appctx->ctx.cli.p0) { |
| 6458 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6459 | |
| 6460 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6461 | 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] | 6462 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6463 | |
| 6464 | if (appctx->ctx.cli.i1 == 0) |
| 6465 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 6466 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6467 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6468 | int head; |
| 6469 | |
| 6470 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 6471 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6472 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6473 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6474 | |
| 6475 | chunk_reset(t2); |
| 6476 | /* 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] | 6477 | if (ref->key_size_bits == 128) { |
| 6478 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 6479 | sizeof(struct tls_sess_key_128), |
| 6480 | t2->area, t2->size); |
| 6481 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 6482 | t2->area); |
| 6483 | } |
| 6484 | else if (ref->key_size_bits == 256) { |
| 6485 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 6486 | sizeof(struct tls_sess_key_256), |
| 6487 | t2->area, t2->size); |
| 6488 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 6489 | t2->area); |
| 6490 | } |
| 6491 | else { |
| 6492 | /* This case should never happen */ |
| 6493 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 6494 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6495 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6496 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6497 | /* let's try again later from this stream. We add ourselves into |
| 6498 | * this stream's users so that it can remove us upon termination. |
| 6499 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6500 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6501 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6502 | return 0; |
| 6503 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6504 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6505 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 6506 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6507 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6508 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 6509 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6510 | /* let's try again later from this stream. We add ourselves into |
| 6511 | * this stream's users so that it can remove us upon termination. |
| 6512 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 6513 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6514 | return 0; |
| 6515 | } |
| 6516 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6517 | 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] | 6518 | break; |
| 6519 | |
| 6520 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6521 | 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] | 6522 | } |
| 6523 | |
| 6524 | appctx->st2 = STAT_ST_FIN; |
| 6525 | /* fall through */ |
| 6526 | |
| 6527 | default: |
| 6528 | appctx->st2 = STAT_ST_FIN; |
| 6529 | return 1; |
| 6530 | } |
| 6531 | return 0; |
| 6532 | } |
| 6533 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6534 | /* 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] | 6535 | 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] | 6536 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6537 | /* no parameter, shows only file list */ |
| 6538 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6539 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6540 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6541 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6542 | } |
| 6543 | |
| 6544 | if (args[2][0] == '*') { |
| 6545 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6546 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6547 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6548 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6549 | if (!appctx->ctx.cli.p0) |
| 6550 | 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] | 6551 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6552 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 6553 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6554 | } |
| 6555 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6556 | 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] | 6557 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6558 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6559 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6560 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6561 | /* 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] | 6562 | if (!*args[3] || !*args[4]) |
| 6563 | 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] | 6564 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 6565 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6566 | if (!ref) |
| 6567 | 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] | 6568 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6569 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6570 | if (ret < 0) |
| 6571 | 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] | 6572 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6573 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6574 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 6575 | 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] | 6576 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6577 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6578 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6579 | #endif |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 6580 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6581 | 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] | 6582 | { |
| 6583 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 6584 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6585 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 6586 | |
| 6587 | if (!payload) |
| 6588 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6589 | |
| 6590 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6591 | if (!*payload) |
| 6592 | 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] | 6593 | |
| 6594 | /* remove \r and \n from the payload */ |
| 6595 | for (i = 0, j = 0; payload[i]; i++) { |
| 6596 | if (payload[i] == '\r' || payload[i] == '\n') |
| 6597 | continue; |
| 6598 | payload[j++] = payload[i]; |
| 6599 | } |
| 6600 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6601 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6602 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6603 | if (ret < 0) |
| 6604 | 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] | 6605 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 6606 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6607 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6608 | if (err) |
| 6609 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 6610 | else |
| 6611 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6612 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6613 | |
| 6614 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6615 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 6616 | 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] | 6617 | #endif |
| 6618 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 6619 | } |
| 6620 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6621 | /* register cli keywords */ |
| 6622 | static struct cli_kw_list cli_kws = {{ },{ |
| 6623 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6624 | { { "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] | 6625 | { { "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] | 6626 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6627 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6628 | { { NULL }, NULL, NULL, NULL } |
| 6629 | }}; |
| 6630 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6631 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 6632 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 6633 | /* transport-layer operations for SSL sockets */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6634 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6635 | .snd_buf = ssl_sock_from_buf, |
| 6636 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6637 | .subscribe = ssl_subscribe, |
| 6638 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6639 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6640 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6641 | .rcv_pipe = NULL, |
| 6642 | .snd_pipe = NULL, |
| 6643 | .shutr = NULL, |
| 6644 | .shutw = ssl_sock_shutw, |
| 6645 | .close = ssl_sock_close, |
| 6646 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 6647 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 6648 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 6649 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 6650 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6651 | .get_alpn = ssl_sock_get_alpn, |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6652 | .takeover = ssl_takeover, |
Willy Tarreau | b0d3a28 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6653 | .set_idle = ssl_set_idle, |
| 6654 | .set_used = ssl_set_used, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 6655 | .name = "SSL", |
Willy Tarreau | daad3c8 | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6656 | .show_fd = ssl_sock_show_fd, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6657 | }; |
| 6658 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6659 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 6660 | struct session *sess, struct stream *s, int flags) |
| 6661 | { |
| 6662 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6663 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6664 | |
| 6665 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6666 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6667 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 6668 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6669 | 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] | 6670 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6671 | s->req.flags |= CF_READ_NULL; |
| 6672 | return ACT_RET_YIELD; |
| 6673 | } |
| 6674 | } |
| 6675 | return (ACT_RET_CONT); |
| 6676 | } |
| 6677 | |
| 6678 | 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) |
| 6679 | { |
| 6680 | rule->action_ptr = ssl_action_wait_for_hs; |
| 6681 | |
| 6682 | return ACT_RET_PRS_OK; |
| 6683 | } |
| 6684 | |
| 6685 | static struct action_kw_list http_req_actions = {ILH, { |
| 6686 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 6687 | { /* END */ } |
| 6688 | }}; |
| 6689 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 6690 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 6691 | |
Ilya Shipitsin | 49bab75 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 6692 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6693 | |
| 6694 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6695 | { |
| 6696 | if (ptr) { |
| 6697 | chunk_destroy(ptr); |
| 6698 | free(ptr); |
| 6699 | } |
| 6700 | } |
| 6701 | |
| 6702 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 6703 | |
| 6704 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 6705 | static void ssl_sock_ocsp_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6706 | { |
| 6707 | struct ocsp_cbk_arg *ocsp_arg; |
| 6708 | |
| 6709 | if (ptr) { |
| 6710 | ocsp_arg = ptr; |
| 6711 | |
| 6712 | if (ocsp_arg->is_single) { |
| 6713 | ssl_sock_free_ocsp(ocsp_arg->s_ocsp); |
| 6714 | ocsp_arg->s_ocsp = NULL; |
| 6715 | } else { |
| 6716 | int i; |
| 6717 | |
| 6718 | for (i = 0; i < SSL_SOCK_NUM_KEYTYPES; i++) { |
| 6719 | ssl_sock_free_ocsp(ocsp_arg->m_ocsp[i]); |
| 6720 | ocsp_arg->m_ocsp[i] = NULL; |
| 6721 | } |
| 6722 | } |
| 6723 | free(ocsp_arg); |
| 6724 | } |
| 6725 | } |
| 6726 | #endif |
| 6727 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6728 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6729 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6730 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 6731 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 6732 | |
Ilya Shipitsin | 04a5a44 | 2020-11-03 14:15:38 +0500 | [diff] [blame] | 6733 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 6734 | static void ssl_sock_keylog_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 6735 | { |
| 6736 | struct ssl_keylog *keylog; |
| 6737 | |
| 6738 | if (!ptr) |
| 6739 | return; |
| 6740 | |
| 6741 | keylog = ptr; |
| 6742 | |
| 6743 | pool_free(pool_head_ssl_keylog_str, keylog->client_random); |
| 6744 | pool_free(pool_head_ssl_keylog_str, keylog->client_early_traffic_secret); |
| 6745 | pool_free(pool_head_ssl_keylog_str, keylog->client_handshake_traffic_secret); |
| 6746 | pool_free(pool_head_ssl_keylog_str, keylog->server_handshake_traffic_secret); |
| 6747 | pool_free(pool_head_ssl_keylog_str, keylog->client_traffic_secret_0); |
| 6748 | pool_free(pool_head_ssl_keylog_str, keylog->server_traffic_secret_0); |
| 6749 | pool_free(pool_head_ssl_keylog_str, keylog->exporter_secret); |
| 6750 | pool_free(pool_head_ssl_keylog_str, keylog->early_exporter_secret); |
| 6751 | |
| 6752 | pool_free(pool_head_ssl_keylog, ptr); |
| 6753 | } |
| 6754 | #endif |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6755 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6756 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 6757 | static void __ssl_sock_init(void) |
| 6758 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6759 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6760 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6761 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6762 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6763 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6764 | if (global_ssl.listen_default_ciphers) |
| 6765 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 6766 | if (global_ssl.connect_default_ciphers) |
| 6767 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 6768 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 6769 | if (global_ssl.listen_default_ciphersuites) |
| 6770 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 6771 | if (global_ssl.connect_default_ciphersuites) |
| 6772 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 6773 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 6774 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 6775 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 6776 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6777 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6778 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6779 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6780 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6781 | n = sk_SSL_COMP_num(cm); |
| 6782 | while (n--) { |
| 6783 | (void) sk_SSL_COMP_pop(cm); |
| 6784 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 6785 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 6786 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6787 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6788 | ssl_locking_init(); |
| 6789 | #endif |
Ilya Shipitsin | 49bab75 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 6790 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 6791 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 6792 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 6793 | |
| 6794 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 6795 | ocsp_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_ocsp_free_func); |
| 6796 | #endif |
| 6797 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 6798 | 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] | 6799 | 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] | 6800 | #ifdef HAVE_OPENSSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 6801 | ssl_keylog_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_keylog_free_func); |
| 6802 | #endif |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6803 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6804 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6805 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6806 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 6807 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 6808 | hap_register_post_check(tlskeys_finalize_config); |
| 6809 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6810 | |
| 6811 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 6812 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 6813 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6814 | hap_register_post_deinit(ssl_free_global_issuers); |
| 6815 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6816 | #ifndef OPENSSL_NO_DH |
| 6817 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 6818 | hap_register_post_deinit(ssl_free_dh); |
| 6819 | #endif |
| 6820 | #ifndef OPENSSL_NO_ENGINE |
| 6821 | hap_register_post_deinit(ssl_free_engines); |
| 6822 | #endif |
| 6823 | /* Load SSL string for the verbose & debug mode. */ |
| 6824 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6825 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 6826 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 6827 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 6828 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 6829 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 6830 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 6831 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 6832 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 6833 | |
| 6834 | HA_SPIN_INIT(&ckch_lock); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 6835 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 6836 | /* Try to register dedicated SSL/TLS protocol message callbacks for |
| 6837 | * heartbleed attack (CVE-2014-0160) and clienthello. |
| 6838 | */ |
| 6839 | hap_register_post_check(ssl_sock_register_msg_callbacks); |
| 6840 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 6841 | /* Try to free all callbacks that were registered by using |
| 6842 | * ssl_sock_register_msg_callback(). |
| 6843 | */ |
| 6844 | hap_register_post_deinit(ssl_sock_unregister_msg_callbacks); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6845 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 6846 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6847 | /* Compute and register the version string */ |
| 6848 | static void ssl_register_build_options() |
| 6849 | { |
| 6850 | char *ptr = NULL; |
| 6851 | int i; |
| 6852 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6853 | memprintf(&ptr, "Built with OpenSSL version : " |
| 6854 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 6855 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6856 | #else /* OPENSSL_IS_BORINGSSL */ |
| 6857 | OPENSSL_VERSION_TEXT |
| 6858 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6859 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 6860 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6861 | #endif |
| 6862 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 6863 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6864 | "no (library version too old)" |
| 6865 | #elif defined(OPENSSL_NO_TLSEXT) |
| 6866 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 6867 | #else |
| 6868 | "yes" |
| 6869 | #endif |
| 6870 | "", ptr); |
| 6871 | |
| 6872 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 6873 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 6874 | "yes" |
| 6875 | #else |
| 6876 | #ifdef OPENSSL_NO_TLSEXT |
| 6877 | "no (because of OPENSSL_NO_TLSEXT)" |
| 6878 | #else |
| 6879 | "no (version might be too old, 0.9.8f min needed)" |
| 6880 | #endif |
| 6881 | #endif |
| 6882 | "", ptr); |
| 6883 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 6884 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 6885 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 6886 | if (methodVersions[i].option) |
| 6887 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 6888 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6889 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6890 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 6891 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 6892 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 6893 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6894 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6895 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6896 | void ssl_free_engines(void) { |
| 6897 | struct ssl_engine_list *wl, *wlb; |
| 6898 | /* free up engine list */ |
| 6899 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 6900 | ENGINE_finish(wl->e); |
| 6901 | ENGINE_free(wl->e); |
| 6902 | LIST_DEL(&wl->list); |
| 6903 | free(wl); |
| 6904 | } |
| 6905 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 6906 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6907 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6908 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6909 | void ssl_free_dh(void) { |
| 6910 | if (local_dh_1024) { |
| 6911 | DH_free(local_dh_1024); |
| 6912 | local_dh_1024 = NULL; |
| 6913 | } |
| 6914 | if (local_dh_2048) { |
| 6915 | DH_free(local_dh_2048); |
| 6916 | local_dh_2048 = NULL; |
| 6917 | } |
| 6918 | if (local_dh_4096) { |
| 6919 | DH_free(local_dh_4096); |
| 6920 | local_dh_4096 = NULL; |
| 6921 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 6922 | if (global_dh) { |
| 6923 | DH_free(global_dh); |
| 6924 | global_dh = NULL; |
| 6925 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 6926 | } |
| 6927 | #endif |
| 6928 | |
| 6929 | __attribute__((destructor)) |
| 6930 | static void __ssl_sock_deinit(void) |
| 6931 | { |
| 6932 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6933 | if (ssl_ctx_lru_tree) { |
| 6934 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 6935 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 6936 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6937 | #endif |
| 6938 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6939 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6940 | ERR_remove_state(0); |
| 6941 | ERR_free_strings(); |
| 6942 | |
| 6943 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6944 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6945 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6946 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6947 | CRYPTO_cleanup_all_ex_data(); |
| 6948 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6949 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 6950 | } |
| 6951 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6952 | /* |
| 6953 | * Local variables: |
| 6954 | * c-indent-level: 8 |
| 6955 | * c-basic-offset: 8 |
| 6956 | * End: |
| 6957 | */ |