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> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 33 | #include <string.h> |
| 34 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 35 | |
| 36 | #include <sys/socket.h> |
| 37 | #include <sys/stat.h> |
| 38 | #include <sys/types.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 39 | #include <netdb.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 40 | #include <netinet/tcp.h> |
| 41 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 42 | #include <import/ebpttree.h> |
| 43 | #include <import/ebsttree.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 44 | #include <import/lru.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 46 | #include <haproxy/api.h> |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 47 | #include <haproxy/applet.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 48 | #include <haproxy/arg.h> |
| 49 | #include <haproxy/base64.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 50 | #include <haproxy/channel.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 51 | #include <haproxy/chunk.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 52 | #include <haproxy/cli.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 53 | #include <haproxy/connection.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 54 | #include <haproxy/dynbuf.h> |
Willy Tarreau | 8d36697 | 2020-05-27 16:10:29 +0200 | [diff] [blame] | 55 | #include <haproxy/errors.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 56 | #include <haproxy/fd.h> |
| 57 | #include <haproxy/freq_ctr.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 58 | #include <haproxy/frontend.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 59 | #include <haproxy/global.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 60 | #include <haproxy/http_rules.h> |
Willy Tarreau | aeed4a8 | 2020-06-04 22:01:04 +0200 | [diff] [blame] | 61 | #include <haproxy/log.h> |
Willy Tarreau | 6019fab | 2020-05-27 16:26:00 +0200 | [diff] [blame] | 62 | #include <haproxy/openssl-compat.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 63 | #include <haproxy/pattern-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/proto_tcp.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 65 | #include <haproxy/proxy.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 66 | #include <haproxy/sample.h> |
| 67 | #include <haproxy/sc_strm.h> |
Amaury Denoyelle | 92fa63f | 2022-09-30 18:11:13 +0200 | [diff] [blame] | 68 | #include <haproxy/quic_conn.h> |
Frédéric Lécaille | 748ece6 | 2022-05-21 23:58:40 +0200 | [diff] [blame] | 69 | #include <haproxy/quic_tp.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 70 | #include <haproxy/server.h> |
Willy Tarreau | 334099c | 2020-06-03 18:38:48 +0200 | [diff] [blame] | 71 | #include <haproxy/shctx.h> |
Willy Tarreau | 47d7f90 | 2020-06-04 14:25:47 +0200 | [diff] [blame] | 72 | #include <haproxy/ssl_ckch.h> |
Willy Tarreau | 52d8872 | 2020-06-04 14:29:23 +0200 | [diff] [blame] | 73 | #include <haproxy/ssl_crtlist.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 74 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | b2bd865 | 2020-06-04 14:21:22 +0200 | [diff] [blame] | 75 | #include <haproxy/ssl_utils.h> |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 76 | #include <haproxy/stats.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 77 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 78 | #include <haproxy/stream-t.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 79 | #include <haproxy/task.h> |
Willy Tarreau | c2f7c58 | 2020-06-02 18:15:32 +0200 | [diff] [blame] | 80 | #include <haproxy/ticks.h> |
Willy Tarreau | 92b4f13 | 2020-06-01 11:05:15 +0200 | [diff] [blame] | 81 | #include <haproxy/time.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 82 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 83 | #include <haproxy/vars.h> |
Tim Duesterhus | d5fc8fc | 2021-09-11 17:51:13 +0200 | [diff] [blame] | 84 | #include <haproxy/xxhash.h> |
Remi Tricot-Le Breton | 2e7d1eb | 2022-01-11 10:11:10 +0100 | [diff] [blame] | 85 | #include <haproxy/istbuf.h> |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 86 | #include <haproxy/ssl_ocsp.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 87 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 88 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 89 | /* ***** READ THIS before adding code here! ***** |
| 90 | * |
| 91 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 92 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 93 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 94 | * exclusively so that the whole code consistently uses the same macros. |
| 95 | * |
| 96 | * Whenever possible if a macro is missing in certain versions, it's better |
| 97 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 98 | */ |
| 99 | |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 100 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 101 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 102 | static struct eb_root cert_issuer_tree = EB_ROOT; /* issuers tree from "issuers-chain-path" */ |
| 103 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 104 | struct global_ssl global_ssl = { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 105 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 106 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 107 | #endif |
| 108 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 109 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 110 | #endif |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 111 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 112 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 113 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 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 |
Thomas Prückl | 1024393 | 2022-04-27 13:04:54 +0200 | [diff] [blame] | 128 | .hard_max_record = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 129 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 130 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 131 | .capture_buffer_size = 0, |
William Lallemand | 3af48e7 | 2020-02-03 17:15:52 +0100 | [diff] [blame] | 132 | .extra_files = SSL_GF_ALL, |
William Lallemand | 8e8581e | 2020-10-20 17:36:46 +0200 | [diff] [blame] | 133 | .extra_files_noext = 0, |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 134 | #ifdef HAVE_SSL_KEYLOG |
Remi Tricot-Le Breton | 5843237 | 2023-02-28 17:46:29 +0100 | [diff] [blame] | 135 | .keylog = 0, |
| 136 | #endif |
| 137 | #ifndef OPENSSL_NO_OCSP |
| 138 | .ocsp_update.delay_max = SSL_OCSP_UPDATE_DELAY_MAX, |
| 139 | .ocsp_update.delay_min = SSL_OCSP_UPDATE_DELAY_MIN, |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 140 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 141 | }; |
| 142 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 143 | static BIO_METHOD *ha_meth; |
| 144 | |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 145 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx", sizeof(struct ssl_sock_ctx)); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 146 | |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 147 | DECLARE_STATIC_POOL(ssl_sock_client_sni_pool, "ssl_sock_client_sni", TLSEXT_MAXLEN_host_name + 1); |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 148 | |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 149 | /* ssl stats module */ |
| 150 | enum { |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 151 | SSL_ST_SESS, |
| 152 | SSL_ST_REUSED_SESS, |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 153 | SSL_ST_FAILED_HANDSHAKE, |
Amaury Denoyelle | fbc3377 | 2020-11-03 17:10:01 +0100 | [diff] [blame] | 154 | |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 155 | SSL_ST_STATS_COUNT /* must be the last member of the enum */ |
| 156 | }; |
| 157 | |
| 158 | static struct name_desc ssl_stats[] = { |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 159 | [SSL_ST_SESS] = { .name = "ssl_sess", |
| 160 | .desc = "Total number of ssl sessions established" }, |
| 161 | [SSL_ST_REUSED_SESS] = { .name = "ssl_reused_sess", |
| 162 | .desc = "Total number of ssl sessions reused" }, |
| 163 | [SSL_ST_FAILED_HANDSHAKE] = { .name = "ssl_failed_handshake", |
| 164 | .desc = "Total number of failed handshake" }, |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | static struct ssl_counters { |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 168 | long long sess; |
| 169 | long long reused_sess; |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 170 | long long failed_handshake; |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 171 | } ssl_counters; |
| 172 | |
| 173 | static void ssl_fill_stats(void *data, struct field *stats) |
| 174 | { |
Amaury Denoyelle | fbc3377 | 2020-11-03 17:10:01 +0100 | [diff] [blame] | 175 | struct ssl_counters *counters = data; |
| 176 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 177 | stats[SSL_ST_SESS] = mkf_u64(FN_COUNTER, counters->sess); |
| 178 | stats[SSL_ST_REUSED_SESS] = mkf_u64(FN_COUNTER, counters->reused_sess); |
| 179 | stats[SSL_ST_FAILED_HANDSHAKE] = mkf_u64(FN_COUNTER, counters->failed_handshake); |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static struct stats_module ssl_stats_module = { |
| 183 | .name = "ssl", |
| 184 | .fill_stats = ssl_fill_stats, |
| 185 | .stats = ssl_stats, |
| 186 | .stats_count = SSL_ST_STATS_COUNT, |
| 187 | .counters = &ssl_counters, |
| 188 | .counters_size = sizeof(ssl_counters), |
| 189 | .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_LI|STATS_PX_CAP_BE|STATS_PX_CAP_SRV), |
| 190 | .clearable = 1, |
| 191 | }; |
| 192 | |
| 193 | INITCALL1(STG_REGISTER, stats_register_module, &ssl_stats_module); |
| 194 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 195 | /* CLI context for "show tls-keys" */ |
| 196 | struct show_keys_ctx { |
| 197 | struct tls_keys_ref *next_ref; /* next reference to be dumped */ |
| 198 | int names_only; /* non-zero = only show file names */ |
| 199 | int next_index; /* next index to be dumped */ |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 200 | int dump_entries; /* dump entries also */ |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 201 | enum { |
| 202 | SHOW_KEYS_INIT = 0, |
| 203 | SHOW_KEYS_LIST, |
| 204 | SHOW_KEYS_DONE, |
| 205 | } state; /* phase of the current dump */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 206 | }; |
| 207 | |
Willy Tarreau | 691d503 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 208 | /* ssl_sock_io_cb is exported to see it resolved in "show fd" */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 209 | struct task *ssl_sock_io_cb(struct task *, void *, unsigned int); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 210 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 211 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 212 | /* Methods to implement OpenSSL BIO */ |
| 213 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 214 | { |
| 215 | struct buffer tmpbuf; |
| 216 | struct ssl_sock_ctx *ctx; |
| 217 | int ret; |
| 218 | |
| 219 | ctx = BIO_get_data(h); |
| 220 | tmpbuf.size = num; |
| 221 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 222 | tmpbuf.data = num; |
| 223 | tmpbuf.head = 0; |
| 224 | 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] | 225 | 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] | 226 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 227 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 228 | } else if (ret == 0) |
| 229 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 234 | { |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | static int ha_ssl_puts(BIO *h, const char *str) |
| 240 | { |
| 241 | |
| 242 | return ha_ssl_write(h, str, strlen(str)); |
| 243 | } |
| 244 | |
| 245 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 246 | { |
| 247 | struct buffer tmpbuf; |
| 248 | struct ssl_sock_ctx *ctx; |
| 249 | int ret; |
| 250 | |
| 251 | ctx = BIO_get_data(h); |
| 252 | tmpbuf.size = size; |
| 253 | tmpbuf.area = buf; |
| 254 | tmpbuf.data = 0; |
| 255 | tmpbuf.head = 0; |
| 256 | 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] | 257 | 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] | 258 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 259 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 260 | } else if (ret == 0) |
| 261 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 262 | |
| 263 | return ret; |
| 264 | } |
| 265 | |
| 266 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 267 | { |
| 268 | int ret = 0; |
| 269 | switch (cmd) { |
| 270 | case BIO_CTRL_DUP: |
| 271 | case BIO_CTRL_FLUSH: |
| 272 | ret = 1; |
| 273 | break; |
| 274 | } |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | static int ha_ssl_new(BIO *h) |
| 279 | { |
| 280 | BIO_set_init(h, 1); |
| 281 | BIO_set_data(h, NULL); |
| 282 | BIO_clear_flags(h, ~0); |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | static int ha_ssl_free(BIO *data) |
| 287 | { |
| 288 | |
| 289 | return 1; |
| 290 | } |
| 291 | |
| 292 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 293 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 294 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 295 | static HA_RWLOCK_T *ssl_rwlocks; |
| 296 | |
| 297 | |
| 298 | unsigned long ssl_id_function(void) |
| 299 | { |
| 300 | return (unsigned long)tid; |
| 301 | } |
| 302 | |
| 303 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 304 | { |
| 305 | if (mode & CRYPTO_LOCK) { |
| 306 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 307 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 308 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 309 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 310 | } |
| 311 | else { |
| 312 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 313 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 314 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 315 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
| 319 | static int ssl_locking_init(void) |
| 320 | { |
| 321 | int i; |
| 322 | |
| 323 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 324 | if (!ssl_rwlocks) |
| 325 | return -1; |
| 326 | |
| 327 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 328 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 329 | |
| 330 | CRYPTO_set_id_callback(ssl_id_function); |
| 331 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 332 | |
| 333 | return 0; |
| 334 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 335 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 336 | #endif |
| 337 | |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 338 | __decl_thread(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 339 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 340 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 341 | |
| 342 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 343 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 344 | { |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 345 | X509_STORE *store = NULL; |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 346 | struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0); |
| 347 | if (ca_e) |
| 348 | store = ca_e->ca_store; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 349 | if (store_ctx && store) { |
| 350 | int i; |
| 351 | X509_OBJECT *obj; |
| 352 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 353 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 354 | obj = sk_X509_OBJECT_value(objs, i); |
| 355 | switch (X509_OBJECT_get_type(obj)) { |
| 356 | case X509_LU_X509: |
| 357 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 358 | break; |
| 359 | case X509_LU_CRL: |
| 360 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 361 | break; |
| 362 | default: |
| 363 | break; |
| 364 | } |
| 365 | } |
| 366 | return 1; |
| 367 | } |
| 368 | return 0; |
| 369 | } |
| 370 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 371 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 372 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 373 | { |
| 374 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 375 | return ssl_set_cert_crl_file(store_ctx, path); |
| 376 | } |
| 377 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 378 | /* |
| 379 | Extract CA_list from CA_file already in tree. |
| 380 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 381 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 382 | */ |
| 383 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 384 | { |
| 385 | struct ebmb_node *eb; |
| 386 | struct cafile_entry *ca_e; |
| 387 | |
| 388 | eb = ebst_lookup(&cafile_tree, path); |
| 389 | if (!eb) |
| 390 | return NULL; |
| 391 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 392 | |
| 393 | if (ca_e->ca_list == NULL) { |
| 394 | int i; |
| 395 | unsigned long key; |
| 396 | struct eb_root ca_name_tree = EB_ROOT; |
| 397 | struct eb64_node *node, *back; |
| 398 | struct { |
| 399 | struct eb64_node node; |
| 400 | X509_NAME *xname; |
| 401 | } *ca_name; |
| 402 | STACK_OF(X509_OBJECT) *objs; |
| 403 | STACK_OF(X509_NAME) *skn; |
| 404 | X509 *x; |
| 405 | X509_NAME *xn; |
| 406 | |
| 407 | skn = sk_X509_NAME_new_null(); |
| 408 | /* take x509 from cafile_tree */ |
| 409 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 410 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 411 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 412 | if (!x) |
| 413 | continue; |
| 414 | xn = X509_get_subject_name(x); |
| 415 | if (!xn) |
| 416 | continue; |
| 417 | /* Check for duplicates. */ |
| 418 | key = X509_NAME_hash(xn); |
| 419 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 420 | node && ca_name == NULL; |
| 421 | node = eb64_next(node)) { |
| 422 | ca_name = container_of(node, typeof(*ca_name), node); |
| 423 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 424 | ca_name = NULL; |
| 425 | } |
| 426 | /* find a duplicate */ |
| 427 | if (ca_name) |
| 428 | continue; |
| 429 | ca_name = calloc(1, sizeof *ca_name); |
| 430 | xn = X509_NAME_dup(xn); |
| 431 | if (!ca_name || |
| 432 | !xn || |
| 433 | !sk_X509_NAME_push(skn, xn)) { |
| 434 | free(ca_name); |
| 435 | X509_NAME_free(xn); |
| 436 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 437 | sk_X509_NAME_free(skn); |
| 438 | skn = NULL; |
| 439 | break; |
| 440 | } |
| 441 | ca_name->node.key = key; |
| 442 | ca_name->xname = xn; |
| 443 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 444 | } |
| 445 | ca_e->ca_list = skn; |
| 446 | /* remove temporary ca_name tree */ |
| 447 | node = eb64_first(&ca_name_tree); |
| 448 | while (node) { |
| 449 | ca_name = container_of(node, typeof(*ca_name), node); |
| 450 | back = eb64_next(node); |
| 451 | eb64_delete(node); |
| 452 | free(ca_name); |
| 453 | node = back; |
| 454 | } |
| 455 | } |
| 456 | return ca_e->ca_list; |
| 457 | } |
| 458 | |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 459 | struct pool_head *pool_head_ssl_capture __read_mostly = NULL; |
William Lallemand | 15e1694 | 2020-05-15 00:25:08 +0200 | [diff] [blame] | 460 | int ssl_capture_ptr_index = -1; |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 461 | int ssl_app_data_index = -1; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 462 | #ifdef USE_QUIC |
| 463 | int ssl_qc_app_data_index = -1; |
| 464 | #endif /* USE_QUIC */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 465 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 466 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 467 | int ssl_keylog_index = -1; |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 468 | struct pool_head *pool_head_ssl_keylog __read_mostly = NULL; |
| 469 | struct pool_head *pool_head_ssl_keylog_str __read_mostly = NULL; |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 470 | #endif |
| 471 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 472 | int ssl_client_crt_ref_index = -1; |
| 473 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 474 | /* Used to store the client's SNI in case of ClientHello callback error */ |
| 475 | int ssl_client_sni_index = -1; |
| 476 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 477 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 478 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 479 | #endif |
| 480 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 481 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 482 | unsigned int openssl_engines_initialized; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 483 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 484 | struct ssl_engine_list { |
| 485 | struct list list; |
| 486 | ENGINE *e; |
| 487 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 488 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 489 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 490 | #ifdef HAVE_SSL_PROVIDERS |
| 491 | struct list openssl_providers = LIST_HEAD_INIT(openssl_providers); |
| 492 | struct ssl_provider_list { |
| 493 | struct list list; |
| 494 | OSSL_PROVIDER *provider; |
| 495 | }; |
| 496 | #endif |
| 497 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 498 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 499 | static int ssl_dh_ptr_index = -1; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 500 | static HASSL_DH *global_dh = NULL; |
| 501 | static HASSL_DH *local_dh_1024 = NULL; |
| 502 | static HASSL_DH *local_dh_2048 = NULL; |
| 503 | static HASSL_DH *local_dh_4096 = NULL; |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 504 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 505 | static DH *ssl_get_tmp_dh_cbk(SSL *ssl, int export, int keylen); |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 506 | #else |
| 507 | static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey); |
| 508 | #endif |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 509 | #endif /* OPENSSL_NO_DH */ |
| 510 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 511 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 512 | /* X509V3 Extensions that will be added on generated certificates */ |
| 513 | #define X509V3_EXT_SIZE 5 |
| 514 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 515 | "basicConstraints", |
| 516 | "nsComment", |
| 517 | "subjectKeyIdentifier", |
| 518 | "authorityKeyIdentifier", |
| 519 | "keyUsage", |
| 520 | }; |
| 521 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 522 | "CA:FALSE", |
| 523 | "\"OpenSSL Generated Certificate\"", |
| 524 | "hash", |
| 525 | "keyid,issuer:always", |
| 526 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 527 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 528 | /* LRU cache to store generated certificate */ |
| 529 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 530 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 531 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 532 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 533 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 534 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 535 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 536 | /* The order here matters for picking a default context, |
| 537 | * keep the most common keytype at the bottom of the list |
| 538 | */ |
| 539 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 540 | "dsa", |
| 541 | "ecdsa", |
| 542 | "rsa" |
| 543 | }; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 544 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 545 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 546 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 547 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 548 | /* Dedicated callback functions for heartbeat and clienthello. |
| 549 | */ |
| 550 | #ifdef TLS1_RT_HEARTBEAT |
| 551 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 552 | int content_type, const void *buf, size_t len, |
| 553 | SSL *ssl); |
| 554 | #endif |
| 555 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 556 | int content_type, const void *buf, size_t len, |
| 557 | SSL *ssl); |
| 558 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 559 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 560 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 561 | int content_type, const void *buf, size_t len, |
| 562 | SSL *ssl); |
| 563 | #endif |
| 564 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 565 | /* List head of all registered SSL/TLS protocol message callbacks. */ |
| 566 | struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks); |
| 567 | |
| 568 | /* Registers the function <func> in order to be called on SSL/TLS protocol |
| 569 | * message processing. It will return 0 if the function <func> is not set |
| 570 | * or if it fails to allocate memory. |
| 571 | */ |
| 572 | int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func) |
| 573 | { |
| 574 | struct ssl_sock_msg_callback *cbk; |
| 575 | |
| 576 | if (!func) |
| 577 | return 0; |
| 578 | |
| 579 | cbk = calloc(1, sizeof(*cbk)); |
| 580 | if (!cbk) { |
| 581 | ha_alert("out of memory in ssl_sock_register_msg_callback().\n"); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | cbk->func = func; |
| 586 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 587 | LIST_APPEND(&ssl_sock_msg_callbacks, &cbk->list); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 588 | |
| 589 | return 1; |
| 590 | } |
| 591 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 592 | /* Used to register dedicated SSL/TLS protocol message callbacks. |
| 593 | */ |
| 594 | static int ssl_sock_register_msg_callbacks(void) |
| 595 | { |
| 596 | #ifdef TLS1_RT_HEARTBEAT |
| 597 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat)) |
| 598 | return ERR_ABORT; |
| 599 | #endif |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 600 | if (global_ssl.capture_buffer_size > 0) { |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 601 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello)) |
| 602 | return ERR_ABORT; |
| 603 | } |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 604 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 605 | if (global_ssl.keylog > 0) { |
| 606 | if (!ssl_sock_register_msg_callback(ssl_init_keylog)) |
| 607 | return ERR_ABORT; |
| 608 | } |
| 609 | #endif |
| 610 | |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 611 | return ERR_NONE; |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 612 | } |
| 613 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 614 | /* Used to free all SSL/TLS protocol message callbacks that were |
| 615 | * registered by using ssl_sock_register_msg_callback(). |
| 616 | */ |
| 617 | static void ssl_sock_unregister_msg_callbacks(void) |
| 618 | { |
| 619 | struct ssl_sock_msg_callback *cbk, *cbkback; |
| 620 | |
| 621 | list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 622 | LIST_DELETE(&cbk->list); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 623 | free(cbk); |
| 624 | } |
| 625 | } |
| 626 | |
Willy Tarreau | de82795 | 2022-04-11 10:43:28 +0200 | [diff] [blame] | 627 | static struct ssl_sock_ctx *ssl_sock_get_ctx(struct connection *conn) |
| 628 | { |
| 629 | if (!conn || conn->xprt != xprt_get(XPRT_SSL) || !conn->xprt_ctx) |
| 630 | return NULL; |
| 631 | |
| 632 | return (struct ssl_sock_ctx *)conn->xprt_ctx; |
| 633 | } |
| 634 | |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 635 | SSL *ssl_sock_get_ssl_object(struct connection *conn) |
| 636 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 637 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 638 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 639 | return ctx ? ctx->ssl : NULL; |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 640 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 641 | /* |
| 642 | * This function gives the detail of the SSL error. It is used only |
| 643 | * if the debug mode and the verbose mode are activated. It dump all |
| 644 | * the SSL error until the stack was empty. |
| 645 | */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 646 | static forceinline void ssl_sock_dump_errors(struct connection *conn, |
| 647 | struct quic_conn *qc) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 648 | { |
| 649 | unsigned long ret; |
| 650 | |
| 651 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 652 | while(1) { |
Remi Tricot-Le Breton | 1effd9a | 2022-02-11 12:04:44 +0100 | [diff] [blame] | 653 | const char *func = NULL; |
| 654 | ERR_peek_error_func(&func); |
| 655 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 656 | ret = ERR_get_error(); |
| 657 | if (ret == 0) |
| 658 | return; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 659 | if (conn) { |
| 660 | fprintf(stderr, "fd[%#x] OpenSSL error[0x%lx] %s: %s\n", |
| 661 | conn_fd(conn), ret, |
| 662 | func, ERR_reason_error_string(ret)); |
| 663 | } |
| 664 | #ifdef USE_QUIC |
| 665 | else { |
| 666 | /* TODO: we are not sure <conn> is always initialized for QUIC connections */ |
| 667 | fprintf(stderr, "qc @%p OpenSSL error[0x%lx] %s: %s\n", qc, ret, |
| 668 | func, ERR_reason_error_string(ret)); |
| 669 | } |
| 670 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 671 | } |
| 672 | } |
| 673 | } |
| 674 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 675 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 676 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 677 | 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] | 678 | { |
| 679 | int err_code = ERR_ABORT; |
| 680 | ENGINE *engine; |
| 681 | struct ssl_engine_list *el; |
| 682 | |
| 683 | /* grab the structural reference to the engine */ |
| 684 | engine = ENGINE_by_id(engine_id); |
| 685 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 686 | 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] | 687 | goto fail_get; |
| 688 | } |
| 689 | |
| 690 | if (!ENGINE_init(engine)) { |
| 691 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 692 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 693 | goto fail_init; |
| 694 | } |
| 695 | |
| 696 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 697 | 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] | 698 | goto fail_set_method; |
| 699 | } |
| 700 | |
| 701 | el = calloc(1, sizeof(*el)); |
Remi Tricot-Le Breton | 612b2c3 | 2021-05-12 17:45:21 +0200 | [diff] [blame] | 702 | if (!el) |
| 703 | goto fail_alloc; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 704 | el->e = engine; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 705 | LIST_INSERT(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 706 | nb_engines++; |
| 707 | if (global_ssl.async) |
| 708 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 709 | return 0; |
| 710 | |
Remi Tricot-Le Breton | 612b2c3 | 2021-05-12 17:45:21 +0200 | [diff] [blame] | 711 | fail_alloc: |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 712 | fail_set_method: |
| 713 | /* release the functional reference from ENGINE_init() */ |
| 714 | ENGINE_finish(engine); |
| 715 | |
| 716 | fail_init: |
| 717 | /* release the structural reference from ENGINE_by_id() */ |
| 718 | ENGINE_free(engine); |
| 719 | |
| 720 | fail_get: |
| 721 | return err_code; |
| 722 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 723 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 724 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 725 | #ifdef HAVE_SSL_PROVIDERS |
| 726 | int ssl_init_provider(const char *provider_name) |
| 727 | { |
| 728 | int err_code = ERR_ABORT; |
| 729 | struct ssl_provider_list *prov = NULL; |
| 730 | |
| 731 | prov = calloc(1, sizeof(*prov)); |
| 732 | if (!prov) { |
| 733 | ha_alert("ssl-provider %s: memory allocation failure\n", provider_name); |
| 734 | goto error; |
| 735 | } |
| 736 | |
| 737 | if ((prov->provider = OSSL_PROVIDER_load(NULL, provider_name)) == NULL) { |
| 738 | ha_alert("ssl-provider %s: unknown provider\n", provider_name); |
| 739 | goto error; |
| 740 | } |
| 741 | |
| 742 | LIST_INSERT(&openssl_providers, &prov->list); |
| 743 | |
| 744 | return 0; |
| 745 | |
| 746 | error: |
| 747 | ha_free(&prov); |
| 748 | return err_code; |
| 749 | } |
| 750 | #endif /* HAVE_SSL_PROVIDERS */ |
| 751 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 752 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 753 | /* |
| 754 | * openssl async fd handler |
| 755 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 756 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 757 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 758 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 759 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 760 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 761 | * to poll this fd until it is requested |
| 762 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 763 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 764 | fd_cant_recv(fd); |
| 765 | |
| 766 | /* crypto engine is available, let's notify the associated |
| 767 | * connection that it can pursue its processing. |
| 768 | */ |
Olivier Houchard | a459826 | 2020-09-15 22:16:02 +0200 | [diff] [blame] | 769 | tasklet_wakeup(ctx->wait_event.tasklet); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 770 | } |
| 771 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 772 | /* |
| 773 | * openssl async delayed SSL_free handler |
| 774 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 775 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 776 | { |
| 777 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 778 | OSSL_ASYNC_FD all_fd[32]; |
| 779 | size_t num_all_fds = 0; |
| 780 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 781 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 782 | /* We suppose that the async job for a same SSL * |
| 783 | * are serialized. So if we are awake it is |
| 784 | * because the running job has just finished |
| 785 | * and we can remove all async fds safely |
| 786 | */ |
| 787 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 788 | if (num_all_fds > 32) { |
| 789 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 790 | return; |
| 791 | } |
| 792 | |
| 793 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 794 | for (i=0 ; i < num_all_fds ; i++) { |
| 795 | /* We want to remove the fd from the fdtab |
| 796 | * but we flag it to disown because the |
| 797 | * close is performed by the engine itself |
| 798 | */ |
| 799 | fdtab[all_fd[i]].state |= FD_DISOWN; |
| 800 | fd_delete(all_fd[i]); |
| 801 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 802 | |
| 803 | /* 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] | 804 | SSL_free(ssl); |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 805 | _HA_ATOMIC_DEC(&global.sslconns); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 806 | _HA_ATOMIC_DEC(&jobs); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 807 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 808 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 809 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 810 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 811 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 812 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 813 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 814 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 815 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 816 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 817 | size_t num_add_fds = 0; |
| 818 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 819 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 820 | |
| 821 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 822 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 823 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 824 | 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] | 825 | return; |
| 826 | } |
| 827 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 828 | 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] | 829 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 830 | /* We remove unused fds from the fdtab */ |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 831 | for (i=0 ; i < num_del_fds ; i++) { |
| 832 | /* We want to remove the fd from the fdtab |
| 833 | * but we flag it to disown because the |
| 834 | * close is performed by the engine itself |
| 835 | */ |
| 836 | fdtab[del_fd[i]].state |= FD_DISOWN; |
| 837 | fd_delete(del_fd[i]); |
| 838 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 839 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 840 | /* We add new fds to the fdtab */ |
| 841 | for (i=0 ; i < num_add_fds ; i++) { |
Willy Tarreau | 27a3245 | 2022-07-07 08:29:00 +0200 | [diff] [blame] | 842 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tgid, ti->ltid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 845 | num_add_fds = 0; |
| 846 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 847 | if (num_add_fds > 32) { |
| 848 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 849 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 850 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 851 | |
| 852 | /* We activate the polling for all known async fds */ |
| 853 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 854 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 855 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 856 | /* To ensure that the fd cache won't be used |
| 857 | * We'll prefer to catch a real RD event |
| 858 | * because handling an EAGAIN on this fd will |
| 859 | * result in a context switch and also |
| 860 | * some engines uses a fd in blocking mode. |
| 861 | */ |
| 862 | fd_cant_recv(add_fd[i]); |
| 863 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 864 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 865 | } |
| 866 | #endif |
| 867 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 868 | |
| 869 | /* |
| 870 | * Initialize an HMAC context <hctx> using the <key> and <md> parameters. |
| 871 | * Returns -1 in case of error, 1 otherwise. |
| 872 | */ |
| 873 | static int ssl_hmac_init(MAC_CTX *hctx, unsigned char *key, int key_len, const EVP_MD *md) |
| 874 | { |
| 875 | #ifdef HAVE_OSSL_PARAM |
| 876 | OSSL_PARAM params[3]; |
| 877 | |
| 878 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, key, key_len); |
| 879 | params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, (char*)EVP_MD_name(md), 0); |
| 880 | params[2] = OSSL_PARAM_construct_end(); |
| 881 | if (EVP_MAC_CTX_set_params(hctx, params) == 0) |
| 882 | return -1; /* error in mac initialisation */ |
| 883 | |
| 884 | #else |
| 885 | HMAC_Init_ex(hctx, key, key_len, md, NULL); |
| 886 | #endif |
| 887 | return 1; |
| 888 | } |
| 889 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 890 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
Remi Tricot-Le Breton | 8ea1f5f | 2022-02-08 17:45:58 +0100 | [diff] [blame] | 891 | |
| 892 | static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, MAC_CTX *hctx, int enc) |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 893 | { |
Frédéric Lécaille | 6aec1f3 | 2022-09-06 17:04:55 +0200 | [diff] [blame] | 894 | struct tls_keys_ref *ref = NULL; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 895 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 896 | int head; |
| 897 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 898 | int ret = -1; /* error by default */ |
Frédéric Lécaille | 6aec1f3 | 2022-09-06 17:04:55 +0200 | [diff] [blame] | 899 | struct connection *conn = SSL_get_ex_data(s, ssl_app_data_index); |
| 900 | #ifdef USE_QUIC |
| 901 | struct quic_conn *qc = SSL_get_ex_data(s, ssl_qc_app_data_index); |
| 902 | #endif |
| 903 | |
| 904 | if (conn) |
| 905 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
| 906 | #ifdef USE_QUIC |
| 907 | else if (qc) |
| 908 | ref = qc->li->bind_conf->keys_ref; |
| 909 | #endif |
| 910 | |
| 911 | if (!ref) { |
| 912 | /* must never happen */ |
| 913 | ABORT_NOW(); |
| 914 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 915 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 916 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 917 | |
| 918 | keys = ref->tlskeys; |
| 919 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 920 | |
| 921 | if (enc) { |
| 922 | memcpy(key_name, keys[head].name, 16); |
| 923 | |
| 924 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 925 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 926 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 927 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 928 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 929 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 930 | goto end; |
| 931 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 932 | if (ssl_hmac_init(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0) |
| 933 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 934 | ret = 1; |
| 935 | } |
| 936 | else if (ref->key_size_bits == 256 ) { |
| 937 | |
| 938 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 939 | goto end; |
| 940 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 941 | if (ssl_hmac_init(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0) |
| 942 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 943 | ret = 1; |
| 944 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 945 | } else { |
| 946 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 947 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 948 | goto found; |
| 949 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 950 | ret = 0; |
| 951 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 952 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 953 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 954 | if (ref->key_size_bits == 128) { |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 955 | if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0) |
| 956 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 957 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 958 | goto end; |
| 959 | /* 2 for key renewal, 1 if current key is still valid */ |
| 960 | ret = i ? 2 : 1; |
| 961 | } |
| 962 | else if (ref->key_size_bits == 256) { |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 963 | if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0) |
| 964 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 965 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 966 | goto end; |
| 967 | /* 2 for key renewal, 1 if current key is still valid */ |
| 968 | ret = i ? 2 : 1; |
| 969 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 970 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 971 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 972 | end: |
| 973 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 974 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 975 | } |
| 976 | |
| 977 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 978 | { |
| 979 | struct tls_keys_ref *ref; |
| 980 | |
| 981 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 982 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 983 | return ref; |
| 984 | return NULL; |
| 985 | } |
| 986 | |
| 987 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 988 | { |
| 989 | struct tls_keys_ref *ref; |
| 990 | |
| 991 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 992 | if (ref->unique_id == unique_id) |
| 993 | return ref; |
| 994 | return NULL; |
| 995 | } |
| 996 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 997 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 998 | * match existing ones, this function returns -1 |
| 999 | * else it returns 0 on success. |
| 1000 | */ |
| 1001 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1002 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1003 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1004 | if (ref->key_size_bits == 128) { |
| 1005 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1006 | return -1; |
| 1007 | } |
| 1008 | else if (ref->key_size_bits == 256) { |
| 1009 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1010 | return -1; |
| 1011 | } |
| 1012 | else |
| 1013 | return -1; |
| 1014 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1015 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1016 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1017 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1018 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1019 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1020 | |
| 1021 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1022 | } |
| 1023 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1024 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1025 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1026 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1027 | |
| 1028 | if(!ref) { |
| 1029 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1030 | return 1; |
| 1031 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1032 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1033 | memprintf(err, "Invalid key size"); |
| 1034 | return 1; |
| 1035 | } |
| 1036 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1037 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1038 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1039 | |
| 1040 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1041 | * automatic ids. It's called just after the basic checks. It returns |
| 1042 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1043 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1044 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1045 | { |
| 1046 | int i = 0; |
| 1047 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1048 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1049 | |
| 1050 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1051 | if (ref->unique_id == -1) { |
| 1052 | /* Look for the first free id. */ |
| 1053 | while (1) { |
| 1054 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1055 | if (ref2->unique_id == i) { |
| 1056 | i++; |
| 1057 | break; |
| 1058 | } |
| 1059 | } |
| 1060 | if (&ref2->list == &tlskeys_reference) |
| 1061 | break; |
| 1062 | } |
| 1063 | |
| 1064 | /* Uses the unique id and increment it for the next entry. */ |
| 1065 | ref->unique_id = i; |
| 1066 | i++; |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | /* This sort the reference list by id. */ |
| 1071 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1072 | LIST_DELETE(&ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1073 | list_for_each_entry(ref3, &tkr, list) { |
| 1074 | if (ref->unique_id < ref3->unique_id) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1075 | LIST_APPEND(&ref3->list, &ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1076 | break; |
| 1077 | } |
| 1078 | } |
| 1079 | if (&ref3->list == &tkr) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1080 | LIST_APPEND(&tkr, &ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | /* swap root */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1084 | LIST_INSERT(&tkr, &tlskeys_reference); |
| 1085 | LIST_DELETE(&tkr); |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1086 | return ERR_NONE; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1087 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1088 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1089 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1090 | |
Ilya Shipitsin | b3201a3 | 2020-10-18 09:11:50 +0500 | [diff] [blame] | 1091 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1092 | /* |
| 1093 | * 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] | 1094 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1095 | * status extension, the issuer's certificate is mandatory. It should be |
| 1096 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1097 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1098 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1099 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1100 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1101 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1102 | * |
| 1103 | * 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] | 1104 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1105 | */ |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 1106 | static int ssl_sock_load_ocsp(const char *path, SSL_CTX *ctx, struct ckch_data *data, STACK_OF(X509) *chain) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1107 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1108 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1109 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1110 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1111 | char *warn = NULL; |
| 1112 | unsigned char *p; |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1113 | #ifndef USE_OPENSSL_WOLFSSL |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1114 | void (*callback) (void); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1115 | #else |
| 1116 | tlsextStatusCb callback; |
| 1117 | #endif |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1118 | struct buffer *ocsp_uri = get_trash_chunk(); |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame^] | 1119 | char *err = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1120 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1121 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 1122 | x = data->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1123 | if (!x) |
| 1124 | goto out; |
| 1125 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame^] | 1126 | ssl_ocsp_get_uri_from_cert(x, ocsp_uri, &err); |
| 1127 | /* We should have an "OCSP URI" field in order for auto update to work. */ |
| 1128 | if (data->ocsp_update_mode == SSL_SOCK_OCSP_UPDATE_ON && b_data(ocsp_uri) == 0) |
| 1129 | goto out; |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1130 | |
| 1131 | /* In case of ocsp update mode set to 'on', this function might be |
| 1132 | * called with no known ocsp response. If no ocsp uri can be found in |
| 1133 | * the certificate, nothing needs to be done here. */ |
| 1134 | if (!data->ocsp_response) { |
| 1135 | if (data->ocsp_update_mode != SSL_SOCK_OCSP_UPDATE_ON || b_data(ocsp_uri) == 0) { |
| 1136 | ret = 0; |
| 1137 | goto out; |
| 1138 | } |
| 1139 | } |
| 1140 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 1141 | issuer = data->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1142 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1143 | if (chain) { |
| 1144 | /* check if one of the certificate of the chain is the issuer */ |
| 1145 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1146 | X509 *ti = sk_X509_value(chain, i); |
| 1147 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1148 | issuer = ti; |
| 1149 | break; |
| 1150 | } |
| 1151 | } |
| 1152 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1153 | if (!issuer) |
| 1154 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1155 | |
Remi Tricot-Le Breton | 2d1daa8 | 2023-01-09 12:02:47 +0100 | [diff] [blame] | 1156 | if (!data->ocsp_cid) |
| 1157 | data->ocsp_cid = OCSP_cert_to_id(0, x, issuer); |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1158 | if (!data->ocsp_cid) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1159 | goto out; |
| 1160 | |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1161 | i = i2d_OCSP_CERTID(data->ocsp_cid, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1162 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1163 | goto out; |
| 1164 | |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 1165 | ocsp = calloc(1, sizeof(*ocsp)+strlen(path)+1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1166 | if (!ocsp) |
| 1167 | goto out; |
| 1168 | |
| 1169 | p = ocsp->key_data; |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1170 | ocsp->key_length = i2d_OCSP_CERTID(data->ocsp_cid, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1171 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1172 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1173 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1174 | if (iocsp == ocsp) |
| 1175 | ocsp = NULL; |
| 1176 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1177 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1178 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1179 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1180 | #endif |
| 1181 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1182 | |
| 1183 | if (!callback) { |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1184 | struct ocsp_cbk_arg *cb_arg; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1185 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1186 | |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1187 | cb_arg = calloc(1, sizeof(*cb_arg)); |
| 1188 | if (!cb_arg) |
| 1189 | goto out; |
| 1190 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1191 | cb_arg->is_single = 1; |
| 1192 | cb_arg->s_ocsp = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1193 | iocsp->refcount++; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1194 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1195 | pkey = X509_get_pubkey(x); |
| 1196 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1197 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1198 | |
| 1199 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1200 | 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 */ |
| 1201 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1202 | } else { |
| 1203 | /* |
| 1204 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1205 | * Update that cb_arg with the new cert's staple |
| 1206 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1207 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1208 | struct certificate_ocsp *tmp_ocsp; |
| 1209 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1210 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1211 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1212 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1213 | cb_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1214 | |
| 1215 | /* |
| 1216 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1217 | * the order of operations below matter, take care when changing it |
| 1218 | */ |
| 1219 | tmp_ocsp = cb_arg->s_ocsp; |
| 1220 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1221 | cb_arg->s_ocsp = NULL; |
| 1222 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1223 | cb_arg->is_single = 0; |
| 1224 | cb_arg->single_kt = 0; |
| 1225 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1226 | pkey = X509_get_pubkey(x); |
| 1227 | key_type = EVP_PKEY_base_id(pkey); |
| 1228 | EVP_PKEY_free(pkey); |
| 1229 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1230 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1231 | if (index >= 0 && !cb_arg->m_ocsp[index]) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1232 | cb_arg->m_ocsp[index] = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1233 | iocsp->refcount++; |
| 1234 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1235 | } |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1236 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1237 | |
| 1238 | ret = 0; |
| 1239 | |
| 1240 | warn = NULL; |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1241 | if (data->ocsp_response && ssl_sock_load_ocsp_response(data->ocsp_response, iocsp, data->ocsp_cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1242 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1243 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1244 | } |
| 1245 | |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1246 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame^] | 1247 | /* Do not insert the same certificate_ocsp structure in the |
| 1248 | * update tree more than once. */ |
| 1249 | if (!ocsp) { |
| 1250 | /* Issuer certificate is not included in the certificate |
| 1251 | * chain, it will have to be treated separately during |
| 1252 | * ocsp response validation. */ |
| 1253 | if (issuer == data->ocsp_issuer) { |
| 1254 | iocsp->issuer = issuer; |
| 1255 | X509_up_ref(issuer); |
| 1256 | } |
| 1257 | if (data->chain) |
| 1258 | iocsp->chain = X509_chain_up_ref(data->chain); |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1259 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame^] | 1260 | iocsp->uri = calloc(1, sizeof(*iocsp->uri)); |
| 1261 | if (!chunk_dup(iocsp->uri, ocsp_uri)) { |
| 1262 | ha_free(&iocsp->uri); |
| 1263 | goto out; |
| 1264 | } |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1265 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame^] | 1266 | strcpy(iocsp->path, path); |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 1267 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame^] | 1268 | if (data->ocsp_update_mode == SSL_SOCK_OCSP_UPDATE_ON) { |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1269 | ssl_ocsp_update_insert(iocsp); |
Remi Tricot-Le Breton | 86d1e0b | 2023-03-02 15:49:53 +0100 | [diff] [blame] | 1270 | /* If we are during init the update task is not |
| 1271 | * scheduled yet so a wakeup won't do anything. |
| 1272 | * Otherwise, if the OCSP was added through the CLI, we |
| 1273 | * wake the task up to manage the case of a new entry |
| 1274 | * that needs to be updated before the previous first |
| 1275 | * entry. |
| 1276 | */ |
| 1277 | if (ocsp_update_task) |
| 1278 | task_wakeup(ocsp_update_task, TASK_WOKEN_MSG); |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1279 | } |
| 1280 | } |
| 1281 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1282 | out: |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1283 | if (ret && data->ocsp_cid) |
| 1284 | OCSP_CERTID_free(data->ocsp_cid); |
| 1285 | |
| 1286 | if (!ret && data->ocsp_response) { |
| 1287 | ha_free(&data->ocsp_response->area); |
| 1288 | ha_free(&data->ocsp_response); |
| 1289 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1290 | |
| 1291 | if (ocsp) |
Remi Tricot-Le Breton | aa529f7 | 2022-11-03 15:16:49 +0100 | [diff] [blame] | 1292 | ssl_sock_free_ocsp(ocsp); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1293 | |
| 1294 | if (warn) |
| 1295 | free(warn); |
| 1296 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1297 | return ret; |
| 1298 | } |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 1299 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 1300 | #endif |
| 1301 | |
| 1302 | #ifdef OPENSSL_IS_BORINGSSL |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 1303 | static int ssl_sock_load_ocsp(const char *path, SSL_CTX *ctx, struct ckch_data *data, STACK_OF(X509) *chain) |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1304 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1305 | 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] | 1306 | } |
| 1307 | #endif |
| 1308 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1309 | |
Ilya Shipitsin | 7bbf586 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 1310 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1311 | |
| 1312 | #define CT_EXTENSION_TYPE 18 |
| 1313 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1314 | int sctl_ex_index = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1315 | |
| 1316 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1317 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1318 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1319 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1320 | *out = (unsigned char *) sctl->area; |
| 1321 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1322 | |
| 1323 | return 1; |
| 1324 | } |
| 1325 | |
| 1326 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1327 | { |
| 1328 | return 1; |
| 1329 | } |
| 1330 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1331 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1332 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1333 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1334 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1335 | 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] | 1336 | goto out; |
| 1337 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1338 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1339 | |
| 1340 | ret = 0; |
| 1341 | |
| 1342 | out: |
| 1343 | return ret; |
| 1344 | } |
| 1345 | |
| 1346 | #endif |
| 1347 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1348 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1349 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1350 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1351 | #ifdef USE_QUIC |
| 1352 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1353 | #endif /* USE_QUIC */ |
| 1354 | struct ssl_sock_ctx *ctx = NULL; |
| 1355 | |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1356 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1357 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1358 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1359 | if (conn) |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 1360 | ctx = conn_get_ssl_sock_ctx(conn); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1361 | #ifdef USE_QUIC |
| 1362 | else if (qc) |
| 1363 | ctx = qc->xprt_ctx; |
| 1364 | #endif /* USE_QUIC */ |
Amaury Denoyelle | 7c564bf | 2022-01-24 11:04:05 +0100 | [diff] [blame] | 1365 | |
| 1366 | if (!ctx) { |
| 1367 | /* must never happen */ |
| 1368 | ABORT_NOW(); |
| 1369 | return; |
| 1370 | } |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1371 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1372 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1373 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1374 | * change this to #if and do not assign a default value to this macro! |
| 1375 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1376 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1377 | /* Disable renegotiation (CVE-2009-3555) */ |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1378 | if (conn && (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] | 1379 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1380 | conn->err_code = CO_ER_SSL_RENEG; |
| 1381 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1382 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1383 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1384 | |
| 1385 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1386 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1387 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1388 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1389 | consider that the buffering was activated, |
| 1390 | so we rise the output buffer size from 4k |
| 1391 | to 16k */ |
| 1392 | write_bio = SSL_get_wbio(ssl); |
| 1393 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1394 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1395 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1396 | } |
| 1397 | } |
| 1398 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1399 | } |
| 1400 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1401 | /* Callback is called for each certificate of the chain during a verify |
| 1402 | ok is set to 1 if preverify detect no error on current certificate. |
| 1403 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1404 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1405 | { |
| 1406 | SSL *ssl; |
| 1407 | struct connection *conn; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1408 | struct ssl_sock_ctx *ctx = NULL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1409 | int err, depth; |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1410 | X509 *client_crt; |
| 1411 | STACK_OF(X509) *certs; |
Frédéric Lécaille | 48e46f9 | 2022-10-14 09:34:00 +0200 | [diff] [blame] | 1412 | struct bind_conf *bind_conf = NULL; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1413 | struct quic_conn *qc = NULL; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1414 | |
| 1415 | 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] | 1416 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1417 | client_crt = SSL_get_ex_data(ssl, ssl_client_crt_ref_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1418 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1419 | if (conn) { |
| 1420 | bind_conf = __objt_listener(conn->target)->bind_conf; |
| 1421 | ctx = __conn_get_ssl_sock_ctx(conn); |
| 1422 | } |
| 1423 | #ifdef USE_QUIC |
| 1424 | else { |
| 1425 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Amaury Denoyelle | ba303de | 2022-10-17 18:46:49 +0200 | [diff] [blame] | 1426 | BUG_ON(!qc); /* Must never happen */ |
| 1427 | bind_conf = qc->li->bind_conf; |
| 1428 | ctx = qc->xprt_ctx; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1429 | } |
| 1430 | #endif |
| 1431 | |
Frédéric Lécaille | 48e46f9 | 2022-10-14 09:34:00 +0200 | [diff] [blame] | 1432 | BUG_ON(!ctx || !bind_conf); |
Christopher Faulet | 881cce9 | 2022-11-23 09:27:13 +0100 | [diff] [blame] | 1433 | ALREADY_CHECKED(ctx); |
| 1434 | ALREADY_CHECKED(bind_conf); |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1435 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1436 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1437 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1438 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1439 | err = X509_STORE_CTX_get_error(x_store); |
| 1440 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1441 | if (ok) /* no errors */ |
| 1442 | return ok; |
| 1443 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1444 | /* Keep a reference to the client's certificate in order to be able to |
| 1445 | * dump some fetches values in a log even when the verification process |
| 1446 | * fails. */ |
| 1447 | if (depth == 0) { |
| 1448 | X509_free(client_crt); |
| 1449 | client_crt = X509_STORE_CTX_get0_cert(x_store); |
| 1450 | if (client_crt) { |
| 1451 | X509_up_ref(client_crt); |
| 1452 | SSL_set_ex_data(ssl, ssl_client_crt_ref_index, client_crt); |
| 1453 | } |
| 1454 | } |
| 1455 | else { |
| 1456 | /* An error occurred on a CA certificate of the certificate |
| 1457 | * chain, we might never call this verify callback on the client |
| 1458 | * certificate's depth (which is 0) so we try to store the |
| 1459 | * reference right now. */ |
Remi Tricot-Le Breton | f95c295 | 2021-08-20 09:51:23 +0200 | [diff] [blame] | 1460 | certs = X509_STORE_CTX_get1_chain(x_store); |
| 1461 | if (certs) { |
| 1462 | client_crt = sk_X509_value(certs, 0); |
| 1463 | if (client_crt) { |
| 1464 | X509_up_ref(client_crt); |
| 1465 | SSL_set_ex_data(ssl, ssl_client_crt_ref_index, client_crt); |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1466 | } |
| 1467 | sk_X509_pop_free(certs, X509_free); |
| 1468 | } |
| 1469 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1470 | |
| 1471 | /* check if CA error needs to be ignored */ |
| 1472 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1473 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1474 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1475 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1476 | } |
| 1477 | |
Remi Tricot-Le Breton | 9b25982 | 2022-11-10 10:48:58 +0100 | [diff] [blame] | 1478 | if (err <= SSL_MAX_VFY_ERROR_CODE && |
William Lallemand | 4639689 | 2022-11-10 16:45:24 +0100 | [diff] [blame] | 1479 | cert_ignerr_bitfield_get(bind_conf->ca_ignerr_bitfield, err)) |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1480 | goto err_ignored; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1481 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1482 | /* TODO: for QUIC connection, this error code is lost */ |
| 1483 | if (conn) |
| 1484 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1485 | return 0; |
| 1486 | } |
| 1487 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1488 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1489 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1490 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1491 | /* check if certificate error needs to be ignored */ |
Remi Tricot-Le Breton | 9b25982 | 2022-11-10 10:48:58 +0100 | [diff] [blame] | 1492 | if (err <= SSL_MAX_VFY_ERROR_CODE && |
William Lallemand | 4639689 | 2022-11-10 16:45:24 +0100 | [diff] [blame] | 1493 | cert_ignerr_bitfield_get(bind_conf->crt_ignerr_bitfield, err)) |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1494 | goto err_ignored; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1495 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1496 | /* TODO: for QUIC connection, this error code is lost */ |
| 1497 | if (conn) |
| 1498 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1499 | return 0; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1500 | |
| 1501 | err_ignored: |
| 1502 | ssl_sock_dump_errors(conn, qc); |
| 1503 | ERR_clear_error(); |
| 1504 | return 1; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1505 | } |
| 1506 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1507 | #ifdef TLS1_RT_HEARTBEAT |
| 1508 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 1509 | int content_type, const void *buf, size_t len, |
| 1510 | SSL *ssl) |
| 1511 | { |
| 1512 | /* test heartbeat received (write_p is set to 0 |
| 1513 | for a received record) */ |
| 1514 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 1515 | struct ssl_sock_ctx *ctx = __conn_get_ssl_sock_ctx(conn); |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1516 | const unsigned char *p = buf; |
| 1517 | unsigned int payload; |
| 1518 | |
| 1519 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
| 1520 | |
| 1521 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1522 | if (*p != TLS1_HB_REQUEST) |
| 1523 | return; |
| 1524 | |
| 1525 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
| 1526 | goto kill_it; |
| 1527 | |
| 1528 | payload = (p[1] * 256) + p[2]; |
| 1529 | if (3 + payload + 16 <= len) |
| 1530 | return; /* OK no problem */ |
| 1531 | kill_it: |
| 1532 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1533 | * advertised payload is larger than the advertised packet |
| 1534 | * length, so we have garbage in the buffer between the |
| 1535 | * payload and the end of the buffer (p+len). We can't know |
| 1536 | * if the SSL stack is patched, and we don't know if we can |
| 1537 | * safely wipe out the area between p+3+len and payload. |
| 1538 | * So instead, we prevent the response from being sent by |
| 1539 | * setting the max_send_fragment to 0 and we report an SSL |
| 1540 | * error, which will kill this connection. It will be reported |
| 1541 | * above as SSL_ERROR_SSL while an other handshake failure with |
| 1542 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1543 | */ |
| 1544 | ssl->max_send_fragment = 0; |
| 1545 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1546 | } |
| 1547 | } |
| 1548 | #endif |
| 1549 | |
| 1550 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 1551 | int content_type, const void *buf, size_t len, |
| 1552 | SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1553 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1554 | struct ssl_capture *capture; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1555 | uchar *msg; |
| 1556 | uchar *end; |
| 1557 | uchar *extensions_end; |
| 1558 | uchar *ec_start = NULL; |
| 1559 | uchar *ec_formats_start = NULL; |
| 1560 | uchar *list_end; |
| 1561 | ushort protocol_version; |
| 1562 | ushort extension_id; |
| 1563 | ushort ec_len = 0; |
| 1564 | uchar ec_formats_len = 0; |
| 1565 | int offset = 0; |
| 1566 | int rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1567 | |
| 1568 | /* This function is called for "from client" and "to server" |
| 1569 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1570 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1571 | */ |
| 1572 | |
| 1573 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1574 | * otherwise it is set to 1. |
| 1575 | */ |
| 1576 | if (write_p != 0) |
| 1577 | return; |
| 1578 | |
| 1579 | /* content_type contains the type of message received or sent |
| 1580 | * according with the SSL/TLS protocol spec. This message is |
| 1581 | * encoded with one byte. The value 256 (two bytes) is used |
| 1582 | * for designing the SSL/TLS record layer. According with the |
| 1583 | * rfc6101, the expected message (other than 256) are: |
| 1584 | * - change_cipher_spec(20) |
| 1585 | * - alert(21) |
| 1586 | * - handshake(22) |
| 1587 | * - application_data(23) |
| 1588 | * - (255) |
| 1589 | * We are interessed by the handshake and specially the client |
| 1590 | * hello. |
| 1591 | */ |
| 1592 | if (content_type != 22) |
| 1593 | return; |
| 1594 | |
| 1595 | /* The message length is at least 4 bytes, containing the |
| 1596 | * message type and the message length. |
| 1597 | */ |
| 1598 | if (len < 4) |
| 1599 | return; |
| 1600 | |
| 1601 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1602 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1603 | * - hello_request(0) |
| 1604 | * - client_hello(1) |
| 1605 | * - server_hello(2) |
| 1606 | * - certificate(11) |
| 1607 | * - server_key_exchange (12) |
| 1608 | * - certificate_request(13) |
| 1609 | * - server_hello_done(14) |
| 1610 | * We are interested by the client hello. |
| 1611 | */ |
| 1612 | msg = (unsigned char *)buf; |
| 1613 | if (msg[0] != 1) |
| 1614 | return; |
| 1615 | |
| 1616 | /* Next three bytes are the length of the message. The total length |
| 1617 | * must be this decoded length + 4. If the length given as argument |
| 1618 | * is not the same, we abort the protocol dissector. |
| 1619 | */ |
| 1620 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1621 | if (len < rec_len + 4) |
| 1622 | return; |
| 1623 | msg += 4; |
| 1624 | end = msg + rec_len; |
| 1625 | if (end < msg) |
| 1626 | return; |
| 1627 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1628 | /* Expect 2 bytes for protocol version |
| 1629 | * (1 byte for major and 1 byte for minor) |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1630 | */ |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1631 | if (msg + 2 > end) |
| 1632 | return; |
| 1633 | protocol_version = (msg[0] << 8) + msg[1]; |
| 1634 | msg += 2; |
| 1635 | |
| 1636 | /* Expect the random, composed by 4 bytes for the unix time and |
| 1637 | * 28 bytes for unix payload. So we jump 4 + 28. |
| 1638 | */ |
| 1639 | msg += 4 + 28; |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1640 | if (msg > end) |
| 1641 | return; |
| 1642 | |
| 1643 | /* Next, is session id: |
| 1644 | * if present, we have to jump by length + 1 for the size information |
| 1645 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1646 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1647 | if (msg[0] > 0) |
| 1648 | msg += msg[0]; |
| 1649 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1650 | if (msg > end) |
| 1651 | return; |
| 1652 | |
| 1653 | /* Next two bytes are the ciphersuite length. */ |
| 1654 | if (msg + 2 > end) |
| 1655 | return; |
| 1656 | rec_len = (msg[0] << 8) + msg[1]; |
| 1657 | msg += 2; |
| 1658 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1659 | return; |
| 1660 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1661 | capture = pool_zalloc(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1662 | if (!capture) |
| 1663 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1664 | /* Compute the xxh64 of the ciphersuite. */ |
| 1665 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1666 | |
| 1667 | /* Capture the ciphersuite. */ |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1668 | capture->ciphersuite_len = MIN(global_ssl.capture_buffer_size, rec_len); |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1669 | capture->ciphersuite_offset = 0; |
| 1670 | memcpy(capture->data, msg, capture->ciphersuite_len); |
| 1671 | msg += rec_len; |
| 1672 | offset += capture->ciphersuite_len; |
| 1673 | |
| 1674 | /* Initialize other data */ |
| 1675 | capture->protocol_version = protocol_version; |
| 1676 | |
| 1677 | /* Next, compression methods: |
| 1678 | * if present, we have to jump by length + 1 for the size information |
| 1679 | * if not present, we have to jump by 1 only |
| 1680 | */ |
| 1681 | if (msg[0] > 0) |
| 1682 | msg += msg[0]; |
| 1683 | msg += 1; |
| 1684 | if (msg > end) |
| 1685 | goto store_capture; |
| 1686 | |
| 1687 | /* We reached extensions */ |
| 1688 | if (msg + 2 > end) |
| 1689 | goto store_capture; |
| 1690 | rec_len = (msg[0] << 8) + msg[1]; |
| 1691 | msg += 2; |
| 1692 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1693 | goto store_capture; |
| 1694 | extensions_end = msg + rec_len; |
| 1695 | capture->extensions_offset = offset; |
| 1696 | |
| 1697 | /* Parse each extension */ |
| 1698 | while (msg + 4 < extensions_end) { |
| 1699 | /* Add 2 bytes of extension_id */ |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1700 | if (global_ssl.capture_buffer_size >= offset + 2) { |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1701 | capture->data[offset++] = msg[0]; |
| 1702 | capture->data[offset++] = msg[1]; |
| 1703 | capture->extensions_len += 2; |
| 1704 | } |
| 1705 | else |
| 1706 | break; |
| 1707 | extension_id = (msg[0] << 8) + msg[1]; |
| 1708 | /* Length of the extension */ |
| 1709 | rec_len = (msg[2] << 8) + msg[3]; |
| 1710 | |
| 1711 | /* Expect 2 bytes extension id + 2 bytes extension size */ |
| 1712 | msg += 2 + 2; |
| 1713 | if (msg + rec_len > extensions_end || msg + rec_len < msg) |
| 1714 | goto store_capture; |
| 1715 | /* TLS Extensions |
| 1716 | * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */ |
| 1717 | if (extension_id == 0x000a) { |
| 1718 | /* Elliptic Curves: |
| 1719 | * https://www.rfc-editor.org/rfc/rfc8422.html |
| 1720 | * https://www.rfc-editor.org/rfc/rfc7919.html */ |
| 1721 | list_end = msg + rec_len; |
| 1722 | if (msg + 2 > list_end) |
| 1723 | goto store_capture; |
| 1724 | rec_len = (msg[0] << 8) + msg[1]; |
| 1725 | msg += 2; |
| 1726 | |
| 1727 | if (msg + rec_len > list_end || msg + rec_len < msg) |
| 1728 | goto store_capture; |
| 1729 | /* Store location/size of the list */ |
| 1730 | ec_start = msg; |
| 1731 | ec_len = rec_len; |
| 1732 | } |
| 1733 | else if (extension_id == 0x000b) { |
| 1734 | /* Elliptic Curves Point Formats: |
| 1735 | * https://www.rfc-editor.org/rfc/rfc8422.html */ |
| 1736 | list_end = msg + rec_len; |
| 1737 | if (msg + 1 > list_end) |
| 1738 | goto store_capture; |
| 1739 | rec_len = msg[0]; |
| 1740 | msg += 1; |
| 1741 | |
| 1742 | if (msg + rec_len > list_end || msg + rec_len < msg) |
| 1743 | goto store_capture; |
| 1744 | /* Store location/size of the list */ |
| 1745 | ec_formats_start = msg; |
| 1746 | ec_formats_len = rec_len; |
| 1747 | } |
| 1748 | msg += rec_len; |
| 1749 | } |
| 1750 | |
| 1751 | if (ec_start) { |
| 1752 | rec_len = ec_len; |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1753 | if (offset + rec_len > global_ssl.capture_buffer_size) |
| 1754 | rec_len = global_ssl.capture_buffer_size - offset; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1755 | memcpy(capture->data + offset, ec_start, rec_len); |
| 1756 | capture->ec_offset = offset; |
| 1757 | capture->ec_len = rec_len; |
| 1758 | offset += rec_len; |
| 1759 | } |
| 1760 | if (ec_formats_start) { |
| 1761 | rec_len = ec_formats_len; |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1762 | if (offset + rec_len > global_ssl.capture_buffer_size) |
| 1763 | rec_len = global_ssl.capture_buffer_size - offset; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1764 | memcpy(capture->data + offset, ec_formats_start, rec_len); |
| 1765 | capture->ec_formats_offset = offset; |
| 1766 | capture->ec_formats_len = rec_len; |
| 1767 | offset += rec_len; |
| 1768 | } |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1769 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1770 | store_capture: |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1771 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1772 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1773 | |
| 1774 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 1775 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1776 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 1777 | int content_type, const void *buf, size_t len, |
| 1778 | SSL *ssl) |
| 1779 | { |
| 1780 | struct ssl_keylog *keylog; |
| 1781 | |
| 1782 | if (SSL_get_ex_data(ssl, ssl_keylog_index)) |
| 1783 | return; |
| 1784 | |
Willy Tarreau | f208ac0 | 2021-03-22 21:10:12 +0100 | [diff] [blame] | 1785 | keylog = pool_zalloc(pool_head_ssl_keylog); |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1786 | if (!keylog) |
| 1787 | return; |
| 1788 | |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1789 | if (!SSL_set_ex_data(ssl, ssl_keylog_index, keylog)) { |
| 1790 | pool_free(pool_head_ssl_keylog, keylog); |
| 1791 | return; |
| 1792 | } |
| 1793 | } |
| 1794 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1795 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1796 | /* Callback is called for ssl protocol analyse */ |
| 1797 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1798 | { |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1799 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1800 | struct ssl_sock_msg_callback *cbk; |
| 1801 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1802 | /* Try to call all callback functions that were registered by using |
| 1803 | * ssl_sock_register_msg_callback(). |
| 1804 | */ |
| 1805 | list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) { |
| 1806 | cbk->func(conn, write_p, version, content_type, buf, len, ssl); |
| 1807 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1808 | } |
| 1809 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1810 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1811 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1812 | const unsigned char *in, unsigned int inlen, |
| 1813 | void *arg) |
| 1814 | { |
| 1815 | struct server *srv = arg; |
| 1816 | |
| 1817 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1818 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1819 | return SSL_TLSEXT_ERR_OK; |
| 1820 | return SSL_TLSEXT_ERR_NOACK; |
| 1821 | } |
| 1822 | #endif |
| 1823 | |
| 1824 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1825 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1826 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1827 | */ |
| 1828 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1829 | unsigned int *len, void *arg) |
| 1830 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1831 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1832 | |
| 1833 | *data = (const unsigned char *)conf->npn_str; |
| 1834 | *len = conf->npn_len; |
| 1835 | return SSL_TLSEXT_ERR_OK; |
| 1836 | } |
| 1837 | #endif |
| 1838 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1839 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1840 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1841 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1842 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1843 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1844 | unsigned char *outlen, |
| 1845 | const unsigned char *server, |
| 1846 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1847 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1848 | struct ssl_bind_conf *conf = arg; |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 1849 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1850 | struct quic_conn *qc = SSL_get_ex_data(s, ssl_qc_app_data_index); |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 1851 | #endif |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1852 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1853 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1854 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1855 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1856 | if (qc) |
| 1857 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1858 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1859 | return SSL_TLSEXT_ERR_NOACK; |
| 1860 | } |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 1861 | |
| 1862 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1863 | if (qc && !quic_set_app_ops(qc, *out, *outlen)) { |
| 1864 | quic_set_tls_alert(qc, SSL_AD_NO_APPLICATION_PROTOCOL); |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 1865 | return SSL_TLSEXT_ERR_NOACK; |
| 1866 | } |
| 1867 | #endif |
| 1868 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1869 | return SSL_TLSEXT_ERR_OK; |
| 1870 | } |
| 1871 | #endif |
| 1872 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1873 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1874 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1875 | |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 1876 | /* Configure a DNS SAN extension on a certificate. */ |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1877 | int ssl_sock_add_san_ext(X509V3_CTX* ctx, X509* cert, const char *servername) { |
| 1878 | int failure = 0; |
| 1879 | X509_EXTENSION *san_ext = NULL; |
| 1880 | CONF *conf = NULL; |
| 1881 | struct buffer *san_name = get_trash_chunk(); |
| 1882 | |
| 1883 | conf = NCONF_new(NULL); |
| 1884 | if (!conf) { |
| 1885 | failure = 1; |
| 1886 | goto cleanup; |
| 1887 | } |
| 1888 | |
| 1889 | /* Build an extension based on the DNS entry above */ |
| 1890 | chunk_appendf(san_name, "DNS:%s", servername); |
| 1891 | san_ext = X509V3_EXT_nconf_nid(conf, ctx, NID_subject_alt_name, san_name->area); |
| 1892 | if (!san_ext) { |
| 1893 | failure = 1; |
| 1894 | goto cleanup; |
| 1895 | } |
| 1896 | |
| 1897 | /* Add the extension */ |
| 1898 | if (!X509_add_ext(cert, san_ext, -1 /* Add to end */)) { |
| 1899 | failure = 1; |
| 1900 | goto cleanup; |
| 1901 | } |
| 1902 | |
| 1903 | /* Success */ |
| 1904 | failure = 0; |
| 1905 | |
| 1906 | cleanup: |
| 1907 | if (NULL != san_ext) X509_EXTENSION_free(san_ext); |
| 1908 | if (NULL != conf) NCONF_free(conf); |
| 1909 | |
| 1910 | return failure; |
| 1911 | } |
| 1912 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1913 | /* Create a X509 certificate with the specified servername and serial. This |
| 1914 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1915 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1916 | 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] | 1917 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 1918 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
| 1919 | EVP_PKEY *capkey = bind_conf->ca_sign_ckch->key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1920 | SSL_CTX *ssl_ctx = NULL; |
| 1921 | X509 *newcrt = NULL; |
| 1922 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1923 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1924 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1925 | X509_NAME *name; |
| 1926 | const EVP_MD *digest; |
| 1927 | X509V3_CTX ctx; |
| 1928 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1929 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1930 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1931 | /* Get the private key of the default certificate and use it */ |
Ilya Shipitsin | af20488 | 2020-12-19 03:12:12 +0500 | [diff] [blame] | 1932 | #ifdef HAVE_SSL_CTX_get0_privatekey |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1933 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1934 | #else |
| 1935 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1936 | if (tmp_ssl) |
| 1937 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1938 | #endif |
| 1939 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1940 | goto mkcert_error; |
| 1941 | |
| 1942 | /* Create the certificate */ |
| 1943 | if (!(newcrt = X509_new())) |
| 1944 | goto mkcert_error; |
| 1945 | |
| 1946 | /* Set version number for the certificate (X509v3) and the serial |
| 1947 | * number */ |
| 1948 | if (X509_set_version(newcrt, 2L) != 1) |
| 1949 | goto mkcert_error; |
Willy Tarreau | 1db4273 | 2021-04-06 11:44:07 +0200 | [diff] [blame] | 1950 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD_FETCH(&ssl_ctx_serial, 1)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1951 | |
| 1952 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1953 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1954 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1955 | goto mkcert_error; |
| 1956 | |
| 1957 | /* set public key in the certificate */ |
| 1958 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1959 | goto mkcert_error; |
| 1960 | |
| 1961 | /* Set issuer name from the CA */ |
| 1962 | if (!(name = X509_get_subject_name(cacert))) |
| 1963 | goto mkcert_error; |
| 1964 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1965 | goto mkcert_error; |
| 1966 | |
| 1967 | /* Set the subject name using the same, but the CN */ |
| 1968 | name = X509_NAME_dup(name); |
| 1969 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1970 | (const unsigned char *)servername, |
| 1971 | -1, -1, 0) != 1) { |
| 1972 | X509_NAME_free(name); |
| 1973 | goto mkcert_error; |
| 1974 | } |
| 1975 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1976 | X509_NAME_free(name); |
| 1977 | goto mkcert_error; |
| 1978 | } |
| 1979 | X509_NAME_free(name); |
| 1980 | |
| 1981 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1982 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1983 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1984 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1985 | X509_EXTENSION *ext; |
| 1986 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1987 | 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] | 1988 | goto mkcert_error; |
| 1989 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1990 | X509_EXTENSION_free(ext); |
| 1991 | goto mkcert_error; |
| 1992 | } |
| 1993 | X509_EXTENSION_free(ext); |
| 1994 | } |
| 1995 | |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1996 | /* Add SAN extension */ |
| 1997 | if (ssl_sock_add_san_ext(&ctx, newcrt, servername)) { |
| 1998 | goto mkcert_error; |
| 1999 | } |
| 2000 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2001 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2002 | |
| 2003 | key_type = EVP_PKEY_base_id(capkey); |
| 2004 | |
| 2005 | if (key_type == EVP_PKEY_DSA) |
| 2006 | digest = EVP_sha1(); |
| 2007 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2008 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2009 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2010 | digest = EVP_sha256(); |
| 2011 | else { |
Ilya Shipitsin | ec36c91 | 2021-01-07 11:57:42 +0500 | [diff] [blame] | 2012 | #ifdef ASN1_PKEY_CTRL_DEFAULT_MD_NID |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2013 | int nid; |
| 2014 | |
| 2015 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 2016 | goto mkcert_error; |
| 2017 | if (!(digest = EVP_get_digestbynid(nid))) |
| 2018 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 2019 | #else |
| 2020 | goto mkcert_error; |
| 2021 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2022 | } |
| 2023 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2024 | if (!(X509_sign(newcrt, capkey, digest))) |
| 2025 | goto mkcert_error; |
| 2026 | |
| 2027 | /* Create and set the new SSL_CTX */ |
| 2028 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 2029 | goto mkcert_error; |
| 2030 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 2031 | goto mkcert_error; |
| 2032 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 2033 | goto mkcert_error; |
| 2034 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 2035 | goto mkcert_error; |
| 2036 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2037 | /* Build chaining the CA cert and the rest of the chain, keep these order */ |
| 2038 | #if defined(SSL_CTX_add1_chain_cert) |
| 2039 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, bind_conf->ca_sign_ckch->cert)) { |
| 2040 | goto mkcert_error; |
| 2041 | } |
| 2042 | |
| 2043 | if (bind_conf->ca_sign_ckch->chain) { |
| 2044 | for (i = 0; i < sk_X509_num(bind_conf->ca_sign_ckch->chain); i++) { |
| 2045 | X509 *chain_cert = sk_X509_value(bind_conf->ca_sign_ckch->chain, i); |
| 2046 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, chain_cert)) { |
| 2047 | goto mkcert_error; |
| 2048 | } |
| 2049 | } |
| 2050 | } |
| 2051 | #endif |
| 2052 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2053 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2054 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2055 | #ifndef OPENSSL_NO_DH |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 2056 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 2057 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh_cbk); |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 2058 | #else |
| 2059 | ssl_sock_set_tmp_dh_from_pkey(ssl_ctx, pkey); |
| 2060 | #endif |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2061 | #endif |
Remi Tricot-Le Breton | c11e7e1 | 2022-02-08 17:45:56 +0100 | [diff] [blame] | 2062 | |
| 2063 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 2064 | #if defined(SSL_CTX_set1_curves_list) |
| 2065 | { |
| 2066 | const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
| 2067 | if (!SSL_CTX_set1_curves_list(ssl_ctx, ecdhe)) |
| 2068 | goto end; |
| 2069 | } |
| 2070 | #endif |
| 2071 | #else |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2072 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2073 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2074 | 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] | 2075 | EC_KEY *ecc; |
| 2076 | int nid; |
| 2077 | |
| 2078 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2079 | goto end; |
| 2080 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2081 | goto end; |
| 2082 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2083 | EC_KEY_free(ecc); |
| 2084 | } |
Remi Tricot-Le Breton | c11e7e1 | 2022-02-08 17:45:56 +0100 | [diff] [blame] | 2085 | #endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */ |
| 2086 | #endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */ |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2087 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2088 | return ssl_ctx; |
| 2089 | |
| 2090 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2091 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2092 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2093 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2094 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2095 | return NULL; |
| 2096 | } |
| 2097 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2098 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2099 | /* 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] | 2100 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2101 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2102 | 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] | 2103 | { |
| 2104 | struct lru64 *lru = NULL; |
| 2105 | |
| 2106 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2107 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2108 | 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] | 2109 | if (lru && lru->domain) { |
| 2110 | if (ssl) |
| 2111 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2112 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2113 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2114 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2115 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2116 | } |
| 2117 | return NULL; |
| 2118 | } |
| 2119 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2120 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2121 | * function is not thread-safe, it should only be used to check if a certificate |
| 2122 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2123 | * thread). It is kept for backward compatibility. */ |
| 2124 | SSL_CTX * |
| 2125 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2126 | { |
| 2127 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2128 | } |
| 2129 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2130 | /* Set a certificate int the LRU cache used to store generated |
| 2131 | * certificate. Return 0 on success, otherwise -1 */ |
| 2132 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2133 | 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] | 2134 | { |
| 2135 | struct lru64 *lru = NULL; |
| 2136 | |
| 2137 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2138 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2139 | 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] | 2140 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2141 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2142 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2143 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2144 | if (lru->domain && lru->data) |
| 2145 | lru->free((SSL_CTX *)lru->data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2146 | 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] | 2147 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2148 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2149 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2150 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2151 | } |
| 2152 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2153 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2154 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2155 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2156 | { |
| 2157 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2158 | } |
| 2159 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2160 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2161 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2162 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2163 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2164 | 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] | 2165 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2166 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2167 | SSL_CTX *ssl_ctx = NULL; |
| 2168 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2169 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2170 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2171 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2172 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2173 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2174 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2175 | if (lru && lru->domain) |
| 2176 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2177 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2178 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2179 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2180 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2181 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2182 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2183 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2184 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2185 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2186 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2187 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2188 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2189 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2190 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2191 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2192 | return 0; |
| 2193 | } |
| 2194 | static int |
| 2195 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2196 | { |
| 2197 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2198 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2199 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2200 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2201 | 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] | 2202 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2203 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2204 | } |
| 2205 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2206 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2207 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2208 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2209 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2210 | |
| 2211 | 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] | 2212 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2213 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2214 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2215 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2216 | #endif |
| 2217 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2218 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2219 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2220 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2221 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2222 | 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] | 2223 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2224 | 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] | 2225 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2226 | #endif |
| 2227 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2228 | 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] | 2229 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2230 | 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] | 2231 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2232 | #endif |
| 2233 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2234 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2235 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2236 | /* Unusable in this context. */ |
| 2237 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2238 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2239 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2240 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2241 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2242 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2243 | |
| 2244 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2245 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2246 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2247 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2248 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2249 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2250 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2251 | } |
| 2252 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2253 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2254 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2255 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2256 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2257 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2258 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2259 | } |
| 2260 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2261 | 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] | 2262 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2263 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2264 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2265 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2266 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2267 | } |
| 2268 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2269 | 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] | 2270 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2271 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2272 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2273 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2274 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2275 | } |
| 2276 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) { |
William Lallemand | f22b032 | 2021-06-02 16:09:11 +0200 | [diff] [blame] | 2277 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2278 | 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] | 2279 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2280 | #endif |
| 2281 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2282 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
William Lallemand | f22b032 | 2021-06-02 16:09:11 +0200 | [diff] [blame] | 2283 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2284 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2285 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2286 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2287 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2288 | #endif |
| 2289 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2290 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2291 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 2292 | struct methodVersions methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2293 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2294 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2295 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2296 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2297 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2298 | {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] | 2299 | }; |
| 2300 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2301 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2302 | { |
| 2303 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2304 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2305 | SSL_set_SSL_CTX(ssl, ctx); |
| 2306 | } |
| 2307 | |
Ilya Shipitsin | 1fc44d4 | 2021-01-23 00:09:14 +0500 | [diff] [blame] | 2308 | #ifdef HAVE_SSL_CLIENT_HELLO_CB |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2309 | |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2310 | int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2311 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2312 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2313 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2314 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2315 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || (s->options & BC_O_GENERATE_CERTS)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2316 | return SSL_TLSEXT_ERR_OK; |
| 2317 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2318 | } |
| 2319 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2320 | #ifdef OPENSSL_IS_BORINGSSL |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2321 | int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2322 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2323 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2324 | #else |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2325 | int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2326 | { |
| 2327 | #endif |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2328 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 2329 | #ifdef USE_QUIC |
| 2330 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 2331 | #endif /* USE_QUIC */ |
| 2332 | struct bind_conf *s = NULL; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2333 | const uint8_t *extension_data; |
| 2334 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2335 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2336 | |
| 2337 | char *wildp = NULL; |
| 2338 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2339 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2340 | 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] | 2341 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2342 | int i; |
| 2343 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2344 | if (conn) |
| 2345 | s = __objt_listener(conn->target)->bind_conf; |
| 2346 | #ifdef USE_QUIC |
| 2347 | else if (qc) |
| 2348 | s = qc->li->bind_conf; |
| 2349 | #endif /* USE_QUIC */ |
Amaury Denoyelle | 7c564bf | 2022-01-24 11:04:05 +0100 | [diff] [blame] | 2350 | |
| 2351 | if (!s) { |
| 2352 | /* must never happen */ |
| 2353 | ABORT_NOW(); |
| 2354 | return 0; |
| 2355 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2356 | |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2357 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2358 | if (qc) { |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2359 | /* Look for the QUIC transport parameters. */ |
| 2360 | #ifdef OPENSSL_IS_BORINGSSL |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2361 | if (!SSL_early_callback_ctx_extension_get(ctx, qc->tps_tls_ext, |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2362 | &extension_data, &extension_len)) |
| 2363 | #else |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2364 | if (!SSL_client_hello_get0_ext(ssl, qc->tps_tls_ext, |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2365 | &extension_data, &extension_len)) |
| 2366 | #endif |
Frédéric Lécaille | b5b5247 | 2021-11-22 15:55:16 +0100 | [diff] [blame] | 2367 | { |
| 2368 | /* This is not redundant. It we only return 0 without setting |
| 2369 | * <*al>, this has as side effect to generate another TLS alert |
| 2370 | * which would be set after calling quic_set_tls_alert(). |
| 2371 | */ |
| 2372 | *al = SSL_AD_MISSING_EXTENSION; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2373 | quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION); |
Frédéric Lécaille | b5b5247 | 2021-11-22 15:55:16 +0100 | [diff] [blame] | 2374 | return 0; |
| 2375 | } |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2376 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2377 | if (!quic_transport_params_store(qc, 0, extension_data, |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2378 | extension_data + extension_len)) |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2379 | goto abort; |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2380 | |
| 2381 | qc->flags |= QUIC_FL_CONN_TX_TP_RECEIVED; |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2382 | } |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2383 | #endif /* USE_QUIC */ |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2384 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2385 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2386 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2387 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2388 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2389 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2390 | #else |
| 2391 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2392 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2393 | /* |
| 2394 | * The server_name extension was given too much extensibility when it |
| 2395 | * was written, so parsing the normal case is a bit complex. |
| 2396 | */ |
| 2397 | size_t len; |
| 2398 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2399 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2400 | /* Extract the length of the supplied list of names. */ |
| 2401 | len = (*extension_data++) << 8; |
| 2402 | len |= *extension_data++; |
| 2403 | if (len + 2 != extension_len) |
| 2404 | goto abort; |
| 2405 | /* |
| 2406 | * The list in practice only has a single element, so we only consider |
| 2407 | * the first one. |
| 2408 | */ |
| 2409 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2410 | goto abort; |
| 2411 | extension_len = len - 1; |
| 2412 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2413 | if (extension_len <= 2) |
| 2414 | goto abort; |
| 2415 | len = (*extension_data++) << 8; |
| 2416 | len |= *extension_data++; |
| 2417 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2418 | || memchr(extension_data, 0, len) != NULL) |
| 2419 | goto abort; |
| 2420 | servername = extension_data; |
| 2421 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2422 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2423 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2424 | if (s->options & BC_O_GENERATE_CERTS && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2425 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2426 | } |
| 2427 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2428 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2429 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2430 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2431 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2432 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2433 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2434 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2435 | goto abort; |
| 2436 | } |
| 2437 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2438 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2439 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2440 | 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] | 2441 | #else |
| 2442 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2443 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2444 | uint8_t sign; |
| 2445 | size_t len; |
| 2446 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2447 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2448 | len = (*extension_data++) << 8; |
| 2449 | len |= *extension_data++; |
| 2450 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2451 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2452 | if (len % 2 != 0) |
| 2453 | goto abort; |
| 2454 | for (; len > 0; len -= 2) { |
| 2455 | extension_data++; /* hash */ |
| 2456 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2457 | switch (sign) { |
| 2458 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2459 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2460 | break; |
| 2461 | case TLSEXT_signature_ecdsa: |
| 2462 | has_ecdsa_sig = 1; |
| 2463 | break; |
| 2464 | default: |
| 2465 | continue; |
| 2466 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2467 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2468 | break; |
| 2469 | } |
| 2470 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2471 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2472 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2473 | } |
| 2474 | 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] | 2475 | const SSL_CIPHER *cipher; |
| 2476 | size_t len; |
| 2477 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2478 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2479 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2480 | len = ctx->cipher_suites_len; |
| 2481 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2482 | #else |
| 2483 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2484 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2485 | if (len % 2 != 0) |
| 2486 | goto abort; |
| 2487 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2488 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2489 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2490 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2491 | #else |
| 2492 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2493 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2494 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2495 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2496 | break; |
| 2497 | } |
| 2498 | } |
| 2499 | } |
| 2500 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2501 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2502 | trash.area[i] = tolower(servername[i]); |
| 2503 | if (!wildp && (trash.area[i] == '.')) |
| 2504 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2505 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2506 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2507 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2508 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2509 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2510 | /* Look for an ECDSA, RSA and DSA certificate, first in the single |
| 2511 | * name and if not found in the wildcard */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2512 | for (i = 0; i < 2; i++) { |
| 2513 | if (i == 0) /* lookup in full qualified names */ |
| 2514 | node = ebst_lookup(&s->sni_ctx, trash.area); |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2515 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2516 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2517 | else |
| 2518 | break; |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2519 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2520 | for (n = node; n; n = ebmb_next_dup(n)) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2521 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2522 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2523 | if (!container_of(n, struct sni_ctx, name)->neg) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2524 | struct sni_ctx *sni, *sni_tmp; |
| 2525 | int skip = 0; |
| 2526 | |
| 2527 | if (i == 1 && wildp) { /* wildcard */ |
| 2528 | /* If this is a wildcard, look for an exclusion on the same crt-list line */ |
| 2529 | sni = container_of(n, struct sni_ctx, name); |
| 2530 | list_for_each_entry(sni_tmp, &sni->ckch_inst->sni_ctx, by_ckch_inst) { |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 2531 | if (sni_tmp->neg && (strcmp((const char *)sni_tmp->name.key, trash.area) == 0)) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2532 | skip = 1; |
| 2533 | break; |
| 2534 | } |
| 2535 | } |
| 2536 | if (skip) |
| 2537 | continue; |
| 2538 | } |
| 2539 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2540 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2541 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2542 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2543 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2544 | break; |
| 2545 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2546 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2547 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2548 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2549 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2550 | if (!node_anonymous) |
| 2551 | node_anonymous = n; |
| 2552 | break; |
| 2553 | } |
| 2554 | } |
| 2555 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2556 | } |
| 2557 | /* Once the certificates are found, select them depending on what is |
| 2558 | * supported in the client and by key_signature priority order: EDSA > |
| 2559 | * RSA > DSA */ |
William Lallemand | 5b1d1f6 | 2020-08-14 15:30:13 +0200 | [diff] [blame] | 2560 | if (has_ecdsa_sig && node_ecdsa) |
| 2561 | node = node_ecdsa; |
| 2562 | else if (has_rsa_sig && node_rsa) |
| 2563 | node = node_rsa; |
| 2564 | else if (node_anonymous) |
| 2565 | node = node_anonymous; |
| 2566 | else if (node_ecdsa) |
| 2567 | node = node_ecdsa; /* no ecdsa signature case (< TLSv1.2) */ |
| 2568 | else |
| 2569 | node = node_rsa; /* no rsa signature case (far far away) */ |
| 2570 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2571 | if (node) { |
| 2572 | /* switch ctx */ |
| 2573 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2574 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 2575 | if (conf) { |
| 2576 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2577 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2578 | if (conf->early_data) |
| 2579 | allow_early = 1; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2580 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2581 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
| 2582 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2583 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2584 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2585 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2586 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2587 | if (s->options & BC_O_GENERATE_CERTS && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2588 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2589 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2590 | } |
| 2591 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2592 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2593 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2594 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2595 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2596 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2597 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2598 | } |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2599 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 2600 | /* We are about to raise an handshake error so the servername extension |
| 2601 | * callback will never be called and the SNI will never be stored in the |
| 2602 | * SSL context. In order for the ssl_fc_sni sample fetch to still work |
| 2603 | * in such a case, we store the SNI ourselves as an ex_data information |
| 2604 | * in the SSL context. |
| 2605 | */ |
| 2606 | { |
| 2607 | char *client_sni = pool_alloc(ssl_sock_client_sni_pool); |
| 2608 | if (client_sni) { |
| 2609 | strncpy(client_sni, trash.area, TLSEXT_MAXLEN_host_name); |
| 2610 | client_sni[TLSEXT_MAXLEN_host_name] = '\0'; |
| 2611 | SSL_set_ex_data(ssl, ssl_client_sni_index, client_sni); |
| 2612 | } |
| 2613 | } |
| 2614 | |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2615 | /* other cases fallback on abort, if strict-sni is set but no node was found */ |
| 2616 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2617 | abort: |
| 2618 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2619 | if (conn) |
| 2620 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2621 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2622 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2623 | #else |
| 2624 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2625 | return 0; |
| 2626 | #endif |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2627 | |
| 2628 | allow_early: |
| 2629 | #ifdef OPENSSL_IS_BORINGSSL |
| 2630 | if (allow_early) |
| 2631 | SSL_set_early_data_enabled(ssl, 1); |
| 2632 | #else |
| 2633 | if (!allow_early) |
| 2634 | SSL_set_max_early_data(ssl, 0); |
| 2635 | #endif |
| 2636 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2637 | } |
| 2638 | |
William Lallemand | 002e206 | 2021-11-18 15:25:16 +0100 | [diff] [blame] | 2639 | #else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2640 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2641 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2642 | * warning when no match is found, which implies the default (first) cert |
| 2643 | * will keep being used. |
| 2644 | */ |
William Lallemand | 844009d | 2022-09-02 15:27:32 +0200 | [diff] [blame] | 2645 | int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2646 | { |
| 2647 | const char *servername; |
| 2648 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2649 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2650 | struct bind_conf *s = priv; |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2651 | #ifdef USE_QUIC |
| 2652 | const uint8_t *extension_data; |
| 2653 | size_t extension_len; |
| 2654 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 2655 | #endif /* USE_QUIC */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2656 | int i; |
| 2657 | (void)al; /* shut gcc stupid warning */ |
| 2658 | |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2659 | #ifdef USE_QUIC |
| 2660 | if (qc) { |
| 2661 | |
| 2662 | /* Look for the QUIC transport parameters. */ |
| 2663 | SSL_get_peer_quic_transport_params(ssl, &extension_data, &extension_len); |
| 2664 | if (extension_len == 0) { |
| 2665 | /* This is not redundant. It we only return 0 without setting |
| 2666 | * <*al>, this has as side effect to generate another TLS alert |
| 2667 | * which would be set after calling quic_set_tls_alert(). |
| 2668 | */ |
| 2669 | *al = SSL_AD_MISSING_EXTENSION; |
| 2670 | quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION); |
| 2671 | return SSL_TLSEXT_ERR_NOACK; |
| 2672 | } |
| 2673 | |
| 2674 | if (!quic_transport_params_store(qc, 0, extension_data, |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2675 | extension_data + extension_len)) |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2676 | return SSL_TLSEXT_ERR_NOACK; |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2677 | |
| 2678 | qc->flags |= QUIC_FL_CONN_TX_TP_RECEIVED; |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2679 | } |
| 2680 | #endif /* USE_QUIC */ |
| 2681 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2682 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2683 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2684 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2685 | if (s->options & BC_O_GENERATE_CERTS && ssl_sock_generate_certificate_from_conn(s, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2686 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2687 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2688 | if (s->strict_sni) |
| 2689 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2690 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2691 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2692 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2693 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2694 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2695 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2696 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2697 | if (!servername[i]) |
| 2698 | break; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 2699 | trash.area[i] = tolower((unsigned char)servername[i]); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2700 | if (!wildp && (trash.area[i] == '.')) |
| 2701 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2702 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2703 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2704 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2705 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2706 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2707 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2708 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2709 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2710 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2711 | node = n; |
| 2712 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2713 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2714 | } |
| 2715 | if (!node && wildp) { |
| 2716 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2717 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2718 | /* lookup a not neg filter */ |
| 2719 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2720 | node = n; |
| 2721 | break; |
| 2722 | } |
| 2723 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2724 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2725 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2726 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2727 | if (s->options & BC_O_GENERATE_CERTS && ssl_sock_generate_certificate(servername, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2728 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2729 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2730 | return SSL_TLSEXT_ERR_OK; |
| 2731 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2732 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2733 | if (s->strict_sni) { |
| 2734 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2735 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2736 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2737 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2738 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2739 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2740 | } |
| 2741 | |
| 2742 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2743 | 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] | 2744 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2745 | return SSL_TLSEXT_ERR_OK; |
| 2746 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2747 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2748 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2749 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2750 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2751 | |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2752 | static inline HASSL_DH *ssl_new_dh_fromdata(BIGNUM *p, BIGNUM *g) |
| 2753 | { |
| 2754 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 2755 | OSSL_PARAM_BLD *tmpl = NULL; |
| 2756 | OSSL_PARAM *params = NULL; |
| 2757 | EVP_PKEY_CTX *ctx = NULL; |
| 2758 | EVP_PKEY *pkey = NULL; |
| 2759 | |
| 2760 | if ((tmpl = OSSL_PARAM_BLD_new()) == NULL |
| 2761 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
| 2762 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g) |
| 2763 | || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
| 2764 | goto end; |
| 2765 | } |
| 2766 | ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 2767 | if (ctx == NULL |
| 2768 | || !EVP_PKEY_fromdata_init(ctx) |
| 2769 | || !EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params)) { |
| 2770 | goto end; |
| 2771 | } |
| 2772 | |
| 2773 | end: |
| 2774 | EVP_PKEY_CTX_free(ctx); |
| 2775 | OSSL_PARAM_free(params); |
| 2776 | OSSL_PARAM_BLD_free(tmpl); |
Remi Tricot-Le Breton | a2c21db | 2022-11-03 15:16:47 +0100 | [diff] [blame] | 2777 | BN_free(p); |
| 2778 | BN_free(g); |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2779 | return pkey; |
| 2780 | #else |
| 2781 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2782 | HASSL_DH *dh = DH_new(); |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2783 | |
| 2784 | if (!dh) |
| 2785 | return NULL; |
| 2786 | |
| 2787 | DH_set0_pqg(dh, p, NULL, g); |
| 2788 | |
| 2789 | return dh; |
| 2790 | #endif |
| 2791 | } |
| 2792 | |
Remi Tricot-Le Breton | c69be7c | 2022-04-20 18:30:17 +0200 | [diff] [blame] | 2793 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2794 | static inline HASSL_DH *ssl_get_dh_by_nid(int nid) |
| 2795 | { |
| 2796 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 2797 | OSSL_PARAM params[2]; |
| 2798 | EVP_PKEY *pkey = NULL; |
| 2799 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 2800 | const char *named_group = NULL; |
| 2801 | |
| 2802 | if (!pctx) |
| 2803 | goto end; |
| 2804 | |
| 2805 | named_group = OBJ_nid2ln(nid); |
| 2806 | |
| 2807 | if (!named_group) |
| 2808 | goto end; |
| 2809 | |
| 2810 | params[0] = OSSL_PARAM_construct_utf8_string("group", (char*)named_group, 0); |
| 2811 | params[1] = OSSL_PARAM_construct_end(); |
| 2812 | |
| 2813 | if (EVP_PKEY_keygen_init(pctx) && EVP_PKEY_CTX_set_params(pctx, params)) |
| 2814 | EVP_PKEY_generate(pctx, &pkey); |
| 2815 | |
| 2816 | end: |
| 2817 | EVP_PKEY_CTX_free(pctx); |
| 2818 | return pkey; |
| 2819 | #else |
| 2820 | |
| 2821 | HASSL_DH *dh = NULL; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2822 | dh = DH_new_by_nid(nid); |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2823 | return dh; |
| 2824 | #endif |
| 2825 | } |
Remi Tricot-Le Breton | c69be7c | 2022-04-20 18:30:17 +0200 | [diff] [blame] | 2826 | #endif |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2827 | |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2828 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2829 | static HASSL_DH * ssl_get_dh_1024(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2830 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2831 | static unsigned char dh1024_p[]={ |
| 2832 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2833 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2834 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2835 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2836 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2837 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2838 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2839 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2840 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2841 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2842 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2843 | }; |
| 2844 | static unsigned char dh1024_g[]={ |
| 2845 | 0x02, |
| 2846 | }; |
| 2847 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2848 | BIGNUM *p; |
| 2849 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2850 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2851 | HASSL_DH *dh = NULL; |
| 2852 | |
| 2853 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2854 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
| 2855 | |
| 2856 | if (p && g) |
| 2857 | dh = ssl_new_dh_fromdata(p, g); |
| 2858 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2859 | return dh; |
| 2860 | } |
| 2861 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2862 | static HASSL_DH *ssl_get_dh_2048(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2863 | { |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2864 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2865 | static unsigned char dh2048_p[]={ |
| 2866 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2867 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2868 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2869 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2870 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2871 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2872 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2873 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2874 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2875 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2876 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2877 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2878 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2879 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2880 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2881 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2882 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2883 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2884 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2885 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2886 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2887 | 0xB7,0x1F,0x77,0xF3, |
| 2888 | }; |
| 2889 | static unsigned char dh2048_g[]={ |
| 2890 | 0x02, |
| 2891 | }; |
| 2892 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2893 | BIGNUM *p; |
| 2894 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2895 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2896 | HASSL_DH *dh = NULL; |
| 2897 | |
| 2898 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2899 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
| 2900 | |
| 2901 | if (p && g) |
| 2902 | dh = ssl_new_dh_fromdata(p, g); |
| 2903 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2904 | return dh; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2905 | #else |
| 2906 | return ssl_get_dh_by_nid(NID_ffdhe2048); |
| 2907 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2908 | } |
| 2909 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2910 | static HASSL_DH *ssl_get_dh_4096(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2911 | { |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2912 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2913 | static unsigned char dh4096_p[]={ |
| 2914 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2915 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2916 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2917 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2918 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2919 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2920 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2921 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2922 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2923 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2924 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2925 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2926 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2927 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2928 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2929 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2930 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2931 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2932 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2933 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2934 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2935 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2936 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2937 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2938 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2939 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2940 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2941 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2942 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2943 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2944 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2945 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2946 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2947 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2948 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2949 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2950 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2951 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2952 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2953 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2954 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2955 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2956 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2957 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2958 | static unsigned char dh4096_g[]={ |
| 2959 | 0x02, |
| 2960 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2961 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2962 | BIGNUM *p; |
| 2963 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2964 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2965 | HASSL_DH *dh = NULL; |
| 2966 | |
| 2967 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2968 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
| 2969 | |
| 2970 | if (p && g) |
| 2971 | dh = ssl_new_dh_fromdata(p, g); |
| 2972 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2973 | return dh; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2974 | #else |
| 2975 | return ssl_get_dh_by_nid(NID_ffdhe4096); |
| 2976 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2977 | } |
| 2978 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2979 | static HASSL_DH *ssl_get_tmp_dh(EVP_PKEY *pkey) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2980 | { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2981 | HASSL_DH *dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2982 | int type; |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 2983 | int keylen = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2984 | |
| 2985 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2986 | |
Ilya Shipitsin | 52f2ff5 | 2022-07-23 23:55:19 +0500 | [diff] [blame] | 2987 | if (type == EVP_PKEY_EC) { |
| 2988 | keylen = global_ssl.default_dh_param; |
| 2989 | } |
| 2990 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2991 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2992 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2993 | */ |
| 2994 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2995 | keylen = EVP_PKEY_bits(pkey); |
| 2996 | } |
| 2997 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2998 | if (keylen > global_ssl.default_dh_param) { |
| 2999 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3000 | } |
| 3001 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3002 | if (keylen >= 4096) { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3003 | if (!local_dh_4096) |
| 3004 | local_dh_4096 = ssl_get_dh_4096(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3005 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3006 | } |
| 3007 | else if (keylen >= 2048) { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3008 | if (!local_dh_2048) |
| 3009 | local_dh_2048 = ssl_get_dh_2048(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3010 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3011 | } |
| 3012 | else { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3013 | if (!local_dh_1024) |
| 3014 | local_dh_1024 = ssl_get_dh_1024(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3015 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3016 | } |
| 3017 | |
| 3018 | return dh; |
| 3019 | } |
| 3020 | |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3021 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3022 | /* Returns Diffie-Hellman parameters matching the private key length |
| 3023 | but not exceeding global_ssl.default_dh_param */ |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3024 | static HASSL_DH *ssl_get_tmp_dh_cbk(SSL *ssl, int export, int keylen) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3025 | { |
| 3026 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
| 3027 | |
| 3028 | return ssl_get_tmp_dh(pkey); |
| 3029 | } |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3030 | #endif |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3031 | |
Remi Tricot-Le Breton | 846eda9 | 2022-02-11 12:04:50 +0100 | [diff] [blame] | 3032 | static int ssl_sock_set_tmp_dh(SSL_CTX *ctx, HASSL_DH *dh) |
| 3033 | { |
| 3034 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
| 3035 | return SSL_CTX_set_tmp_dh(ctx, dh); |
| 3036 | #else |
| 3037 | int retval = 0; |
| 3038 | HASSL_DH_up_ref(dh); |
| 3039 | |
| 3040 | retval = SSL_CTX_set0_tmp_dh_pkey(ctx, dh); |
| 3041 | |
| 3042 | if (!retval) |
| 3043 | HASSL_DH_free(dh); |
| 3044 | |
| 3045 | return retval; |
| 3046 | #endif |
| 3047 | } |
| 3048 | |
Remi Tricot-Le Breton | 5f17930 | 2022-02-11 12:04:51 +0100 | [diff] [blame] | 3049 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3050 | static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey) |
| 3051 | { |
| 3052 | HASSL_DH *dh = NULL; |
| 3053 | if (pkey && (dh = ssl_get_tmp_dh(pkey))) { |
| 3054 | HASSL_DH_up_ref(dh); |
| 3055 | if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dh)) |
| 3056 | HASSL_DH_free(dh); |
| 3057 | } |
Remi Tricot-Le Breton | 5f17930 | 2022-02-11 12:04:51 +0100 | [diff] [blame] | 3058 | } |
| 3059 | #endif |
| 3060 | |
Remi Tricot-Le Breton | 09ebb33 | 2022-02-11 12:04:48 +0100 | [diff] [blame] | 3061 | HASSL_DH *ssl_sock_get_dh_from_bio(BIO *bio) |
| 3062 | { |
| 3063 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3064 | HASSL_DH *dh = NULL; |
| 3065 | OSSL_DECODER_CTX *dctx = NULL; |
| 3066 | const char *format = "PEM"; |
| 3067 | const char *keytype = "DH"; |
| 3068 | |
| 3069 | dctx = OSSL_DECODER_CTX_new_for_pkey(&dh, format, NULL, keytype, |
| 3070 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
| 3071 | NULL, NULL); |
| 3072 | |
| 3073 | if (dctx == NULL || OSSL_DECODER_CTX_get_num_decoders(dctx) == 0) |
| 3074 | goto end; |
| 3075 | |
| 3076 | /* The DH parameters might not be the first section found in the PEM |
| 3077 | * file so we need to iterate over all of them until we find the right |
| 3078 | * one. |
| 3079 | */ |
| 3080 | while (!BIO_eof(bio) && !dh) |
| 3081 | OSSL_DECODER_from_bio(dctx, bio); |
| 3082 | |
| 3083 | end: |
| 3084 | OSSL_DECODER_CTX_free(dctx); |
| 3085 | return dh; |
| 3086 | #else |
| 3087 | HASSL_DH *dh = NULL; |
| 3088 | |
| 3089 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 3090 | |
| 3091 | return dh; |
| 3092 | #endif |
| 3093 | } |
| 3094 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3095 | static HASSL_DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3096 | { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3097 | HASSL_DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3098 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3099 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3100 | if (in == NULL) |
| 3101 | goto end; |
| 3102 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3103 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3104 | goto end; |
| 3105 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3106 | dh = ssl_sock_get_dh_from_bio(in); |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3107 | |
| 3108 | end: |
| 3109 | if (in) |
| 3110 | BIO_free(in); |
| 3111 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 3112 | ERR_clear_error(); |
| 3113 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3114 | return dh; |
| 3115 | } |
| 3116 | |
| 3117 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 3118 | { |
| 3119 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 3120 | |
| 3121 | if (global_dh) { |
| 3122 | return 0; |
| 3123 | } |
| 3124 | |
| 3125 | return -1; |
| 3126 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3127 | #endif |
| 3128 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3129 | /* 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] | 3130 | 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] | 3131 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 3132 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3133 | { |
| 3134 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3135 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3136 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3137 | if (*name == '!') { |
| 3138 | neg = 1; |
| 3139 | name++; |
| 3140 | } |
| 3141 | if (*name == '*') { |
| 3142 | wild = 1; |
| 3143 | name++; |
| 3144 | } |
| 3145 | /* !* filter is a nop */ |
| 3146 | if (neg && wild) |
| 3147 | return order; |
| 3148 | if (*name) { |
| 3149 | int j, len; |
| 3150 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3151 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 3152 | trash.area[j] = tolower((unsigned char)name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3153 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3154 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3155 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3156 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3157 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 3158 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3159 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3160 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3161 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3162 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3163 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3164 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3165 | sc->order = order++; |
| 3166 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3167 | sc->wild = wild; |
| 3168 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3169 | sc->ckch_inst = ckch_inst; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3170 | LIST_APPEND(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3171 | } |
| 3172 | return order; |
| 3173 | } |
| 3174 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3175 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3176 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 3177 | * This function can't return an error. |
| 3178 | * |
| 3179 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 3180 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3181 | 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] | 3182 | { |
| 3183 | |
| 3184 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 3185 | struct ebmb_node *node; |
| 3186 | |
| 3187 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 3188 | |
| 3189 | /* ignore if sc0 was already inserted in a tree */ |
| 3190 | if (sc0->name.node.leaf_p) |
| 3191 | continue; |
| 3192 | |
| 3193 | /* Check for duplicates. */ |
| 3194 | if (sc0->wild) |
| 3195 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 3196 | else |
| 3197 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 3198 | |
| 3199 | for (; node; node = ebmb_next_dup(node)) { |
| 3200 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 3201 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 3202 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 3203 | /* it's a duplicate, we should remove and free it */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3204 | LIST_DELETE(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3205 | SSL_CTX_free(sc0->ctx); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3206 | ha_free(&sc0); |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 3207 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | /* if duplicate, ignore the insertion */ |
| 3212 | if (!sc0) |
| 3213 | continue; |
| 3214 | |
| 3215 | if (sc0->wild) |
| 3216 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 3217 | else |
| 3218 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3219 | } |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3220 | |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3221 | /* replace the default_ctx if required with the instance's ctx. */ |
| 3222 | if (ckch_inst->is_default) { |
| 3223 | SSL_CTX_free(bind_conf->default_ctx); |
| 3224 | SSL_CTX_up_ref(ckch_inst->ctx); |
| 3225 | bind_conf->default_ctx = ckch_inst->ctx; |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 3226 | bind_conf->default_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3231 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3232 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3233 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3234 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3235 | /* tree of crtlist (crt-list/directory) */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3236 | struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3237 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3238 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 3239 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3240 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 3241 | * DH parameter available in ckchs nor in global, the default |
| 3242 | * DH parameters are applied on the SSL_CTX. |
| 3243 | * Returns a bitfield containing the flags: |
| 3244 | * ERR_FATAL in any fatal error case |
| 3245 | * ERR_ALERT if a reason of the error is availabine in err |
| 3246 | * ERR_WARN if a warning is available into err |
| 3247 | * The value 0 means there is no error nor warning and |
| 3248 | * the operation succeed. |
| 3249 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3250 | #ifndef OPENSSL_NO_DH |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3251 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct ckch_data *data, |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3252 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3253 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3254 | int ret = 0; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3255 | HASSL_DH *dh = NULL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3256 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3257 | if (data && data->dh) { |
| 3258 | dh = data->dh; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3259 | if (!ssl_sock_set_tmp_dh(ctx, dh)) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3260 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 3261 | err && *err ? *err : "", path); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3262 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3263 | err && *err ? *err : ""); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3264 | ret |= ERR_WARN; |
| 3265 | goto end; |
| 3266 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3267 | |
| 3268 | if (ssl_dh_ptr_index >= 0) { |
| 3269 | /* store a pointer to the DH params to avoid complaining about |
| 3270 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 3271 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 3272 | } |
| 3273 | } |
| 3274 | else if (global_dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3275 | if (!ssl_sock_set_tmp_dh(ctx, global_dh)) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3276 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 3277 | err && *err ? *err : "", path); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3278 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3279 | err && *err ? *err : ""); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3280 | ret |= ERR_WARN; |
| 3281 | goto end; |
| 3282 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3283 | } |
| 3284 | else { |
| 3285 | /* Clear openssl global errors stack */ |
| 3286 | ERR_clear_error(); |
| 3287 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3288 | /* We do not want DHE ciphers to be added to the cipher list |
| 3289 | * unless there is an explicit global dh option in the conf. |
| 3290 | */ |
| 3291 | if (global_ssl.default_dh_param) { |
| 3292 | if (global_ssl.default_dh_param <= 1024) { |
| 3293 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3294 | if (local_dh_1024 == NULL) |
| 3295 | local_dh_1024 = ssl_get_dh_1024(); |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3296 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3297 | if (local_dh_1024 == NULL) { |
| 3298 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3299 | err && *err ? *err : "", path); |
| 3300 | ret |= ERR_ALERT | ERR_FATAL; |
| 3301 | goto end; |
| 3302 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3303 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3304 | if (!ssl_sock_set_tmp_dh(ctx, local_dh_1024)) { |
| 3305 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3306 | err && *err ? *err : "", path); |
| 3307 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3308 | err && *err ? *err : ""); |
| 3309 | ret |= ERR_WARN; |
| 3310 | goto end; |
| 3311 | } |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3312 | } |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3313 | else { |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3314 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3315 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh_cbk); |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3316 | #else |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3317 | ssl_sock_set_tmp_dh_from_pkey(ctx, data ? data->key : NULL); |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3318 | #endif |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3319 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3320 | } |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3321 | } |
| 3322 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3323 | end: |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3324 | ERR_clear_error(); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3325 | return ret; |
| 3326 | } |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3327 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3328 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3329 | |
| 3330 | /* Load a certificate chain into an SSL context. |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3331 | * Returns a bitfield containing the flags: |
| 3332 | * ERR_FATAL in any fatal error case |
| 3333 | * ERR_ALERT if the reason of the error is available in err |
| 3334 | * ERR_WARN if a warning is available into err |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3335 | * The caller is responsible of freeing the newly built or newly refcounted |
| 3336 | * find_chain element. |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3337 | * The value 0 means there is no error nor warning and |
| 3338 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3339 | */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3340 | static int ssl_sock_load_cert_chain(const char *path, const struct ckch_data *data, |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3341 | SSL_CTX *ctx, STACK_OF(X509) **find_chain, char **err) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3342 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3343 | int errcode = 0; |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3344 | int ret; |
| 3345 | |
| 3346 | ERR_clear_error(); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3347 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3348 | if (find_chain == NULL) { |
| 3349 | errcode |= ERR_FATAL; |
| 3350 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3351 | } |
| 3352 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3353 | if (!SSL_CTX_use_certificate(ctx, data->cert)) { |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3354 | ret = ERR_get_error(); |
| 3355 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s': %s.\n", |
| 3356 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3357 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3358 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3359 | } |
| 3360 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3361 | if (data->chain) { |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3362 | *find_chain = X509_chain_up_ref(data->chain); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3363 | } else { |
| 3364 | /* Find Certificate Chain in global */ |
| 3365 | struct issuer_chain *issuer; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3366 | issuer = ssl_get0_issuer_chain(data->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3367 | if (issuer) |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3368 | *find_chain = X509_chain_up_ref(issuer->chain); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3369 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3370 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3371 | if (!*find_chain) { |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3372 | /* always put a null chain stack in the SSL_CTX so it does not |
| 3373 | * try to build the chain from the verify store */ |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3374 | *find_chain = sk_X509_new_null(); |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3375 | } |
| 3376 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3377 | /* Load all certs in the data into the ctx_chain for the ssl_ctx */ |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3378 | #ifdef SSL_CTX_set1_chain |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3379 | if (!SSL_CTX_set1_chain(ctx, *find_chain)) { |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3380 | ret = ERR_get_error(); |
| 3381 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s': %s. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3382 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3383 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3384 | goto end; |
| 3385 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3386 | #else |
| 3387 | { /* legacy compat (< openssl 1.0.2) */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3388 | X509 *ca; |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3389 | while ((ca = sk_X509_shift(*find_chain))) |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3390 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3391 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3392 | err && *err ? *err : "", path); |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3393 | X509_free(ca); |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3394 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3395 | goto end; |
| 3396 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3397 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3398 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3399 | |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3400 | #ifdef SSL_CTX_build_cert_chain |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3401 | /* remove the Root CA from the SSL_CTX if the option is activated */ |
| 3402 | if (global_ssl.skip_self_issued_ca) { |
| 3403 | if (!SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT|SSL_BUILD_CHAIN_FLAG_UNTRUSTED|SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)) { |
| 3404 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3405 | err && *err ? *err : "", path); |
| 3406 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3407 | goto end; |
| 3408 | } |
| 3409 | } |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3410 | #endif |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3411 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3412 | end: |
| 3413 | return errcode; |
| 3414 | } |
| 3415 | |
| 3416 | |
| 3417 | /* Loads the info in ckch into ctx |
| 3418 | * Returns a bitfield containing the flags: |
| 3419 | * ERR_FATAL in any fatal error case |
| 3420 | * ERR_ALERT if the reason of the error is available in err |
| 3421 | * ERR_WARN if a warning is available into err |
| 3422 | * The value 0 means there is no error nor warning and |
| 3423 | * the operation succeed. |
| 3424 | */ |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 3425 | static int ssl_sock_put_ckch_into_ctx(const char *path, struct ckch_data *data, SSL_CTX *ctx, char **err) |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3426 | { |
| 3427 | int errcode = 0; |
| 3428 | STACK_OF(X509) *find_chain = NULL; |
| 3429 | |
William Lallemand | 5de4951 | 2022-10-27 14:41:07 +0200 | [diff] [blame] | 3430 | ERR_clear_error(); |
| 3431 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3432 | if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) { |
William Lallemand | 5de4951 | 2022-10-27 14:41:07 +0200 | [diff] [blame] | 3433 | int ret; |
| 3434 | |
| 3435 | ret = ERR_get_error(); |
| 3436 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s': %s.\n", |
| 3437 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3438 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3439 | return errcode; |
| 3440 | } |
| 3441 | |
| 3442 | /* Load certificate chain */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3443 | errcode |= ssl_sock_load_cert_chain(path, data, ctx, &find_chain, err); |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3444 | if (errcode & ERR_CODE) |
| 3445 | goto end; |
| 3446 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3447 | #ifndef OPENSSL_NO_DH |
| 3448 | /* store a NULL pointer to indicate we have not yet loaded |
| 3449 | a custom DH param file */ |
| 3450 | if (ssl_dh_ptr_index >= 0) { |
| 3451 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3452 | } |
| 3453 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3454 | errcode |= ssl_sock_load_dh_params(ctx, data, path, err); |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3455 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3456 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3457 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3458 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3459 | } |
| 3460 | #endif |
| 3461 | |
Ilya Shipitsin | 7bbf586 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 3462 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3463 | if (sctl_ex_index >= 0 && data->sctl) { |
| 3464 | if (ssl_sock_load_sctl(ctx, data->sctl) < 0) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3465 | 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] | 3466 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3467 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3468 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3469 | } |
| 3470 | } |
| 3471 | #endif |
| 3472 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 3473 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 3474 | /* Load OCSP Info into context |
| 3475 | * If OCSP update mode is set to 'on', an entry will be created in the |
| 3476 | * ocsp tree even if no ocsp_response was known during init, unless the |
| 3477 | * frontend's conf disables ocsp update explicitely. |
| 3478 | */ |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 3479 | if (ssl_sock_load_ocsp(path, ctx, data, find_chain) < 0) { |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 3480 | if (data->ocsp_response) |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3481 | 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", |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 3482 | err && *err ? *err : "", path); |
| 3483 | else |
| 3484 | memprintf(err, "%s '%s' has an OCSP URI and OCSP auto-update is set to 'on' but an error occurred (maybe the issuer could not be found)'.\n", |
| 3485 | err && *err ? *err : "", path); |
| 3486 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3487 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3488 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3489 | #endif |
| 3490 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3491 | end: |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3492 | sk_X509_pop_free(find_chain, X509_free); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3493 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3494 | } |
| 3495 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3496 | |
| 3497 | /* Loads the info of a ckch built out of a backend certificate into an SSL ctx |
| 3498 | * Returns a bitfield containing the flags: |
| 3499 | * ERR_FATAL in any fatal error case |
| 3500 | * ERR_ALERT if the reason of the error is available in err |
| 3501 | * ERR_WARN if a warning is available into err |
| 3502 | * The value 0 means there is no error nor warning and |
| 3503 | * the operation succeed. |
| 3504 | */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3505 | static int ssl_sock_put_srv_ckch_into_ctx(const char *path, const struct ckch_data *data, |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3506 | SSL_CTX *ctx, char **err) |
| 3507 | { |
| 3508 | int errcode = 0; |
| 3509 | STACK_OF(X509) *find_chain = NULL; |
| 3510 | |
| 3511 | /* Load the private key */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3512 | if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) { |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3513 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3514 | err && *err ? *err : "", path); |
| 3515 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3516 | } |
| 3517 | |
| 3518 | /* Load certificate chain */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3519 | errcode |= ssl_sock_load_cert_chain(path, data, ctx, &find_chain, err); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3520 | if (errcode & ERR_CODE) |
| 3521 | goto end; |
| 3522 | |
| 3523 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3524 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3525 | err && *err ? *err : "", path); |
| 3526 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3527 | } |
| 3528 | |
| 3529 | end: |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3530 | sk_X509_pop_free(find_chain, X509_free); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3531 | return errcode; |
| 3532 | } |
| 3533 | |
| 3534 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3535 | /* |
| 3536 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3537 | * |
| 3538 | * Returns a bitfield containing the flags: |
| 3539 | * ERR_FATAL in any fatal error case |
| 3540 | * ERR_ALERT if the reason of the error is available in err |
| 3541 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3542 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3543 | 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] | 3544 | 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] | 3545 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3546 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3547 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3548 | int order = 0; |
| 3549 | X509_NAME *xname; |
| 3550 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3551 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3552 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3553 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3554 | STACK_OF(GENERAL_NAME) *names; |
| 3555 | #endif |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3556 | struct ckch_data *data; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3557 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3558 | int errcode = 0; |
| 3559 | |
| 3560 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3561 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3562 | if (!ckchs || !ckchs->data) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3563 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3564 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3565 | data = ckchs->data; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3566 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3567 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3568 | if (!ctx) { |
| 3569 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3570 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3571 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3572 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3573 | } |
| 3574 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3575 | errcode |= ssl_sock_put_ckch_into_ctx(path, data, ctx, err); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3576 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3577 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3578 | |
| 3579 | ckch_inst = ckch_inst_new(); |
| 3580 | if (!ckch_inst) { |
| 3581 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3582 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3583 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3584 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3585 | } |
| 3586 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3587 | pkey = X509_get_pubkey(data->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3588 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3589 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3590 | switch(EVP_PKEY_base_id(pkey)) { |
| 3591 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3592 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3593 | break; |
| 3594 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3595 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3596 | break; |
| 3597 | case EVP_PKEY_DSA: |
| 3598 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3599 | break; |
| 3600 | } |
| 3601 | EVP_PKEY_free(pkey); |
| 3602 | } |
| 3603 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3604 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3605 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3606 | 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] | 3607 | if (order < 0) { |
| 3608 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3609 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3610 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3611 | } |
| 3612 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3613 | } |
| 3614 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3615 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3616 | names = X509_get_ext_d2i(data->cert, NID_subject_alt_name, NULL, NULL); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3617 | if (names) { |
| 3618 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3619 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3620 | if (name->type == GEN_DNS) { |
| 3621 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3622 | 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] | 3623 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3624 | if (order < 0) { |
| 3625 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3626 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3627 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3628 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3629 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3630 | } |
| 3631 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3632 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3633 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3634 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3635 | xname = X509_get_subject_name(data->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3636 | i = -1; |
| 3637 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3638 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3639 | ASN1_STRING *value; |
| 3640 | |
| 3641 | value = X509_NAME_ENTRY_get_data(entry); |
| 3642 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3643 | 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] | 3644 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3645 | if (order < 0) { |
| 3646 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3647 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3648 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3649 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3650 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3651 | } |
| 3652 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3653 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3654 | * the tree, so it will be discovered and cleaned in time. |
| 3655 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3656 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3657 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3658 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3659 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3660 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3661 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3662 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3663 | } |
| 3664 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3665 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3666 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3667 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3668 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3669 | SSL_CTX_up_ref(ctx); |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 3670 | bind_conf->default_inst = ckch_inst; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3671 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3672 | |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3673 | /* Always keep a reference to the newly constructed SSL_CTX in the |
| 3674 | * instance. This way if the instance has no SNIs, the SSL_CTX will |
| 3675 | * still be linked. */ |
| 3676 | SSL_CTX_up_ref(ctx); |
| 3677 | ckch_inst->ctx = ctx; |
| 3678 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3679 | /* everything succeed, the ckch instance can be used */ |
| 3680 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3681 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3682 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3683 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3684 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3685 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3686 | *ckchi = ckch_inst; |
| 3687 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3688 | |
| 3689 | error: |
| 3690 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3691 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3692 | if (ckch_inst->is_default) |
| 3693 | SSL_CTX_free(ctx); |
| 3694 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3695 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3696 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3697 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3698 | SSL_CTX_free(ctx); |
| 3699 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3700 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3701 | } |
| 3702 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3703 | |
| 3704 | /* |
| 3705 | * This function allocate a ckch_inst that will be used on the backend side |
| 3706 | * (server line) |
| 3707 | * |
| 3708 | * Returns a bitfield containing the flags: |
| 3709 | * ERR_FATAL in any fatal error case |
| 3710 | * ERR_ALERT if the reason of the error is available in err |
| 3711 | * ERR_WARN if a warning is available into err |
| 3712 | */ |
| 3713 | int ckch_inst_new_load_srv_store(const char *path, struct ckch_store *ckchs, |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 3714 | struct ckch_inst **ckchi, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3715 | { |
| 3716 | SSL_CTX *ctx; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3717 | struct ckch_data *data; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3718 | struct ckch_inst *ckch_inst = NULL; |
| 3719 | int errcode = 0; |
| 3720 | |
| 3721 | *ckchi = NULL; |
| 3722 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3723 | if (!ckchs || !ckchs->data) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3724 | return ERR_FATAL; |
| 3725 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3726 | data = ckchs->data; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3727 | |
| 3728 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 3729 | if (!ctx) { |
| 3730 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3731 | err && *err ? *err : "", path); |
| 3732 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3733 | goto error; |
| 3734 | } |
| 3735 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3736 | errcode |= ssl_sock_put_srv_ckch_into_ctx(path, data, ctx, err); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3737 | if (errcode & ERR_CODE) |
| 3738 | goto error; |
| 3739 | |
| 3740 | ckch_inst = ckch_inst_new(); |
| 3741 | if (!ckch_inst) { |
| 3742 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3743 | err && *err ? *err : "", path); |
| 3744 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3745 | goto error; |
| 3746 | } |
| 3747 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3748 | /* everything succeed, the ckch instance can be used */ |
| 3749 | ckch_inst->bind_conf = NULL; |
| 3750 | ckch_inst->ssl_conf = NULL; |
| 3751 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 3752 | ckch_inst->ctx = ctx; |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3753 | ckch_inst->is_server_instance = 1; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3754 | |
| 3755 | *ckchi = ckch_inst; |
| 3756 | return errcode; |
| 3757 | |
| 3758 | error: |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3759 | SSL_CTX_free(ctx); |
| 3760 | |
| 3761 | return errcode; |
| 3762 | } |
| 3763 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 3764 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3765 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 3766 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3767 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3768 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3769 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3770 | |
| 3771 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | e7eb1fe | 2020-09-16 16:17:51 +0200 | [diff] [blame] | 3772 | 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] | 3773 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3774 | if (errcode & ERR_CODE) |
| 3775 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3776 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3777 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3778 | |
| 3779 | /* succeed, add the instance to the ckch_store's list of instance */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3780 | LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3781 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3782 | } |
| 3783 | |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3784 | /* This function generates a <struct ckch_inst *> for a <struct server *>, and |
| 3785 | * fill the SSL_CTX of the server. |
| 3786 | * |
| 3787 | * Returns a set of ERR_* flags possibly with an error in <err>. */ |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3788 | static int ssl_sock_load_srv_ckchs(const char *path, struct ckch_store *ckchs, |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3789 | struct server *server, struct ckch_inst **ckch_inst, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3790 | { |
| 3791 | int errcode = 0; |
| 3792 | |
| 3793 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 3794 | errcode |= ckch_inst_new_load_srv_store(path, ckchs, ckch_inst, err); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3795 | |
| 3796 | if (errcode & ERR_CODE) |
| 3797 | return errcode; |
| 3798 | |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3799 | (*ckch_inst)->server = server; |
| 3800 | /* Keep the reference to the SSL_CTX in the server. */ |
| 3801 | SSL_CTX_up_ref((*ckch_inst)->ctx); |
| 3802 | server->ssl_ctx.ctx = (*ckch_inst)->ctx; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3803 | /* succeed, add the instance to the ckch_store's list of instance */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3804 | LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3805 | return errcode; |
| 3806 | } |
| 3807 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3808 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3809 | |
| 3810 | |
| 3811 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3812 | * done once. Zero is returned if the operation fails. No error is returned |
| 3813 | * if the random is said as not implemented, because we expect that openssl |
| 3814 | * will use another method once needed. |
| 3815 | */ |
Amaury Denoyelle | c593bcd | 2021-05-19 15:35:29 +0200 | [diff] [blame] | 3816 | int ssl_initialize_random(void) |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3817 | { |
| 3818 | unsigned char random; |
| 3819 | static int random_initialized = 0; |
| 3820 | |
| 3821 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3822 | random_initialized = 1; |
| 3823 | |
| 3824 | return random_initialized; |
| 3825 | } |
| 3826 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3827 | /* Load a crt-list file, this is done in 2 parts: |
| 3828 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 3829 | * - generate the instances by iterating on entries in the crtlist struct |
| 3830 | * |
| 3831 | * Nothing is locked there, this function is used in the configuration parser. |
| 3832 | * |
| 3833 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3834 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3835 | 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] | 3836 | { |
| 3837 | struct crtlist *crtlist = NULL; |
| 3838 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3839 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3840 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3841 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3842 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3843 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3844 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 3845 | if (!bind_conf_node) { |
| 3846 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 3847 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3848 | goto error; |
| 3849 | } |
| 3850 | bind_conf_node->next = NULL; |
| 3851 | bind_conf_node->bind_conf = bind_conf; |
| 3852 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3853 | /* strip trailing slashes, including first one */ |
| 3854 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 3855 | *end = 0; |
| 3856 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3857 | /* look for an existing crtlist or create one */ |
| 3858 | eb = ebst_lookup(&crtlists_tree, file); |
| 3859 | if (eb) { |
| 3860 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 3861 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3862 | /* load a crt-list OR a directory */ |
| 3863 | if (dir) |
| 3864 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 3865 | else |
| 3866 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 3867 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3868 | if (!(cfgerr & ERR_CODE)) |
| 3869 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 3870 | } |
| 3871 | |
| 3872 | if (cfgerr & ERR_CODE) { |
| 3873 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3874 | goto error; |
| 3875 | } |
| 3876 | |
| 3877 | /* generates ckch instance from the crtlist_entry */ |
| 3878 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3879 | struct ckch_store *store; |
| 3880 | struct ckch_inst *ckch_inst = NULL; |
| 3881 | |
| 3882 | store = entry->node.key; |
| 3883 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 3884 | if (cfgerr & ERR_CODE) { |
| 3885 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 3886 | goto error; |
| 3887 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3888 | LIST_APPEND(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 3889 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3890 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3891 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3892 | /* add the bind_conf to the list */ |
| 3893 | bind_conf_node->next = crtlist->bind_conf; |
| 3894 | crtlist->bind_conf = bind_conf_node; |
| 3895 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3896 | return cfgerr; |
| 3897 | error: |
| 3898 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3899 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3900 | struct ckch_inst *inst, *s_inst; |
| 3901 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3902 | lastentry = entry; /* which entry we tried to generate last */ |
| 3903 | if (lastentry) { |
| 3904 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3905 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 3906 | break; |
| 3907 | |
| 3908 | 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] | 3909 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3910 | /* this was not generated for this bind_conf, skip */ |
| 3911 | if (inst->bind_conf != bind_conf) |
| 3912 | continue; |
| 3913 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3914 | /* free the sni_ctx and instance */ |
| 3915 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3916 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3917 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3918 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3919 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3920 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3921 | return cfgerr; |
| 3922 | } |
| 3923 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3924 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 3925 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 3926 | { |
| 3927 | struct stat buf; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3928 | int cfgerr = 0; |
| 3929 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3930 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3931 | int found = 0; /* did we found a file to load ? */ |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3932 | |
| 3933 | if ((ckchs = ckchs_lookup(path))) { |
| 3934 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3935 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3936 | found++; |
| 3937 | } else if (stat(path, &buf) == 0) { |
| 3938 | found++; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3939 | if (S_ISDIR(buf.st_mode) == 0) { |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3940 | ckchs = ckchs_load_cert_file(path, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3941 | if (!ckchs) |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3942 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3943 | 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] | 3944 | } else { |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3945 | 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] | 3946 | } |
| 3947 | } else { |
| 3948 | /* stat failed, could be a bundle */ |
| 3949 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3950 | char fp[MAXPATHLEN+1] = {0}; |
| 3951 | int n = 0; |
| 3952 | |
| 3953 | /* Load all possible certs and keys in separate ckch_store */ |
| 3954 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3955 | struct stat buf; |
| 3956 | int ret; |
| 3957 | |
| 3958 | ret = snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3959 | if (ret > sizeof(fp)) |
| 3960 | continue; |
| 3961 | |
| 3962 | if ((ckchs = ckchs_lookup(fp))) { |
| 3963 | cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3964 | found++; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3965 | } else { |
| 3966 | if (stat(fp, &buf) == 0) { |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3967 | found++; |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3968 | ckchs = ckchs_load_cert_file(fp, err); |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3969 | if (!ckchs) |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3970 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3971 | cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3972 | } |
| 3973 | } |
| 3974 | } |
William Lallemand | b7fdfdf | 2020-12-04 15:45:02 +0100 | [diff] [blame] | 3975 | #if HA_OPENSSL_VERSION_NUMBER < 0x10101000L |
| 3976 | if (found) { |
| 3977 | memprintf(err, "%sCan't load '%s'. Loading a multi certificates bundle requires OpenSSL >= 1.1.1\n", |
| 3978 | err && *err ? *err : "", path); |
| 3979 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3980 | } |
| 3981 | #endif |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3982 | } |
| 3983 | } |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3984 | if (!found) { |
| 3985 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3986 | err && *err ? *err : "", path, strerror(errno)); |
| 3987 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3988 | } |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3989 | |
| 3990 | return cfgerr; |
| 3991 | } |
| 3992 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3993 | |
| 3994 | /* Create a full ssl context and ckch instance that will be used for a specific |
| 3995 | * backend server (server configuration line). |
| 3996 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3997 | */ |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 3998 | int ssl_sock_load_srv_cert(char *path, struct server *server, int create_if_none, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3999 | { |
| 4000 | struct stat buf; |
| 4001 | int cfgerr = 0; |
| 4002 | struct ckch_store *ckchs; |
| 4003 | int found = 0; /* did we found a file to load ? */ |
| 4004 | |
| 4005 | if ((ckchs = ckchs_lookup(path))) { |
| 4006 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 4007 | cfgerr |= ssl_sock_load_srv_ckchs(path, ckchs, server, &server->ssl_ctx.inst, err); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4008 | found++; |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4009 | } else { |
| 4010 | if (!create_if_none) { |
| 4011 | memprintf(err, "%sunable to stat SSL certificate '%s'.\n", |
| 4012 | err && *err ? *err : "", path); |
| 4013 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4014 | goto out; |
| 4015 | } |
| 4016 | |
| 4017 | if (stat(path, &buf) == 0) { |
| 4018 | /* We do not manage directories on backend side. */ |
| 4019 | if (S_ISDIR(buf.st_mode) == 0) { |
| 4020 | ++found; |
| 4021 | ckchs = ckchs_load_cert_file(path, err); |
| 4022 | if (!ckchs) |
| 4023 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4024 | cfgerr |= ssl_sock_load_srv_ckchs(path, ckchs, server, &server->ssl_ctx.inst, err); |
| 4025 | } |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4026 | } |
| 4027 | } |
| 4028 | if (!found) { |
| 4029 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4030 | err && *err ? *err : "", path, strerror(errno)); |
| 4031 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4032 | } |
| 4033 | |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4034 | out: |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4035 | return cfgerr; |
| 4036 | } |
| 4037 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4038 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4039 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4040 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4041 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4042 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4043 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4044 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4045 | SSL_OP_NO_SSLv2 | |
| 4046 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4047 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4048 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4049 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 4050 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4051 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4052 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4053 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4054 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4055 | SSL_MODE_RELEASE_BUFFERS | |
| 4056 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 4057 | 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] | 4058 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4059 | int flags = MC_SSL_O_ALL; |
| 4060 | int cfgerr = 0; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 4061 | const int default_min_ver = CONF_TLSV12; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4062 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4063 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4064 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4065 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4066 | 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] | 4067 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 4068 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4069 | 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] | 4070 | else |
| 4071 | flags = conf_ssl_methods->flags; |
| 4072 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 4073 | min = conf_ssl_methods->min; |
| 4074 | max = conf_ssl_methods->max; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 4075 | |
| 4076 | /* default minimum is TLSV12, */ |
| 4077 | if (!min) { |
| 4078 | if (!max || (max >= default_min_ver)) { |
| 4079 | min = default_min_ver; |
| 4080 | } else { |
| 4081 | 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). " |
| 4082 | "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n", |
| 4083 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name); |
| 4084 | min = max; |
| 4085 | } |
| 4086 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4087 | /* 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] | 4088 | if (min) |
| 4089 | flags |= (methodVersions[min].flag - 1); |
| 4090 | if (max) |
| 4091 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4092 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4093 | min = max = CONF_TLSV_NONE; |
| 4094 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4095 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4096 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4097 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4098 | if (min) { |
| 4099 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4100 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 4101 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4102 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 4103 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4104 | hole = 0; |
| 4105 | } |
| 4106 | max = i; |
| 4107 | } |
| 4108 | else { |
| 4109 | min = max = i; |
| 4110 | } |
| 4111 | } |
| 4112 | else { |
| 4113 | if (min) |
| 4114 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4115 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4116 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4117 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4118 | 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] | 4119 | cfgerr += 1; |
| 4120 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4121 | /* save real min/max in bind_conf */ |
| 4122 | conf_ssl_methods->min = min; |
| 4123 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4124 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4125 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4126 | /* 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] | 4127 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4128 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4129 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4130 | else |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4131 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) { |
| 4132 | /* clear every version flags in case SSL_CTX_new() |
| 4133 | * returns an SSL_CTX with disabled versions */ |
| 4134 | SSL_CTX_clear_options(ctx, methodVersions[i].option); |
| 4135 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4136 | if (flags & methodVersions[i].flag) |
| 4137 | options |= methodVersions[i].option; |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4138 | |
| 4139 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4140 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4141 | /* 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] | 4142 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4143 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 4144 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4145 | |
| 4146 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 4147 | options |= SSL_OP_NO_TICKET; |
| 4148 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 4149 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 4150 | |
| 4151 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 4152 | options |= SSL_OP_NO_RENEGOTIATION; |
| 4153 | #endif |
| 4154 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4155 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4156 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 4157 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4158 | if (global_ssl.async) |
| 4159 | mode |= SSL_MODE_ASYNC; |
| 4160 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4161 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4162 | if (global_ssl.life_time) |
| 4163 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4164 | |
| 4165 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4166 | # ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4167 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 4168 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4169 | # elif defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 4170 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 4171 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 4172 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4173 | # endif /* ! SSL_OP_NO_ANTI_REPLAY */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 4174 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 4175 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4176 | # else /* ! OPENSSL_IS_BORINGSSL && ! HAVE_SSL_CLIENT_HELLO_CB */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4177 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4178 | # endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 4179 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4180 | #endif /* ! SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4181 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4182 | } |
| 4183 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4184 | |
| 4185 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 4186 | { |
| 4187 | if (first == block) { |
| 4188 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4189 | if (first->len > 0) |
| 4190 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | /* return first block from sh_ssl_sess */ |
| 4195 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 4196 | { |
| 4197 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 4198 | |
| 4199 | } |
| 4200 | |
| 4201 | /* store a session into the cache |
| 4202 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 4203 | * data: asn1 encoded session |
| 4204 | * data_len: asn1 encoded session length |
| 4205 | * Returns 1 id session was stored (else 0) |
| 4206 | */ |
| 4207 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 4208 | { |
| 4209 | struct shared_block *first; |
| 4210 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 4211 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4212 | 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] | 4213 | if (!first) { |
| 4214 | /* Could not retrieve enough free blocks to store that session */ |
| 4215 | return 0; |
| 4216 | } |
| 4217 | |
| 4218 | /* STORE the key in the first elem */ |
| 4219 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4220 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 4221 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4222 | |
| 4223 | /* it returns the already existing node |
| 4224 | or current node if none, never returns null */ |
| 4225 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4226 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4227 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4228 | /* release the reserved row */ |
William Lallemand | 222e5a2 | 2023-01-31 14:12:28 +0100 | [diff] [blame] | 4229 | first->len = 0; /* the len must be liberated in order not to call the release callback on it */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4230 | shctx_row_dec_hot(ssl_shctx, first); |
| 4231 | /* replace the previous session already in the tree */ |
| 4232 | sh_ssl_sess = oldsh_ssl_sess; |
| 4233 | /* ignore the previous session data, only use the header */ |
| 4234 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4235 | shctx_row_inc_hot(ssl_shctx, first); |
| 4236 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4237 | } |
| 4238 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4239 | 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] | 4240 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4241 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4245 | |
| 4246 | return 1; |
| 4247 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4248 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4249 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4250 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4251 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4252 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4253 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4254 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4255 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4256 | |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4257 | /* RWLOCK: only read lock the SSL cache even when writing in it because there is |
| 4258 | * one cache per thread, it only prevents to flush it from the CLI in |
| 4259 | * another thread */ |
| 4260 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4261 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4262 | int len; |
| 4263 | unsigned char *ptr; |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4264 | const char *sni; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4265 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4266 | len = i2d_SSL_SESSION(sess, NULL); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4267 | sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4268 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4269 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4270 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4271 | } else { |
Willy Tarreau | 3bda3f4 | 2021-02-26 21:05:08 +0100 | [diff] [blame] | 4272 | ptr = realloc(s->ssl_ctx.reused_sess[tid].ptr, len); |
| 4273 | s->ssl_ctx.reused_sess[tid].ptr = ptr; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4274 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4275 | } |
| 4276 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4277 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4278 | &ptr); |
| 4279 | } |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4280 | |
| 4281 | if (s->ssl_ctx.reused_sess[tid].sni) { |
| 4282 | /* if the new sni is empty or isn' t the same as the old one */ |
| 4283 | if ((!sni) || strcmp(s->ssl_ctx.reused_sess[tid].sni, sni) != 0) { |
| 4284 | ha_free(&s->ssl_ctx.reused_sess[tid].sni); |
| 4285 | if (sni) |
| 4286 | s->ssl_ctx.reused_sess[tid].sni = strdup(sni); |
| 4287 | } |
| 4288 | } else if (sni) { |
| 4289 | /* if there wasn't an old sni but there is a new one */ |
| 4290 | s->ssl_ctx.reused_sess[tid].sni = strdup(sni); |
| 4291 | } |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4292 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4293 | } else { |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4294 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4295 | ha_free(&s->ssl_ctx.reused_sess[tid].ptr); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4296 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4300 | } |
| 4301 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4302 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4303 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4304 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4305 | { |
| 4306 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4307 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4308 | unsigned char *p; |
| 4309 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4310 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4311 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4312 | |
| 4313 | /* 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] | 4314 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4315 | * note: SSL_SESSION_set1_id is using |
| 4316 | * a memcpy so we need to use a different pointer |
| 4317 | * than sid_data or sid_ctx_data to avoid valgrind |
| 4318 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4319 | */ |
| 4320 | |
| 4321 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4322 | |
| 4323 | /* copy value in an other buffer */ |
| 4324 | memcpy(encid, sid_data, sid_length); |
| 4325 | |
| 4326 | /* pad with 0 */ |
| 4327 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4328 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4329 | |
| 4330 | /* force length to zero to avoid ASN1 encoding */ |
| 4331 | SSL_SESSION_set1_id(sess, encid, 0); |
| 4332 | |
| 4333 | /* force length to zero to avoid ASN1 encoding */ |
| 4334 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4335 | |
| 4336 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4337 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4338 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4339 | goto err; |
| 4340 | |
| 4341 | p = encsess; |
| 4342 | |
| 4343 | /* process ASN1 session encoding before the lock */ |
| 4344 | i2d_SSL_SESSION(sess, &p); |
| 4345 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4346 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4347 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4348 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4349 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4350 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4351 | err: |
| 4352 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4353 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 4354 | 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] | 4355 | |
| 4356 | return 0; /* do not increment session reference count */ |
| 4357 | } |
| 4358 | |
| 4359 | /* 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] | 4360 | 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] | 4361 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4362 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4363 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4364 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4365 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4366 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4367 | |
Willy Tarreau | 4c19e99 | 2021-06-15 16:39:22 +0200 | [diff] [blame] | 4368 | _HA_ATOMIC_INC(&global.shctx_lookups); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4369 | |
| 4370 | /* allow the session to be freed automatically by openssl */ |
| 4371 | *do_copy = 0; |
| 4372 | |
| 4373 | /* tree key is zeros padded sessionid */ |
| 4374 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4375 | memcpy(tmpkey, key, key_len); |
| 4376 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4377 | key = tmpkey; |
| 4378 | } |
| 4379 | |
| 4380 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4381 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4382 | |
| 4383 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4384 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4385 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4386 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4387 | shctx_unlock(ssl_shctx); |
Willy Tarreau | 4c19e99 | 2021-06-15 16:39:22 +0200 | [diff] [blame] | 4388 | _HA_ATOMIC_INC(&global.shctx_misses); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4389 | return NULL; |
| 4390 | } |
| 4391 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4392 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4393 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4394 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4395 | 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] | 4396 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4397 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4398 | |
| 4399 | /* decode ASN1 session */ |
| 4400 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4401 | 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] | 4402 | /* Reset session id and session id contenxt */ |
| 4403 | if (sess) { |
| 4404 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4405 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4406 | } |
| 4407 | |
| 4408 | return sess; |
| 4409 | } |
| 4410 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4411 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4412 | /* 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] | 4413 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4414 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4415 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4416 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4417 | unsigned int sid_length; |
| 4418 | const unsigned char *sid_data; |
| 4419 | (void)ctx; |
| 4420 | |
| 4421 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4422 | /* tree key is zeros padded sessionid */ |
| 4423 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4424 | memcpy(tmpkey, sid_data, sid_length); |
| 4425 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4426 | sid_data = tmpkey; |
| 4427 | } |
| 4428 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4429 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4430 | |
| 4431 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4432 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4433 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4434 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4435 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4436 | } |
| 4437 | |
| 4438 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4439 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4440 | } |
| 4441 | |
| 4442 | /* Set session cache mode to server and disable openssl internal cache. |
| 4443 | * Set shared cache callbacks on an ssl context. |
| 4444 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4445 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4446 | { |
| 4447 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4448 | |
| 4449 | if (!ssl_shctx) { |
| 4450 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4451 | return; |
| 4452 | } |
| 4453 | |
| 4454 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4455 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4456 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4457 | |
| 4458 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4459 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4460 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4461 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4462 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4463 | |
| 4464 | /* |
| 4465 | * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format |
| 4466 | * |
| 4467 | * The format is: |
| 4468 | * * <Label> <space> <ClientRandom> <space> <Secret> |
| 4469 | * We only need to copy the secret as there is a sample fetch for the ClientRandom |
| 4470 | */ |
| 4471 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 4472 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4473 | void SSL_CTX_keylog(const SSL *ssl, const char *line) |
| 4474 | { |
| 4475 | struct ssl_keylog *keylog; |
| 4476 | char *lastarg = NULL; |
| 4477 | char *dst = NULL; |
| 4478 | |
| 4479 | keylog = SSL_get_ex_data(ssl, ssl_keylog_index); |
| 4480 | if (!keylog) |
| 4481 | return; |
| 4482 | |
| 4483 | lastarg = strrchr(line, ' '); |
| 4484 | if (lastarg == NULL || ++lastarg == NULL) |
| 4485 | return; |
| 4486 | |
| 4487 | dst = pool_alloc(pool_head_ssl_keylog_str); |
| 4488 | if (!dst) |
| 4489 | return; |
| 4490 | |
| 4491 | strncpy(dst, lastarg, SSL_KEYLOG_MAX_SECRET_SIZE-1); |
| 4492 | dst[SSL_KEYLOG_MAX_SECRET_SIZE-1] = '\0'; |
| 4493 | |
| 4494 | if (strncmp(line, "CLIENT_RANDOM ", strlen("CLIENT RANDOM ")) == 0) { |
| 4495 | if (keylog->client_random) |
| 4496 | goto error; |
| 4497 | keylog->client_random = dst; |
| 4498 | |
| 4499 | } else if (strncmp(line, "CLIENT_EARLY_TRAFFIC_SECRET ", strlen("CLIENT_EARLY_TRAFFIC_SECRET ")) == 0) { |
| 4500 | if (keylog->client_early_traffic_secret) |
| 4501 | goto error; |
| 4502 | keylog->client_early_traffic_secret = dst; |
| 4503 | |
| 4504 | } else if (strncmp(line, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ", strlen("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 4505 | if(keylog->client_handshake_traffic_secret) |
| 4506 | goto error; |
| 4507 | keylog->client_handshake_traffic_secret = dst; |
| 4508 | |
| 4509 | } else if (strncmp(line, "SERVER_HANDSHAKE_TRAFFIC_SECRET ", strlen("SERVER_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 4510 | if (keylog->server_handshake_traffic_secret) |
| 4511 | goto error; |
| 4512 | keylog->server_handshake_traffic_secret = dst; |
| 4513 | |
| 4514 | } else if (strncmp(line, "CLIENT_TRAFFIC_SECRET_0 ", strlen("CLIENT_TRAFFIC_SECRET_0 ")) == 0) { |
| 4515 | if (keylog->client_traffic_secret_0) |
| 4516 | goto error; |
| 4517 | keylog->client_traffic_secret_0 = dst; |
| 4518 | |
| 4519 | } else if (strncmp(line, "SERVER_TRAFFIC_SECRET_0 ", strlen("SERVER_TRAFFIC_SECRET_0 ")) == 0) { |
| 4520 | if (keylog->server_traffic_secret_0) |
| 4521 | goto error; |
| 4522 | keylog->server_traffic_secret_0 = dst; |
| 4523 | |
| 4524 | } else if (strncmp(line, "EARLY_EXPORTER_SECRET ", strlen("EARLY_EXPORTER_SECRET ")) == 0) { |
| 4525 | if (keylog->early_exporter_secret) |
| 4526 | goto error; |
| 4527 | keylog->early_exporter_secret = dst; |
| 4528 | |
| 4529 | } else if (strncmp(line, "EXPORTER_SECRET ", strlen("EXPORTER_SECRET ")) == 0) { |
| 4530 | if (keylog->exporter_secret) |
| 4531 | goto error; |
| 4532 | keylog->exporter_secret = dst; |
| 4533 | } else { |
| 4534 | goto error; |
| 4535 | } |
| 4536 | |
| 4537 | return; |
| 4538 | |
| 4539 | error: |
| 4540 | pool_free(pool_head_ssl_keylog_str, dst); |
| 4541 | |
| 4542 | return; |
| 4543 | } |
| 4544 | #endif |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4545 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4546 | /* |
| 4547 | * This function applies the SSL configuration on a SSL_CTX |
| 4548 | * It returns an error code and fills the <err> buffer |
| 4549 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4550 | static 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] | 4551 | { |
| 4552 | struct proxy *curproxy = bind_conf->frontend; |
| 4553 | int cfgerr = 0; |
| 4554 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4555 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4556 | const char *conf_ciphers; |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 4557 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4558 | const char *conf_ciphersuites; |
| 4559 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4560 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4561 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4562 | if (ssl_conf) { |
| 4563 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4564 | int i, min, max; |
| 4565 | int flags = MC_SSL_O_ALL; |
| 4566 | |
| 4567 | /* 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] | 4568 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4569 | 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] | 4570 | if (min) |
| 4571 | flags |= (methodVersions[min].flag - 1); |
| 4572 | if (max) |
| 4573 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4574 | min = max = CONF_TLSV_NONE; |
| 4575 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4576 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4577 | if (min) |
| 4578 | max = i; |
| 4579 | else |
| 4580 | min = max = i; |
| 4581 | } |
| 4582 | /* save real min/max */ |
| 4583 | conf_ssl_methods->min = min; |
| 4584 | conf_ssl_methods->max = max; |
| 4585 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4586 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4587 | 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] | 4588 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4589 | } |
| 4590 | } |
| 4591 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4592 | 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] | 4593 | case SSL_SOCK_VERIFY_NONE: |
| 4594 | verify = SSL_VERIFY_NONE; |
| 4595 | break; |
| 4596 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4597 | verify = SSL_VERIFY_PEER; |
| 4598 | break; |
| 4599 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4600 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4601 | break; |
| 4602 | } |
| 4603 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4604 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4605 | 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] | 4606 | 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] | 4607 | 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] | 4608 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4609 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4610 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4611 | 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] | 4612 | 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] | 4613 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4614 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4615 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 4616 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 4617 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4618 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4619 | } |
| 4620 | 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] | 4621 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 4622 | 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] | 4623 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4624 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4625 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4626 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4627 | 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] | 4628 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4629 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4630 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4631 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4632 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4633 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4634 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4635 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4636 | 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] | 4637 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4638 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4639 | else { |
| 4640 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4641 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4642 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4643 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4644 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4645 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4646 | #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] | 4647 | if(bind_conf->keys_ref) { |
Remi Tricot-Le Breton | 8ea1f5f | 2022-02-08 17:45:58 +0100 | [diff] [blame] | 4648 | if (!SSL_CTX_set_tlsext_ticket_key_evp_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4649 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4650 | 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] | 4651 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4652 | } |
| 4653 | } |
| 4654 | #endif |
| 4655 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4656 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4657 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4658 | if (conf_ciphers && |
| 4659 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4660 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4661 | 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] | 4662 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4663 | } |
| 4664 | |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 4665 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4666 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4667 | if (conf_ciphersuites && |
| 4668 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4669 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4670 | 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] | 4671 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4672 | } |
| 4673 | #endif |
| 4674 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4675 | #ifndef OPENSSL_NO_DH |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 4676 | if (!local_dh_1024) |
| 4677 | local_dh_1024 = ssl_get_dh_1024(); |
| 4678 | if (!local_dh_2048) |
| 4679 | local_dh_2048 = ssl_get_dh_2048(); |
| 4680 | if (!local_dh_4096) |
| 4681 | local_dh_4096 = ssl_get_dh_4096(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4682 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4683 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4684 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Ilya Shipitsin | 7ff7747 | 2021-02-08 16:55:06 +0500 | [diff] [blame] | 4685 | #ifdef SSL_CTRL_SET_MSG_CALLBACK |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4686 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4687 | #endif |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 4688 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | b60a77b | 2022-11-18 15:00:15 +0100 | [diff] [blame] | 4689 | /* only activate the keylog callback if it was required to prevent performance loss */ |
| 4690 | if (global_ssl.keylog > 0) |
| 4691 | SSL_CTX_set_keylog_callback(ctx, SSL_CTX_keylog); |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4692 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4693 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4694 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4695 | ssl_conf_cur = NULL; |
| 4696 | if (ssl_conf && ssl_conf->npn_str) |
| 4697 | ssl_conf_cur = ssl_conf; |
| 4698 | else if (bind_conf->ssl_conf.npn_str) |
| 4699 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4700 | if (ssl_conf_cur) |
| 4701 | 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] | 4702 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4703 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4704 | ssl_conf_cur = NULL; |
| 4705 | if (ssl_conf && ssl_conf->alpn_str) |
| 4706 | ssl_conf_cur = ssl_conf; |
| 4707 | else if (bind_conf->ssl_conf.alpn_str) |
| 4708 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4709 | if (ssl_conf_cur) |
| 4710 | 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] | 4711 | #endif |
Ilya Shipitsin | 0aa8c29 | 2020-11-04 00:39:07 +0500 | [diff] [blame] | 4712 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4713 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4714 | if (conf_curves) { |
| 4715 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4716 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4717 | 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] | 4718 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4719 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4720 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4721 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4722 | #endif /* defined(SSL_CTX_set1_curves_list) */ |
| 4723 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4724 | if (!conf_curves) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4725 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4726 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4727 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4728 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4729 | NULL); |
| 4730 | |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4731 | if (ecdhe && SSL_CTX_set1_curves_list(ctx, ecdhe) == 0) { |
| 4732 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4733 | err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4734 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4735 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4736 | #endif /* defined(SSL_CTX_set1_curves_list) */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4737 | #else |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4738 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 4739 | int i; |
| 4740 | EC_KEY *ecdh; |
| 4741 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4742 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4743 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4744 | ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4745 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4746 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4747 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4748 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4749 | 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] | 4750 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4751 | } |
| 4752 | else { |
| 4753 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4754 | EC_KEY_free(ecdh); |
| 4755 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4756 | #endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */ |
| 4757 | #endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */ |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4758 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4759 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4760 | return cfgerr; |
| 4761 | } |
| 4762 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4763 | |
| 4764 | /* |
| 4765 | * Prepare the SSL_CTX based on the bind line configuration. |
| 4766 | * Since the CA file loading is made depending on the verify option of the bind |
| 4767 | * line, the link between the SSL_CTX and the CA file tree entry is made here. |
| 4768 | * If we want to create a link between the CA file entry and the corresponding |
| 4769 | * ckch instance (for CA file hot update), it needs to be done after |
| 4770 | * ssl_sock_prepare_ctx. |
| 4771 | * Returns 0 in case of success. |
| 4772 | */ |
| 4773 | int ssl_sock_prep_ctx_and_inst(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4774 | SSL_CTX *ctx, struct ckch_inst *ckch_inst, char **err) |
| 4775 | { |
| 4776 | int errcode = 0; |
| 4777 | |
| 4778 | errcode |= ssl_sock_prepare_ctx(bind_conf, ssl_conf, ctx, err); |
| 4779 | if (!errcode && ckch_inst) |
| 4780 | ckch_inst_add_cafile_link(ckch_inst, bind_conf, ssl_conf, NULL); |
| 4781 | |
| 4782 | return errcode; |
| 4783 | } |
| 4784 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4785 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4786 | { |
| 4787 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4788 | size_t prefixlen, suffixlen; |
| 4789 | |
| 4790 | /* Trivial case */ |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4791 | if (strcasecmp(pattern, hostname) == 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4792 | return 1; |
| 4793 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4794 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4795 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4796 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4797 | pattern_wildcard = NULL; |
| 4798 | pattern_left_label_end = pattern; |
| 4799 | while (*pattern_left_label_end != '.') { |
| 4800 | switch (*pattern_left_label_end) { |
| 4801 | case 0: |
| 4802 | /* End of label not found */ |
| 4803 | return 0; |
| 4804 | case '*': |
| 4805 | /* If there is more than one wildcards */ |
| 4806 | if (pattern_wildcard) |
| 4807 | return 0; |
| 4808 | pattern_wildcard = pattern_left_label_end; |
| 4809 | break; |
| 4810 | } |
| 4811 | pattern_left_label_end++; |
| 4812 | } |
| 4813 | |
| 4814 | /* If it's not trivial and there is no wildcard, it can't |
| 4815 | * match */ |
| 4816 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4817 | return 0; |
| 4818 | |
| 4819 | /* Make sure all labels match except the leftmost */ |
| 4820 | hostname_left_label_end = strchr(hostname, '.'); |
| 4821 | if (!hostname_left_label_end |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4822 | || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4823 | return 0; |
| 4824 | |
| 4825 | /* Make sure the leftmost label of the hostname is long enough |
| 4826 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4827 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4828 | return 0; |
| 4829 | |
| 4830 | /* Finally compare the string on either side of the |
| 4831 | * wildcard */ |
| 4832 | prefixlen = pattern_wildcard - pattern; |
| 4833 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4834 | if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0)) |
| 4835 | || (suffixlen && (strncasecmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4836 | return 0; |
| 4837 | |
| 4838 | return 1; |
| 4839 | } |
| 4840 | |
| 4841 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4842 | { |
| 4843 | SSL *ssl; |
| 4844 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4845 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4846 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4847 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4848 | |
| 4849 | int depth; |
| 4850 | X509 *cert; |
| 4851 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4852 | int i; |
| 4853 | X509_NAME *cert_subject; |
| 4854 | char *str; |
| 4855 | |
| 4856 | if (ok == 0) |
| 4857 | return ok; |
| 4858 | |
| 4859 | 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] | 4860 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 4861 | ssl_ctx = __conn_get_ssl_sock_ctx(conn); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4862 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4863 | /* We're checking if the provided hostnames match the desired one. The |
| 4864 | * desired hostname comes from the SNI we presented if any, or if not |
| 4865 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4866 | * directive. If neither is set, we don't care about the name so the |
| 4867 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4868 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4869 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4870 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4871 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4872 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4873 | if (!servername) |
| 4874 | return ok; |
| 4875 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4876 | |
| 4877 | /* We only need to verify the CN on the actual server cert, |
| 4878 | * not the indirect CAs */ |
| 4879 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4880 | if (depth != 0) |
| 4881 | return ok; |
| 4882 | |
| 4883 | /* At this point, the cert is *not* OK unless we can find a |
| 4884 | * hostname match */ |
| 4885 | ok = 0; |
| 4886 | |
| 4887 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4888 | /* It seems like this might happen if verify peer isn't set */ |
| 4889 | if (!cert) |
| 4890 | return ok; |
| 4891 | |
| 4892 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4893 | if (alt_names) { |
| 4894 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4895 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4896 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4897 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4898 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4899 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4900 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4901 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4902 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4903 | OPENSSL_free(str); |
| 4904 | } |
| 4905 | } |
| 4906 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4907 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4908 | } |
| 4909 | |
| 4910 | cert_subject = X509_get_subject_name(cert); |
| 4911 | i = -1; |
| 4912 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4913 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4914 | ASN1_STRING *value; |
| 4915 | value = X509_NAME_ENTRY_get_data(entry); |
| 4916 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4917 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4918 | OPENSSL_free(str); |
| 4919 | } |
| 4920 | } |
| 4921 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4922 | /* report the mismatch and indicate if SNI was used or not */ |
| 4923 | if (!ok && !conn->err_code) |
| 4924 | 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] | 4925 | return ok; |
| 4926 | } |
| 4927 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4928 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4929 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4930 | { |
| 4931 | int cfgerr = 0; |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 4932 | SSL_CTX *ctx; |
William Lallemand | 0adafb3 | 2022-12-13 18:17:44 +0100 | [diff] [blame] | 4933 | /* Automatic memory computations need to know we use SSL there |
| 4934 | * If this is an internal proxy, don't use it for the computation */ |
William Lallemand | 04007cb | 2022-12-14 10:34:36 +0100 | [diff] [blame] | 4935 | if (!(srv->proxy->cap & PR_CAP_INT)) |
William Lallemand | 0adafb3 | 2022-12-13 18:17:44 +0100 | [diff] [blame] | 4936 | global.ssl_used_backend = 1; |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4937 | |
| 4938 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4939 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4940 | if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 4941 | ha_alert("out of memory.\n"); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4942 | cfgerr++; |
| 4943 | return cfgerr; |
| 4944 | } |
| 4945 | } |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 4946 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4947 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4948 | |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 4949 | if (srv->ssl_ctx.client_crt) { |
| 4950 | const int create_if_none = srv->flags & SRV_F_DYNAMIC ? 0 : 1; |
| 4951 | char *err = NULL; |
| 4952 | int err_code = 0; |
| 4953 | |
| 4954 | /* If there is a crt keyword there, the SSL_CTX will be created here. */ |
| 4955 | err_code = ssl_sock_load_srv_cert(srv->ssl_ctx.client_crt, srv, create_if_none, &err); |
| 4956 | if (err_code != ERR_NONE) { |
| 4957 | if ((err_code & ERR_WARN) && !(err_code & ERR_ALERT)) |
| 4958 | ha_warning("%s", err); |
| 4959 | else |
| 4960 | ha_alert("%s", err); |
| 4961 | |
| 4962 | if (err_code & (ERR_FATAL|ERR_ABORT)) |
| 4963 | cfgerr++; |
| 4964 | } |
| 4965 | ha_free(&err); |
| 4966 | } |
| 4967 | |
| 4968 | ctx = srv->ssl_ctx.ctx; |
| 4969 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4970 | /* The context will be uninitialized if there wasn't any "cert" option |
| 4971 | * in the server line. */ |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4972 | if (!ctx) { |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4973 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 4974 | if (!ctx) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 4975 | ha_alert("unable to allocate ssl context.\n"); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4976 | cfgerr++; |
| 4977 | return cfgerr; |
| 4978 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4979 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4980 | srv->ssl_ctx.ctx = ctx; |
| 4981 | } |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 4982 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4983 | cfgerr += ssl_sock_prep_srv_ctx_and_inst(srv, srv->ssl_ctx.ctx, srv->ssl_ctx.inst); |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 4984 | |
| 4985 | return cfgerr; |
| 4986 | } |
| 4987 | |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 4988 | /* Initialize an SSL context that will be used on the backend side. |
| 4989 | * Returns an error count. |
| 4990 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4991 | static int ssl_sock_prepare_srv_ssl_ctx(const struct server *srv, SSL_CTX *ctx) |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 4992 | { |
| 4993 | struct proxy *curproxy = srv->proxy; |
| 4994 | int cfgerr = 0; |
| 4995 | long options = |
| 4996 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4997 | SSL_OP_NO_SSLv2 | |
| 4998 | SSL_OP_NO_COMPRESSION; |
| 4999 | long mode = |
| 5000 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5001 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 5002 | SSL_MODE_RELEASE_BUFFERS | |
| 5003 | SSL_MODE_SMALL_BUFFERS; |
| 5004 | int verify = SSL_VERIFY_NONE; |
| 5005 | const struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
| 5006 | int i, min, max, hole; |
| 5007 | int flags = MC_SSL_O_ALL; |
| 5008 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5009 | if (conf_ssl_methods->flags && (conf_ssl_methods->min || conf_ssl_methods->max)) |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5010 | ha_warning("no-sslv3/no-tlsv1x are ignored for this server. " |
| 5011 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n"); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5012 | else |
| 5013 | flags = conf_ssl_methods->flags; |
| 5014 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5015 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5016 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5017 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5018 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5019 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5020 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5021 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5022 | min = max = CONF_TLSV_NONE; |
| 5023 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5024 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5025 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5026 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5027 | if (min) { |
| 5028 | if (hole) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 5029 | ha_warning("%s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5030 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5031 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5032 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5033 | hole = 0; |
| 5034 | } |
| 5035 | max = i; |
| 5036 | } |
| 5037 | else { |
| 5038 | min = max = i; |
| 5039 | } |
| 5040 | } |
| 5041 | else { |
| 5042 | if (min) |
| 5043 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5044 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5045 | if (!min) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 5046 | ha_alert("%s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5047 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5048 | cfgerr += 1; |
| 5049 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5050 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5051 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5052 | /* 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] | 5053 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5054 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5055 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5056 | else |
| 5057 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5058 | if (flags & methodVersions[i].flag) |
| 5059 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5060 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5061 | /* 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] | 5062 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5063 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5064 | #endif |
| 5065 | |
| 5066 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5067 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5068 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5069 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5070 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5071 | if (global_ssl.async) |
| 5072 | mode |= SSL_MODE_ASYNC; |
| 5073 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5074 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5075 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5076 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 5077 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5078 | switch (srv->ssl_ctx.verify) { |
| 5079 | case SSL_SOCK_VERIFY_NONE: |
| 5080 | verify = SSL_VERIFY_NONE; |
| 5081 | break; |
| 5082 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5083 | verify = SSL_VERIFY_PEER; |
| 5084 | break; |
| 5085 | } |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5086 | SSL_CTX_set_verify(ctx, verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5087 | (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] | 5088 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5089 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5090 | /* set CAfile to verify */ |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5091 | if (!ssl_set_verify_locations_file(ctx, srv->ssl_ctx.ca_file)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5092 | ha_alert("unable to set CA file '%s'.\n", |
| 5093 | srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5094 | cfgerr++; |
| 5095 | } |
| 5096 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5097 | else { |
| 5098 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5099 | ha_alert("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"); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5100 | else |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5101 | ha_alert("verify is enabled but no CA file specified.\n"); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5102 | cfgerr++; |
| 5103 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5104 | #ifdef X509_V_FLAG_CRL_CHECK |
| 5105 | if (srv->ssl_ctx.crl_file) { |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5106 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5107 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5108 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5109 | ha_alert("unable to configure CRL file '%s'.\n", |
| 5110 | srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5111 | cfgerr++; |
| 5112 | } |
| 5113 | else { |
| 5114 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5115 | } |
| 5116 | } |
| 5117 | #endif |
| 5118 | } |
| 5119 | |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5120 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 5121 | SSL_CTX_sess_set_new_cb(ctx, ssl_sess_new_srv_cb); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5122 | if (srv->ssl_ctx.ciphers && |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5123 | !SSL_CTX_set_cipher_list(ctx, srv->ssl_ctx.ciphers)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5124 | ha_alert("unable to set SSL cipher list to '%s'.\n", |
| 5125 | srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5126 | cfgerr++; |
| 5127 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5128 | |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 5129 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5130 | if (srv->ssl_ctx.ciphersuites && |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5131 | !SSL_CTX_set_ciphersuites(ctx, srv->ssl_ctx.ciphersuites)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5132 | ha_alert("unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 5133 | srv->ssl_ctx.ciphersuites); |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5134 | cfgerr++; |
| 5135 | } |
| 5136 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5137 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 5138 | if (srv->ssl_ctx.npn_str) |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5139 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, (struct server*)srv); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5140 | #endif |
| 5141 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5142 | if (srv->ssl_ctx.alpn_str) |
| 5143 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 5144 | #endif |
| 5145 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5146 | |
| 5147 | return cfgerr; |
| 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * Prepare the frontend's SSL_CTX based on the server line configuration. |
| 5152 | * Since the CA file loading is made depending on the verify option of the |
| 5153 | * server line, the link between the SSL_CTX and the CA file tree entry is |
| 5154 | * made here. |
| 5155 | * If we want to create a link between the CA file entry and the corresponding |
| 5156 | * ckch instance (for CA file hot update), it needs to be done after |
| 5157 | * ssl_sock_prepare_srv_ssl_ctx. |
| 5158 | * Returns an error count. |
| 5159 | */ |
| 5160 | int ssl_sock_prep_srv_ctx_and_inst(const struct server *srv, SSL_CTX *ctx, |
| 5161 | struct ckch_inst *ckch_inst) |
| 5162 | { |
| 5163 | int cfgerr = 0; |
| 5164 | |
| 5165 | cfgerr += ssl_sock_prepare_srv_ssl_ctx(srv, ctx); |
| 5166 | if (!cfgerr && ckch_inst) |
| 5167 | ckch_inst_add_cafile_link(ckch_inst, NULL, NULL, srv); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5168 | |
| 5169 | return cfgerr; |
| 5170 | } |
| 5171 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5172 | |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5173 | /* |
| 5174 | * Create an initial CTX used to start the SSL connections. |
| 5175 | * May be used by QUIC xprt which makes usage of SSL sessions initialized from SSL_CTXs. |
| 5176 | * Returns 0 if succeeded, or something >0 if not. |
| 5177 | */ |
| 5178 | #ifdef USE_QUIC |
| 5179 | static int ssl_initial_ctx(struct bind_conf *bind_conf) |
| 5180 | { |
| 5181 | if (bind_conf->xprt == xprt_get(XPRT_QUIC)) |
| 5182 | return ssl_quic_initial_ctx(bind_conf); |
| 5183 | else |
| 5184 | return ssl_sock_initial_ctx(bind_conf); |
| 5185 | } |
| 5186 | #else |
| 5187 | static int ssl_initial_ctx(struct bind_conf *bind_conf) |
| 5188 | { |
| 5189 | return ssl_sock_initial_ctx(bind_conf); |
| 5190 | } |
| 5191 | #endif |
| 5192 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5193 | /* 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] | 5194 | * be NULL, in which case nothing is done. Returns the number of errors |
| 5195 | * encountered. |
| 5196 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5197 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5198 | { |
| 5199 | struct ebmb_node *node; |
| 5200 | struct sni_ctx *sni; |
| 5201 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5202 | int errcode = 0; |
| 5203 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5204 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5205 | /* Automatic memory computations need to know we use SSL there */ |
| 5206 | global.ssl_used_frontend = 1; |
| 5207 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5208 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 5209 | if (!bind_conf->initial_ctx) { |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5210 | err += ssl_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5211 | /* It should not be necessary to call this function, but it's |
| 5212 | necessary first to check and move all initialisation related |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5213 | to initial_ctx in ssl_initial_ctx. */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5214 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, NULL, bind_conf->initial_ctx, NULL, &errmsg); |
| 5215 | } |
| 5216 | if (bind_conf->default_ctx) { |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 5217 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx, bind_conf->default_inst, &errmsg); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5218 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5219 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5220 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5221 | while (node) { |
| 5222 | sni = ebmb_entry(node, struct sni_ctx, name); |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5223 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5224 | /* only initialize the CTX on its first occurrence and |
| 5225 | if it is not the default_ctx */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5226 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, sni->conf, sni->ctx, sni->ckch_inst, &errmsg); |
| 5227 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5228 | node = ebmb_next(node); |
| 5229 | } |
| 5230 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5231 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5232 | while (node) { |
| 5233 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5234 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5235 | /* only initialize the CTX on its first occurrence and |
| 5236 | if it is not the default_ctx */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5237 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, sni->conf, sni->ctx, sni->ckch_inst, &errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5238 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5239 | node = ebmb_next(node); |
| 5240 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5241 | |
| 5242 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5243 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5244 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5245 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5246 | err++; |
| 5247 | } |
| 5248 | |
| 5249 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5250 | return err; |
| 5251 | } |
| 5252 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5253 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 5254 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 5255 | * alerts are directly emitted since the rest of the stack does it below. |
| 5256 | */ |
| 5257 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 5258 | { |
| 5259 | struct proxy *px = bind_conf->frontend; |
| 5260 | int alloc_ctx; |
| 5261 | int err; |
| 5262 | |
Willy Tarreau | 11ba404 | 2022-05-20 15:56:32 +0200 | [diff] [blame] | 5263 | if (!(bind_conf->options & BC_O_USE_SSL)) { |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5264 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5265 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 5266 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5267 | } |
| 5268 | return 0; |
| 5269 | } |
| 5270 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5271 | if (bind_conf->strict_sni && !(bind_conf->options & BC_O_GENERATE_CERTS)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5272 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 5273 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5274 | } |
| 5275 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5276 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 5277 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5278 | return -1; |
| 5279 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5280 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 5281 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5282 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 5283 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
Willy Tarreau | 9135859 | 2021-06-15 08:08:04 +0200 | [diff] [blame] | 5284 | sizeof(*sh_ssl_sess_tree), (global.nbthread > 1)); |
Frédéric Lécaille | 4c8aa11 | 2018-10-25 20:22:46 +0200 | [diff] [blame] | 5285 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5286 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 5287 | 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"); |
| 5288 | else |
| 5289 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 5290 | return -1; |
| 5291 | } |
| 5292 | /* free block callback */ |
| 5293 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 5294 | /* init the root tree within the extra space */ |
| 5295 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 5296 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5297 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5298 | err = 0; |
| 5299 | /* initialize all certificate contexts */ |
| 5300 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 5301 | |
| 5302 | /* initialize CA variables if the certificates generation is enabled */ |
| 5303 | err += ssl_sock_load_ca(bind_conf); |
| 5304 | |
| 5305 | return -err; |
| 5306 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5307 | |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5308 | /* release ssl context allocated for servers. Most of the field free here |
| 5309 | * must also be allocated in srv_ssl_settings_cpy() */ |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5310 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 5311 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5312 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5313 | ha_free(&srv->ssl_ctx.alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5314 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 5315 | #ifdef OPENSSL_NPN_NEGOTIATED |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5316 | ha_free(&srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 5317 | #endif |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 5318 | if (srv->ssl_ctx.reused_sess) { |
| 5319 | int i; |
| 5320 | |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 5321 | for (i = 0; i < global.nbthread; i++) { |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5322 | ha_free(&srv->ssl_ctx.reused_sess[i].ptr); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 5323 | ha_free(&srv->ssl_ctx.reused_sess[i].sni); |
| 5324 | } |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5325 | ha_free(&srv->ssl_ctx.reused_sess); |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 5326 | } |
| 5327 | |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5328 | if (srv->ssl_ctx.ctx) { |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5329 | SSL_CTX_free(srv->ssl_ctx.ctx); |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5330 | srv->ssl_ctx.ctx = NULL; |
| 5331 | } |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5332 | |
| 5333 | ha_free(&srv->ssl_ctx.ca_file); |
| 5334 | ha_free(&srv->ssl_ctx.crl_file); |
| 5335 | ha_free(&srv->ssl_ctx.client_crt); |
| 5336 | ha_free(&srv->ssl_ctx.verify_host); |
| 5337 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5338 | ha_free(&srv->sni_expr); |
William Lallemand | 43c2ce4 | 2022-03-16 17:48:19 +0100 | [diff] [blame] | 5339 | release_sample_expr(srv->ssl_ctx.sni); |
| 5340 | srv->ssl_ctx.sni = NULL; |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5341 | #endif |
| 5342 | ha_free(&srv->ssl_ctx.ciphers); |
| 5343 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
| 5344 | ha_free(&srv->ssl_ctx.ciphersuites); |
| 5345 | #endif |
William Lallemand | e69563f | 2021-12-30 14:45:19 +0100 | [diff] [blame] | 5346 | /* If there is a certificate we must unlink the ckch instance */ |
| 5347 | ckch_inst_free(srv->ssl_ctx.inst); |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5348 | } |
| 5349 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5350 | /* 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] | 5351 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 5352 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5353 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5354 | { |
| 5355 | struct ebmb_node *node, *back; |
| 5356 | struct sni_ctx *sni; |
| 5357 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5358 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5359 | while (node) { |
| 5360 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5361 | back = ebmb_next(node); |
| 5362 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5363 | SSL_CTX_free(sni->ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5364 | LIST_DELETE(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5365 | free(sni); |
| 5366 | node = back; |
| 5367 | } |
| 5368 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5369 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5370 | while (node) { |
| 5371 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5372 | back = ebmb_next(node); |
| 5373 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5374 | SSL_CTX_free(sni->ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5375 | LIST_DELETE(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5376 | free(sni); |
| 5377 | node = back; |
| 5378 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 5379 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5380 | SSL_CTX_free(bind_conf->initial_ctx); |
| 5381 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5382 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5383 | bind_conf->default_ctx = NULL; |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 5384 | bind_conf->default_inst = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5385 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5386 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 5387 | |
| 5388 | |
| 5389 | void ssl_sock_deinit() |
| 5390 | { |
| 5391 | crtlist_deinit(); /* must be free'd before the ckchs */ |
| 5392 | ckch_deinit(); |
| 5393 | } |
| 5394 | REGISTER_POST_DEINIT(ssl_sock_deinit); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5395 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5396 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5397 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5398 | { |
| 5399 | ssl_sock_free_ca(bind_conf); |
| 5400 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5401 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5402 | free(bind_conf->ca_sign_file); |
| 5403 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5404 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5405 | free(bind_conf->keys_ref->filename); |
| 5406 | free(bind_conf->keys_ref->tlskeys); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5407 | LIST_DELETE(&bind_conf->keys_ref->list); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5408 | free(bind_conf->keys_ref); |
| 5409 | } |
| 5410 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5411 | bind_conf->ca_sign_pass = NULL; |
| 5412 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5413 | } |
| 5414 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5415 | /* Load CA cert file and private key used to generate certificates */ |
| 5416 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5417 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5418 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5419 | struct proxy *px = bind_conf->frontend; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5420 | struct ckch_data *data = NULL; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5421 | int ret = 0; |
| 5422 | char *err = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5423 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5424 | if (!(bind_conf->options & BC_O_GENERATE_CERTS)) |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5425 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5426 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5427 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5428 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5429 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5430 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5431 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5432 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5433 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5434 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5435 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5436 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5437 | "no CA certificate File configured at [%s:%d].\n", |
| 5438 | px->id, bind_conf->file, bind_conf->line); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5439 | goto failed; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5440 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5441 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5442 | /* Allocate cert structure */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5443 | data = calloc(1, sizeof(*data)); |
| 5444 | if (!data) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5445 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain allocation failure\n", |
| 5446 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 5447 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5448 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5449 | |
| 5450 | /* Try to parse file */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5451 | if (ssl_sock_load_files_into_ckch(bind_conf->ca_sign_file, data, &err)) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5452 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain loading failed: %s\n", |
| 5453 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line, err); |
Willy Tarreau | 01acf56 | 2021-02-26 21:12:15 +0100 | [diff] [blame] | 5454 | free(err); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5455 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5456 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5457 | |
| 5458 | /* Fail if missing cert or pkey */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5459 | if ((!data->cert) || (!data->key)) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5460 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain missing certificate or private key\n", |
| 5461 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 5462 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5463 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5464 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5465 | /* Final assignment to bind */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5466 | bind_conf->ca_sign_ckch = data; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5467 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5468 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5469 | failed: |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5470 | if (data) { |
| 5471 | ssl_sock_free_cert_key_and_chain_contents(data); |
| 5472 | free(data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5473 | } |
| 5474 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5475 | bind_conf->options &= ~BC_O_GENERATE_CERTS; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5476 | ret++; |
| 5477 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5478 | } |
| 5479 | |
| 5480 | /* Release CA cert and private key used to generate certificated */ |
| 5481 | void |
| 5482 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5483 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5484 | if (bind_conf->ca_sign_ckch) { |
| 5485 | ssl_sock_free_cert_key_and_chain_contents(bind_conf->ca_sign_ckch); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 5486 | ha_free(&bind_conf->ca_sign_ckch); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5487 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5488 | } |
| 5489 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5490 | /* |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5491 | * Try to allocate the BIO and SSL session objects of <conn> connection with <bio> and |
| 5492 | * <ssl> as addresses, <bio_meth> as BIO method and <ssl_ctx> as SSL context inherited settings. |
| 5493 | * Connect the allocated BIO to the allocated SSL session. Also set <ctx> as address of custom |
| 5494 | * data for the BIO and store <conn> as user data of the SSL session object. |
Ilya Shipitsin | 1e9a666 | 2021-01-05 22:10:46 +0500 | [diff] [blame] | 5495 | * This is the responsibility of the caller to check the validity of all the pointers passed |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5496 | * as parameters to this function. |
| 5497 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <conn> to |
| 5498 | * CO_ER_SSL_NO_MEM. |
| 5499 | */ |
| 5500 | int ssl_bio_and_sess_init(struct connection *conn, SSL_CTX *ssl_ctx, |
| 5501 | SSL **ssl, BIO **bio, BIO_METHOD *bio_meth, void *ctx) |
| 5502 | { |
| 5503 | int retry = 1; |
| 5504 | |
| 5505 | retry: |
| 5506 | /* Alloc a new SSL session. */ |
| 5507 | *ssl = SSL_new(ssl_ctx); |
| 5508 | if (!*ssl) { |
| 5509 | if (!retry--) |
| 5510 | goto err; |
| 5511 | |
| 5512 | pool_gc(NULL); |
| 5513 | goto retry; |
| 5514 | } |
| 5515 | |
| 5516 | *bio = BIO_new(bio_meth); |
| 5517 | if (!*bio) { |
| 5518 | SSL_free(*ssl); |
| 5519 | *ssl = NULL; |
| 5520 | if (!retry--) |
| 5521 | goto err; |
| 5522 | |
| 5523 | pool_gc(NULL); |
| 5524 | goto retry; |
| 5525 | } |
| 5526 | |
| 5527 | BIO_set_data(*bio, ctx); |
| 5528 | SSL_set_bio(*ssl, *bio, *bio); |
| 5529 | |
| 5530 | /* set connection pointer. */ |
| 5531 | if (!SSL_set_ex_data(*ssl, ssl_app_data_index, conn)) { |
| 5532 | SSL_free(*ssl); |
| 5533 | *ssl = NULL; |
| 5534 | if (!retry--) |
| 5535 | goto err; |
| 5536 | |
| 5537 | pool_gc(NULL); |
| 5538 | goto retry; |
| 5539 | } |
| 5540 | |
| 5541 | return 0; |
| 5542 | |
| 5543 | err: |
| 5544 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5545 | return -1; |
| 5546 | } |
| 5547 | |
Olivier Houchard | bc5ce92 | 2021-03-05 23:47:00 +0100 | [diff] [blame] | 5548 | /* This function is called when all the XPRT have been initialized. We can |
| 5549 | * now attempt to start the SSL handshake. |
| 5550 | */ |
| 5551 | static int ssl_sock_start(struct connection *conn, void *xprt_ctx) |
| 5552 | { |
| 5553 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5554 | |
| 5555 | if (ctx->xprt->start) { |
| 5556 | int ret; |
| 5557 | |
| 5558 | ret = ctx->xprt->start(conn, ctx->xprt_ctx); |
| 5559 | if (ret < 0) |
| 5560 | return ret; |
| 5561 | } |
| 5562 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 5563 | |
| 5564 | return 0; |
| 5565 | } |
| 5566 | |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5567 | /* |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5568 | * This function is called if SSL * context is not yet allocated. The function |
| 5569 | * is designed to be called before any other data-layer operation and sets the |
| 5570 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5571 | * It returns 0 on success and -1 in error case. |
| 5572 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5573 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5574 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5575 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5576 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5577 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5578 | return 0; |
| 5579 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5580 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 5581 | if (!ctx) { |
| 5582 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5583 | return -1; |
| 5584 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5585 | ctx->wait_event.tasklet = tasklet_new(); |
| 5586 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5587 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5588 | pool_free(ssl_sock_ctx_pool, ctx); |
| 5589 | return -1; |
| 5590 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5591 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 5592 | ctx->wait_event.tasklet->context = ctx; |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5593 | 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] | 5594 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5595 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5596 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5597 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5598 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 5599 | ctx->xprt_st = 0; |
| 5600 | ctx->xprt_ctx = NULL; |
Remi Tricot-Le Breton | 1fe0fad | 2021-09-29 18:56:52 +0200 | [diff] [blame] | 5601 | ctx->error_code = 0; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5602 | |
| 5603 | /* Only work with sockets for now, this should be adapted when we'll |
| 5604 | * add QUIC support. |
| 5605 | */ |
| 5606 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5607 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5608 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 5609 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5610 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5611 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5612 | if (global.maxsslconn && global.sslconns >= global.maxsslconn) { |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5613 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5614 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5615 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5616 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5617 | /* If it is in client mode initiate SSL session |
| 5618 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5619 | if (objt_server(conn->target)) { |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5620 | if (ssl_bio_and_sess_init(conn, __objt_server(conn->target)->ssl_ctx.ctx, |
| 5621 | &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5622 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5623 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5624 | SSL_set_connect_state(ctx->ssl); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 5625 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &(__objt_server(conn->target)->ssl_ctx.lock)); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5626 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5627 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 5628 | 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] | 5629 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5630 | SSL_SESSION_free(sess); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 5631 | ha_free(&__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5632 | } else if (sess) { |
| 5633 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5634 | } |
| 5635 | } |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 5636 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &(__objt_server(conn->target)->ssl_ctx.lock)); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5637 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5638 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5639 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5640 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5641 | _HA_ATOMIC_INC(&global.sslconns); |
| 5642 | _HA_ATOMIC_INC(&global.totalsslconns); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5643 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5644 | return 0; |
| 5645 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5646 | else if (objt_listener(conn->target)) { |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5647 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5648 | |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5649 | if (ssl_bio_and_sess_init(conn, bc->initial_ctx, |
| 5650 | &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5651 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5652 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5653 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5654 | if (bc->ssl_conf.early_data) { |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 5655 | b_alloc(&ctx->early_buf); |
| 5656 | SSL_set_max_early_data(ctx->ssl, |
| 5657 | /* Only allow early data if we managed to allocate |
| 5658 | * a buffer. |
| 5659 | */ |
| 5660 | (!b_is_null(&ctx->early_buf)) ? |
| 5661 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 5662 | } |
| 5663 | #endif |
| 5664 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5665 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5666 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5667 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5668 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5669 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Willy Tarreau | a84986a | 2021-02-03 11:21:38 +0100 | [diff] [blame] | 5670 | if (bc->ssl_conf.early_data) |
| 5671 | conn->flags |= CO_FL_EARLY_SSL_HS; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5672 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5673 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5674 | _HA_ATOMIC_INC(&global.sslconns); |
| 5675 | _HA_ATOMIC_INC(&global.totalsslconns); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5676 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5677 | return 0; |
| 5678 | } |
| 5679 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5680 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5681 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5682 | if (ctx && ctx->wait_event.tasklet) |
| 5683 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5684 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5685 | return -1; |
| 5686 | } |
| 5687 | |
| 5688 | |
| 5689 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5690 | * updates the FD status if it wants some polling before being called again. |
| 5691 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5692 | * otherwise it returns non-zero and removes itself from the connection's |
| 5693 | * flags (the bit is provided in <flag> by the caller). |
| 5694 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5695 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5696 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 5697 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5698 | int ret; |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 5699 | struct ssl_counters *counters = NULL; |
| 5700 | struct ssl_counters *counters_px = NULL; |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 5701 | struct listener *li; |
| 5702 | struct server *srv; |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5703 | socklen_t lskerr; |
| 5704 | int skerr; |
| 5705 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5706 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5707 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5708 | return 0; |
| 5709 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5710 | /* get counters */ |
| 5711 | switch (obj_type(conn->target)) { |
| 5712 | case OBJ_TYPE_LISTENER: |
Amaury Denoyelle | 2bf5d41 | 2021-07-26 09:59:06 +0200 | [diff] [blame] | 5713 | li = __objt_listener(conn->target); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5714 | counters = EXTRA_COUNTERS_GET(li->extra_counters, &ssl_stats_module); |
| 5715 | counters_px = EXTRA_COUNTERS_GET(li->bind_conf->frontend->extra_counters_fe, |
| 5716 | &ssl_stats_module); |
| 5717 | break; |
| 5718 | |
| 5719 | case OBJ_TYPE_SERVER: |
Amaury Denoyelle | 2bf5d41 | 2021-07-26 09:59:06 +0200 | [diff] [blame] | 5720 | srv = __objt_server(conn->target); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5721 | counters = EXTRA_COUNTERS_GET(srv->extra_counters, &ssl_stats_module); |
| 5722 | counters_px = EXTRA_COUNTERS_GET(srv->proxy->extra_counters_be, |
| 5723 | &ssl_stats_module); |
| 5724 | break; |
| 5725 | |
| 5726 | default: |
| 5727 | break; |
| 5728 | } |
| 5729 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 5730 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5731 | goto out_error; |
| 5732 | |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5733 | /* don't start calculating a handshake on a dead connection */ |
| 5734 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) |
| 5735 | goto out_error; |
| 5736 | |
| 5737 | /* FIXME/WT: for now we don't have a clear way to inspect the connection |
| 5738 | * status from the lower layers, so let's check the FD directly. Ideally |
| 5739 | * the xprt layers should provide some status indicating their knowledge |
| 5740 | * of shutdowns or error. |
| 5741 | */ |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 5742 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 5743 | |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5744 | skerr = 0; |
| 5745 | lskerr = sizeof(skerr); |
| 5746 | if ((getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) < 0) || |
| 5747 | skerr != 0) |
| 5748 | goto out_error; |
| 5749 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5750 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5751 | /* |
| 5752 | * Check if we have early data. If we do, we have to read them |
| 5753 | * before SSL_do_handshake() is called, And there's no way to |
| 5754 | * detect early data, except to try to read them |
| 5755 | */ |
| 5756 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5757 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5758 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5759 | while (1) { |
| 5760 | ret = SSL_read_early_data(ctx->ssl, |
| 5761 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 5762 | &read_data); |
| 5763 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5764 | goto check_error; |
| 5765 | if (read_data > 0) { |
| 5766 | conn->flags |= CO_FL_EARLY_DATA; |
| 5767 | b_add(&ctx->early_buf, read_data); |
| 5768 | } |
| 5769 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5770 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5771 | if (!b_data(&ctx->early_buf)) |
| 5772 | b_free(&ctx->early_buf); |
| 5773 | break; |
| 5774 | } |
| 5775 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5776 | } |
| 5777 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5778 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5779 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5780 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5781 | * the reneg handshake. |
| 5782 | * Here we use SSL_peek as a workaround for reneg. |
| 5783 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5784 | 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] | 5785 | char c; |
| 5786 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5787 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5788 | if (ret <= 0) { |
| 5789 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5790 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5791 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5792 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5793 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5794 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5795 | 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] | 5796 | return 0; |
| 5797 | } |
| 5798 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5799 | /* handshake may have been completed but we have |
| 5800 | * no more data to read. |
| 5801 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5802 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5803 | ret = 1; |
| 5804 | goto reneg_ok; |
| 5805 | } |
| 5806 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5807 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5808 | 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] | 5809 | return 0; |
| 5810 | } |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5811 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5812 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5813 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5814 | return 0; |
| 5815 | } |
| 5816 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5817 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5818 | /* if errno is null, then connection was successfully established */ |
| 5819 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5820 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5821 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5822 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5823 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5824 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5825 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5826 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5827 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5828 | /* 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] | 5829 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5830 | empty_handshake = state == TLS_ST_BEFORE; |
| 5831 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5832 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5833 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5834 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5835 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5836 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5837 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5838 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5839 | else |
| 5840 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5841 | } |
| 5842 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5843 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5844 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5845 | else |
| 5846 | conn->err_code = CO_ER_SSL_ABORT; |
| 5847 | } |
| 5848 | } |
| 5849 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5850 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5851 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5852 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5853 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5854 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5855 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5856 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5857 | goto out_error; |
| 5858 | } |
| 5859 | else { |
| 5860 | /* Fail on all other handshake errors */ |
| 5861 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5862 | * buffer, causing an RST to be emitted upon close() on |
| 5863 | * TCP sockets. We first try to drain possibly pending |
| 5864 | * data to avoid this as much as possible. |
| 5865 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 5866 | conn_ctrl_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5867 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5868 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5869 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5870 | goto out_error; |
| 5871 | } |
| 5872 | } |
| 5873 | /* read some data: consider handshake completed */ |
| 5874 | goto reneg_ok; |
| 5875 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5876 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5877 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5878 | if (ret != 1) { |
| 5879 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5880 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5881 | |
Remi Tricot-Le Breton | 1fe0fad | 2021-09-29 18:56:52 +0200 | [diff] [blame] | 5882 | if (!ctx->error_code) |
| 5883 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 7c6898e | 2021-07-29 09:45:51 +0200 | [diff] [blame] | 5884 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5885 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5886 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5887 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5888 | 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] | 5889 | return 0; |
| 5890 | } |
| 5891 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5892 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5893 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5894 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5895 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5896 | return 0; |
| 5897 | } |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5898 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5899 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5900 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5901 | return 0; |
| 5902 | } |
| 5903 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5904 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5905 | /* if errno is null, then connection was successfully established */ |
| 5906 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5907 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5908 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5909 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5910 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5911 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5912 | #else |
| 5913 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5914 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5915 | /* 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] | 5916 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5917 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5918 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5919 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5920 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5921 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5922 | if (empty_handshake) { |
| 5923 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5924 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5925 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5926 | else |
| 5927 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5928 | } |
| 5929 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5930 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5931 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5932 | else |
| 5933 | conn->err_code = CO_ER_SSL_ABORT; |
| 5934 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5935 | } |
| 5936 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5937 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5938 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5939 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5940 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5941 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5942 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5943 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5944 | goto out_error; |
| 5945 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5946 | else { |
| 5947 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5948 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5949 | * buffer, causing an RST to be emitted upon close() on |
| 5950 | * TCP sockets. We first try to drain possibly pending |
| 5951 | * data to avoid this as much as possible. |
| 5952 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 5953 | conn_ctrl_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5954 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5955 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5956 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5957 | goto out_error; |
| 5958 | } |
| 5959 | } |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5960 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5961 | else { |
| 5962 | /* |
| 5963 | * If the server refused the early data, we have to send a |
| 5964 | * 425 to the client, as we no longer have the data to sent |
| 5965 | * them again. |
| 5966 | */ |
| 5967 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5968 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5969 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5970 | goto out_error; |
| 5971 | } |
| 5972 | } |
| 5973 | } |
| 5974 | #endif |
| 5975 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5976 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5977 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5978 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5979 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5980 | /* ASYNC engine API doesn't support moving read/write |
| 5981 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5982 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5983 | */ |
| 5984 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5985 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5986 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5987 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5988 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5989 | if (objt_server(conn->target)) { |
| 5990 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5991 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5992 | 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] | 5993 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5994 | else { |
| 5995 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5996 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5997 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5998 | } |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 5999 | |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6000 | if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6001 | HA_ATOMIC_INC(&counters->sess); |
| 6002 | HA_ATOMIC_INC(&counters_px->sess); |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6003 | } |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 6004 | } |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6005 | else if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6006 | HA_ATOMIC_INC(&counters->reused_sess); |
| 6007 | HA_ATOMIC_INC(&counters_px->reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6008 | } |
| 6009 | |
| 6010 | /* The connection is now established at both layers, it's time to leave */ |
| 6011 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6012 | return 1; |
| 6013 | |
| 6014 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6015 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6016 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6017 | ERR_clear_error(); |
| 6018 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6019 | /* free resumed session if exists */ |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6020 | if (objt_server(conn->target)) { |
| 6021 | struct server *s = __objt_server(conn->target); |
| 6022 | /* RWLOCK: only rdlock the SSL cache even when writing in it because there is |
| 6023 | * one cache per thread, it only prevents to flush it from the CLI in |
| 6024 | * another thread */ |
| 6025 | |
| 6026 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 6027 | if (s->ssl_ctx.reused_sess[tid].ptr) |
| 6028 | ha_free(&s->ssl_ctx.reused_sess[tid].ptr); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6029 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6030 | } |
| 6031 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6032 | if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6033 | HA_ATOMIC_INC(&counters->failed_handshake); |
| 6034 | HA_ATOMIC_INC(&counters_px->failed_handshake); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6035 | } |
| 6036 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6037 | /* Fail on all other handshake errors */ |
| 6038 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6039 | if (!conn->err_code) |
| 6040 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6041 | return 0; |
| 6042 | } |
| 6043 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6044 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6045 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6046 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6047 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6048 | * unless the transport layer was already released. |
| 6049 | */ |
| 6050 | 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] | 6051 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6052 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6053 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6054 | if (!ctx) |
| 6055 | return -1; |
| 6056 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6057 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6058 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6059 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6060 | ctx->subs = es; |
| 6061 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6062 | |
| 6063 | /* we may have to subscribe to lower layers for new events */ |
| 6064 | event_type &= ~ctx->wait_event.events; |
| 6065 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6066 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6067 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6068 | } |
| 6069 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6070 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6071 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6072 | * subscribe() call. It always returns zero. |
| 6073 | */ |
| 6074 | 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] | 6075 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6076 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6077 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6078 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6079 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6080 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6081 | es->events &= ~event_type; |
| 6082 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6083 | ctx->subs = NULL; |
| 6084 | |
| 6085 | /* If we subscribed, and we're not doing the handshake, |
| 6086 | * then we subscribed because the upper layer asked for it, |
| 6087 | * as the upper layer is no longer interested, we can |
| 6088 | * unsubscribe too. |
| 6089 | */ |
| 6090 | event_type &= ctx->wait_event.events; |
| 6091 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6092 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6093 | |
| 6094 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6095 | } |
| 6096 | |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6097 | /* The connection has been taken over, so destroy the old tasklet and create |
| 6098 | * a new one. The original thread ID must be passed into orig_tid |
| 6099 | * It should be called with the takeover lock for the old thread held. |
| 6100 | * Returns 0 on success, and -1 on failure |
| 6101 | */ |
| 6102 | static int ssl_takeover(struct connection *conn, void *xprt_ctx, int orig_tid) |
| 6103 | { |
| 6104 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6105 | struct tasklet *tl = tasklet_new(); |
| 6106 | |
| 6107 | if (!tl) |
| 6108 | return -1; |
| 6109 | |
| 6110 | ctx->wait_event.tasklet->context = NULL; |
| 6111 | tasklet_wakeup_on(ctx->wait_event.tasklet, orig_tid); |
| 6112 | ctx->wait_event.tasklet = tl; |
| 6113 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6114 | ctx->wait_event.tasklet->context = ctx; |
| 6115 | return 0; |
| 6116 | } |
| 6117 | |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6118 | /* notify the next xprt that the connection is about to become idle and that it |
| 6119 | * may be stolen at any time after the function returns and that any tasklet in |
| 6120 | * the chain must be careful before dereferencing its context. |
| 6121 | */ |
| 6122 | static void ssl_set_idle(struct connection *conn, void *xprt_ctx) |
| 6123 | { |
| 6124 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6125 | |
| 6126 | if (!ctx || !ctx->wait_event.tasklet) |
| 6127 | return; |
| 6128 | |
| 6129 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 6130 | if (ctx->xprt) |
| 6131 | xprt_set_idle(conn, ctx->xprt, ctx->xprt_ctx); |
| 6132 | } |
| 6133 | |
| 6134 | /* notify the next xprt that the connection is not idle anymore and that it may |
| 6135 | * not be stolen before the next xprt_set_idle(). |
| 6136 | */ |
| 6137 | static void ssl_set_used(struct connection *conn, void *xprt_ctx) |
| 6138 | { |
| 6139 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6140 | |
| 6141 | if (!ctx || !ctx->wait_event.tasklet) |
| 6142 | return; |
| 6143 | |
| 6144 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 6145 | if (ctx->xprt) |
| 6146 | xprt_set_used(conn, ctx->xprt, ctx->xprt_ctx); |
| 6147 | } |
| 6148 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6149 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6150 | * Returns 0 on success, and non-zero on failure. |
| 6151 | */ |
| 6152 | 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) |
| 6153 | { |
| 6154 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6155 | |
| 6156 | if (oldxprt_ops != NULL) |
| 6157 | *oldxprt_ops = ctx->xprt; |
| 6158 | if (oldxprt_ctx != NULL) |
| 6159 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6160 | ctx->xprt = toadd_ops; |
| 6161 | ctx->xprt_ctx = toadd_ctx; |
| 6162 | return 0; |
| 6163 | } |
| 6164 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6165 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6166 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6167 | * XPRT. |
| 6168 | */ |
| 6169 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6170 | { |
| 6171 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6172 | |
| 6173 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6174 | ctx->xprt_ctx = newctx; |
| 6175 | ctx->xprt = newops; |
| 6176 | return 0; |
| 6177 | } |
| 6178 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6179 | } |
| 6180 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6181 | struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned int state) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6182 | { |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6183 | struct tasklet *tl = (struct tasklet *)t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6184 | struct ssl_sock_ctx *ctx = context; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6185 | struct connection *conn; |
| 6186 | int conn_in_list; |
| 6187 | int ret = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6188 | |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6189 | if (state & TASK_F_USR1) { |
| 6190 | /* the tasklet was idling on an idle connection, it might have |
| 6191 | * been stolen, let's be careful! |
| 6192 | */ |
| 6193 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6194 | if (tl->context == NULL) { |
| 6195 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6196 | tasklet_free(tl); |
| 6197 | return NULL; |
| 6198 | } |
| 6199 | conn = ctx->conn; |
| 6200 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 6201 | if (conn_in_list) |
| 6202 | conn_delete_from_tree(&conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6203 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6204 | } else { |
| 6205 | conn = ctx->conn; |
| 6206 | conn_in_list = 0; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6207 | } |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6208 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6209 | /* First if we're doing an handshake, try that */ |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6210 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6211 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6212 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6213 | /* handshake completed, leave the bulk queue */ |
Willy Tarreau | 4c48edb | 2021-03-09 17:58:02 +0100 | [diff] [blame] | 6214 | _HA_ATOMIC_AND(&tl->state, ~TASK_HEAVY); |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6215 | } |
| 6216 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6217 | /* If we had an error, or the handshake is done and I/O is available, |
| 6218 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6219 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6220 | * we can't be sure conn_fd_handler() will be called again. |
| 6221 | */ |
| 6222 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6223 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6224 | int woke = 0; |
| 6225 | |
| 6226 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6227 | if (ctx->subs) { |
| 6228 | tasklet_wakeup(ctx->subs->tasklet); |
| 6229 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6230 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6231 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6232 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6233 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6234 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6235 | * upper layers know. If we have no mux, create it, |
| 6236 | * and once we have a mux, call its wake method if we didn't |
| 6237 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6238 | */ |
| 6239 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6240 | if (!ctx->conn->mux) |
| 6241 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6242 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6243 | ret = ctx->conn->mux->wake(ctx->conn); |
| 6244 | goto leave; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6245 | } |
| 6246 | } |
Ilya Shipitsin | 761d64c | 2021-01-07 11:59:58 +0500 | [diff] [blame] | 6247 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6248 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6249 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6250 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6251 | tasklet_wakeup(ctx->subs->tasklet); |
| 6252 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6253 | if (!ctx->subs->events) |
| 6254 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6255 | } |
| 6256 | #endif |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6257 | leave: |
| 6258 | if (!ret && conn_in_list) { |
| 6259 | struct server *srv = objt_server(conn->target); |
| 6260 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6261 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6262 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6263 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6264 | else |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6265 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6266 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6267 | } |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 6268 | return t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6269 | } |
| 6270 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6271 | /* 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] | 6272 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6273 | * buffer wraps, in which case a second call may be performed. The connection's |
| 6274 | * flags are updated with whatever special event is detected (error, read0, |
| 6275 | * empty). The caller is responsible for taking care of those events and |
| 6276 | * avoiding the call if inappropriate. The function does not call the |
| 6277 | * connection's polling update function, so the caller is responsible for this. |
| 6278 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6279 | 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] | 6280 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6281 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 6282 | ssize_t ret; |
| 6283 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6284 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6285 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6286 | goto out_error; |
| 6287 | |
Ilya Shipitsin | 761d64c | 2021-01-07 11:59:58 +0500 | [diff] [blame] | 6288 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6289 | if (b_data(&ctx->early_buf)) { |
| 6290 | try = b_contig_space(buf); |
| 6291 | if (try > b_data(&ctx->early_buf)) |
| 6292 | try = b_data(&ctx->early_buf); |
| 6293 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 6294 | b_add(buf, try); |
| 6295 | b_del(&ctx->early_buf, try); |
| 6296 | if (b_data(&ctx->early_buf) == 0) |
| 6297 | b_free(&ctx->early_buf); |
| 6298 | return try; |
| 6299 | } |
| 6300 | #endif |
| 6301 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6302 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6303 | /* a handshake was requested */ |
| 6304 | return 0; |
| 6305 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6306 | /* read the largest possible block. For this, we perform only one call |
| 6307 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 6308 | * in which case we accept to do it once again. A new attempt is made on |
| 6309 | * EINTR too. |
| 6310 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 6311 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6312 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6313 | try = b_contig_space(buf); |
| 6314 | if (!try) |
| 6315 | break; |
| 6316 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6317 | if (try > count) |
| 6318 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6319 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6320 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6321 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6322 | if (conn->flags & CO_FL_ERROR) { |
| 6323 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6324 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6325 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6326 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6327 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6328 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6329 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6330 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6331 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6332 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6333 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6334 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6335 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6336 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6337 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6338 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6339 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6340 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6341 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6342 | break; |
| 6343 | } |
| 6344 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6345 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6346 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6347 | SUB_RETRY_RECV, |
| 6348 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6349 | /* handshake is running, and it may need to re-enable read */ |
| 6350 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6351 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6352 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6353 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6354 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6355 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6356 | break; |
| 6357 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6358 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6359 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 6360 | goto read0; |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6361 | else if (ret == SSL_ERROR_SSL) { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6362 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
| 6363 | if (ctx && !ctx->error_code) |
Remi Tricot-Le Breton | 9543d5a | 2021-09-29 18:56:53 +0200 | [diff] [blame] | 6364 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6365 | conn->err_code = CO_ERR_SSL_FATAL; |
| 6366 | } |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6367 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 6368 | * stack before shutting down the connection for |
| 6369 | * reading. */ |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 6370 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN || errno == EWOULDBLOCK)) |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6371 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6372 | /* otherwise it's a real error */ |
| 6373 | goto out_error; |
| 6374 | } |
| 6375 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6376 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6377 | return done; |
| 6378 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6379 | clear_ssl_error: |
| 6380 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6381 | ssl_sock_dump_errors(conn, NULL); |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6382 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6383 | read0: |
| 6384 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6385 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6386 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6387 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6388 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6389 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6390 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6391 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6392 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6393 | } |
| 6394 | |
| 6395 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6396 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 6397 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 6398 | * 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] | 6399 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 6400 | * a second call may be performed. The connection's flags are updated with |
| 6401 | * whatever special event is detected (error, empty). The caller is responsible |
| 6402 | * for taking care of those events and avoiding the call if inappropriate. The |
| 6403 | * 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] | 6404 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 6405 | * caller to take care of this. It's up to the caller to update the buffer's |
| 6406 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6407 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6408 | 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] | 6409 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6410 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6411 | ssize_t ret; |
| 6412 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6413 | |
| 6414 | done = 0; |
| 6415 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6416 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6417 | goto out_error; |
| 6418 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6419 | 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] | 6420 | /* a handshake was requested */ |
| 6421 | return 0; |
| 6422 | |
| 6423 | /* send the largest possible block. For this we perform only one call |
| 6424 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 6425 | * in which case we accept to do it once again. |
| 6426 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6427 | while (count) { |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6428 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6429 | size_t written_data; |
| 6430 | #endif |
| 6431 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6432 | try = b_contig_data(buf, done); |
| 6433 | if (try > count) |
| 6434 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6435 | |
Thomas Prückl | 1024393 | 2022-04-27 13:04:54 +0200 | [diff] [blame] | 6436 | if (global_ssl.hard_max_record && try > global_ssl.hard_max_record) |
| 6437 | try = global_ssl.hard_max_record; |
| 6438 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 6439 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6440 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6441 | global_ssl.max_record && try > global_ssl.max_record) { |
| 6442 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6443 | } |
| 6444 | else { |
| 6445 | /* we need to keep the information about the fact that |
| 6446 | * we're not limiting the upcoming send(), because if it |
| 6447 | * fails, we'll have to retry with at least as many data. |
| 6448 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6449 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6450 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6451 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6452 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6453 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6454 | unsigned int max_early; |
| 6455 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6456 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6457 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6458 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6459 | if (SSL_get0_session(ctx->ssl)) |
| 6460 | 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] | 6461 | else |
| 6462 | max_early = 0; |
| 6463 | } |
| 6464 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6465 | if (try + ctx->sent_early_data > max_early) { |
| 6466 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6467 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6468 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6469 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6470 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6471 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6472 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6473 | 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] | 6474 | if (ret == 1) { |
| 6475 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6476 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6477 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6478 | 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] | 6479 | /* Initiate the handshake, now */ |
| 6480 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 6481 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6482 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6483 | } |
| 6484 | |
| 6485 | } else |
| 6486 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6487 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6488 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6489 | if (conn->flags & CO_FL_ERROR) { |
| 6490 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6491 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6492 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6493 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6494 | /* A send succeeded, so we can consider ourself connected */ |
| 6495 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6496 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6497 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6498 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6499 | } |
| 6500 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6501 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6502 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6503 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6504 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6505 | /* handshake is running, and it may need to re-enable write */ |
| 6506 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6507 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6508 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6509 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6510 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6511 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6512 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6513 | break; |
| 6514 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6515 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6516 | break; |
| 6517 | } |
| 6518 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6519 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6520 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6521 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6522 | SUB_RETRY_RECV, |
| 6523 | &ctx->wait_event); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6524 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6525 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6526 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6527 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6528 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6529 | break; |
| 6530 | } |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6531 | else if (ret == SSL_ERROR_SSL || ret == SSL_ERROR_SYSCALL) { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6532 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
| 6533 | |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 6534 | if (ctx && !ctx->error_code) |
Remi Tricot-Le Breton | 9543d5a | 2021-09-29 18:56:53 +0200 | [diff] [blame] | 6535 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6536 | conn->err_code = CO_ERR_SSL_FATAL; |
| 6537 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6538 | goto out_error; |
| 6539 | } |
| 6540 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6541 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6542 | return done; |
| 6543 | |
| 6544 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6545 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6546 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6547 | ERR_clear_error(); |
| 6548 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6549 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6550 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6551 | } |
| 6552 | |
Willy Tarreau | 832e242 | 2021-05-13 10:11:03 +0200 | [diff] [blame] | 6553 | void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6554 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6555 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6556 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6557 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6558 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6559 | if (ctx->wait_event.events != 0) |
| 6560 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 6561 | ctx->wait_event.events, |
| 6562 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6563 | if (ctx->subs) { |
| 6564 | ctx->subs->events = 0; |
| 6565 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6566 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6567 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 6568 | if (ctx->xprt->close) |
| 6569 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6570 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6571 | if (global_ssl.async) { |
| 6572 | OSSL_ASYNC_FD all_fd[32], afd; |
| 6573 | size_t num_all_fds = 0; |
| 6574 | int i; |
| 6575 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6576 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6577 | if (num_all_fds > 32) { |
| 6578 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 6579 | return; |
| 6580 | } |
| 6581 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6582 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6583 | |
| 6584 | /* If an async job is pending, we must try to |
| 6585 | to catch the end using polling before calling |
| 6586 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6587 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6588 | for (i=0 ; i < num_all_fds ; i++) { |
| 6589 | /* switch on an handler designed to |
| 6590 | * handle the SSL_free |
| 6591 | */ |
| 6592 | afd = all_fd[i]; |
| 6593 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6594 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6595 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 6596 | /* To ensure that the fd cache won't be used |
| 6597 | * and we'll catch a real RD event. |
| 6598 | */ |
| 6599 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6600 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6601 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6602 | pool_free(ssl_sock_ctx_pool, ctx); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 6603 | _HA_ATOMIC_INC(&jobs); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6604 | return; |
| 6605 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6606 | /* Else we can remove the fds from the fdtab |
| 6607 | * and call SSL_free. |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 6608 | * note: we do a fd_stop_both and not a delete |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6609 | * because the fd is owned by the engine. |
| 6610 | * the engine is responsible to close |
| 6611 | */ |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 6612 | for (i=0 ; i < num_all_fds ; i++) { |
| 6613 | /* We want to remove the fd from the fdtab |
| 6614 | * but we flag it to disown because the |
| 6615 | * close is performed by the engine itself |
| 6616 | */ |
| 6617 | fdtab[all_fd[i]].state |= FD_DISOWN; |
| 6618 | fd_delete(all_fd[i]); |
| 6619 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6620 | } |
| 6621 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6622 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6623 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6624 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6625 | pool_free(ssl_sock_ctx_pool, ctx); |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 6626 | _HA_ATOMIC_DEC(&global.sslconns); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6627 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6628 | } |
| 6629 | |
| 6630 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 6631 | * any case, flags the connection as reusable if no handshake was in progress. |
| 6632 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6633 | 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] | 6634 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6635 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6636 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6637 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6638 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 6639 | if (!clean) |
| 6640 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6641 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6642 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6643 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6644 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6645 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6646 | ERR_clear_error(); |
| 6647 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6648 | } |
| 6649 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6650 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 6651 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6652 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 6653 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6654 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6655 | X509 *crt; |
| 6656 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6657 | if (!ctx) |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6658 | return 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6659 | crt = SSL_get_certificate(ctx->ssl); |
| 6660 | if (!crt) |
| 6661 | return 0; |
| 6662 | |
| 6663 | return cert_get_pkey_algo(crt, out); |
| 6664 | } |
| 6665 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6666 | /* used for ppv2 cert signature (can be used for logging) */ |
| 6667 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 6668 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6669 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6670 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6671 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 6672 | X509 *crt; |
| 6673 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6674 | if (!ctx) |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6675 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6676 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6677 | if (!crt) |
| 6678 | return NULL; |
| 6679 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6680 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6681 | } |
| 6682 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6683 | /* used for ppv2 authority */ |
| 6684 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6685 | { |
| 6686 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6687 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6688 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6689 | if (!ctx) |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6690 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6691 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6692 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6693 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6694 | #endif |
| 6695 | } |
| 6696 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6697 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6698 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6699 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6700 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6701 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6702 | if (!ctx) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6703 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6704 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6705 | } |
| 6706 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6707 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6708 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 6709 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6710 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6711 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6712 | if (!ctx) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6713 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6714 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6715 | } |
| 6716 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6717 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6718 | { |
| 6719 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6720 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6721 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6722 | if (!ctx) |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 6723 | return; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6724 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6725 | #endif |
| 6726 | } |
| 6727 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6728 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6729 | * to disable SNI. |
| 6730 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6731 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6732 | { |
| 6733 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6734 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6735 | struct server *s; |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6736 | char *prev_name; |
| 6737 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6738 | if (!ctx) |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6739 | return; |
Willy Tarreau | 77bfa66 | 2021-12-23 11:12:13 +0100 | [diff] [blame] | 6740 | |
| 6741 | BUG_ON(!(conn->flags & CO_FL_WAIT_L6_CONN)); |
| 6742 | BUG_ON(!(conn->flags & CO_FL_SSL_WAIT_HS)); |
| 6743 | |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6744 | s = __objt_server(conn->target); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6745 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6746 | /* if the SNI changes, we must destroy the reusable context so that a |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6747 | * new connection will present a new SNI. compare with the SNI |
| 6748 | * previously stored in the reused_sess */ |
| 6749 | /* the RWLOCK is used to ensure that we are not trying to flush the |
| 6750 | * cache from the CLI */ |
| 6751 | |
| 6752 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
| 6753 | prev_name = s->ssl_ctx.reused_sess[tid].sni; |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6754 | if ((!prev_name && hostname) || |
| 6755 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6756 | SSL_set_session(ctx->ssl, NULL); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6757 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6758 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6759 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6760 | #endif |
| 6761 | } |
| 6762 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6763 | /* Extract peer certificate's common name into the chunk dest |
| 6764 | * Returns |
| 6765 | * the len of the extracted common name |
| 6766 | * or 0 if no CN found in DN |
| 6767 | * or -1 on error case (i.e. no peer certificate) |
| 6768 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6769 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6770 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6771 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6772 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6773 | X509 *crt = NULL; |
| 6774 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6775 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6776 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6777 | .area = (char *)&find_cn, |
| 6778 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6779 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6780 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6781 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6782 | if (!ctx) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6783 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6784 | |
| 6785 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6786 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6787 | if (!crt) |
| 6788 | goto out; |
| 6789 | |
| 6790 | name = X509_get_subject_name(crt); |
| 6791 | if (!name) |
| 6792 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6793 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6794 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6795 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6796 | if (crt) |
| 6797 | X509_free(crt); |
| 6798 | |
| 6799 | return result; |
| 6800 | } |
| 6801 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6802 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6803 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6804 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6805 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6806 | X509 *crt = NULL; |
| 6807 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6808 | if (!ctx) |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6809 | return 0; |
| 6810 | |
| 6811 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6812 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6813 | if (!crt) |
| 6814 | return 0; |
| 6815 | |
| 6816 | X509_free(crt); |
| 6817 | return 1; |
| 6818 | } |
| 6819 | |
| 6820 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6821 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6822 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6823 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6824 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6825 | if (!ctx) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6826 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6827 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | /* returns result from SSL verify */ |
| 6831 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6832 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6833 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6834 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6835 | if (!ctx) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6836 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6837 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6838 | } |
| 6839 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6840 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6841 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6842 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6843 | * freed by the caller. NPN is also checked if available since older versions |
| 6844 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6845 | */ |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 6846 | 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] | 6847 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6848 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6849 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6850 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6851 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6852 | return 0; |
| 6853 | |
| 6854 | *str = NULL; |
| 6855 | |
| 6856 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6857 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6858 | if (*str) |
| 6859 | return 1; |
| 6860 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6861 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6862 | 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] | 6863 | if (*str) |
| 6864 | return 1; |
| 6865 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6866 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6867 | return 0; |
| 6868 | } |
| 6869 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6870 | /* "issuers-chain-path" load chain certificate in global */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6871 | 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] | 6872 | { |
| 6873 | X509 *ca; |
| 6874 | X509_NAME *name = NULL; |
| 6875 | ASN1_OCTET_STRING *skid = NULL; |
| 6876 | STACK_OF(X509) *chain = NULL; |
| 6877 | struct issuer_chain *issuer; |
| 6878 | struct eb64_node *node; |
| 6879 | char *path; |
| 6880 | u64 key; |
| 6881 | int ret = 0; |
| 6882 | |
| 6883 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 6884 | if (chain == NULL) { |
| 6885 | chain = sk_X509_new_null(); |
| 6886 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 6887 | name = X509_get_subject_name(ca); |
| 6888 | } |
| 6889 | if (!sk_X509_push(chain, ca)) { |
| 6890 | X509_free(ca); |
| 6891 | goto end; |
| 6892 | } |
| 6893 | } |
| 6894 | if (!chain) { |
| 6895 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 6896 | goto end; |
| 6897 | } |
| 6898 | if (!skid) { |
| 6899 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 6900 | goto end; |
| 6901 | } |
| 6902 | if (!name) { |
| 6903 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 6904 | goto end; |
| 6905 | } |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 6906 | key = XXH3(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6907 | 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] | 6908 | issuer = container_of(node, typeof(*issuer), node); |
| 6909 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 6910 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 6911 | goto end; |
| 6912 | } |
| 6913 | } |
| 6914 | issuer = calloc(1, sizeof *issuer); |
| 6915 | path = strdup(fp); |
| 6916 | if (!issuer || !path) { |
| 6917 | free(issuer); |
| 6918 | free(path); |
| 6919 | goto end; |
| 6920 | } |
| 6921 | issuer->node.key = key; |
| 6922 | issuer->path = path; |
| 6923 | issuer->chain = chain; |
| 6924 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6925 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6926 | ret = 1; |
| 6927 | end: |
| 6928 | if (skid) |
| 6929 | ASN1_OCTET_STRING_free(skid); |
| 6930 | if (chain) |
| 6931 | sk_X509_pop_free(chain, X509_free); |
| 6932 | return ret; |
| 6933 | } |
| 6934 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 6935 | struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6936 | { |
| 6937 | AUTHORITY_KEYID *akid; |
| 6938 | struct issuer_chain *issuer = NULL; |
| 6939 | |
| 6940 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
William Lallemand | f69cd68 | 2020-11-19 16:24:13 +0100 | [diff] [blame] | 6941 | if (akid && akid->keyid) { |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6942 | struct eb64_node *node; |
| 6943 | u64 hk; |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 6944 | hk = XXH3(ASN1_STRING_get0_data(akid->keyid), ASN1_STRING_length(akid->keyid), 0); |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6945 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 6946 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 6947 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 6948 | issuer = ti; |
| 6949 | break; |
| 6950 | } |
| 6951 | } |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6952 | } |
Remi Tricot-Le Breton | 1621dc1 | 2022-11-03 15:16:48 +0100 | [diff] [blame] | 6953 | AUTHORITY_KEYID_free(akid); |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6954 | return issuer; |
| 6955 | } |
| 6956 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6957 | void ssl_free_global_issuers(void) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6958 | { |
| 6959 | struct eb64_node *node, *back; |
| 6960 | struct issuer_chain *issuer; |
| 6961 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6962 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6963 | while (node) { |
| 6964 | issuer = container_of(node, typeof(*issuer), node); |
| 6965 | back = eb64_next(node); |
| 6966 | eb64_delete(node); |
| 6967 | free(issuer->path); |
| 6968 | sk_X509_pop_free(issuer->chain, X509_free); |
| 6969 | free(issuer); |
| 6970 | node = back; |
| 6971 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6972 | } |
| 6973 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 6974 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6975 | static int ssl_check_async_engine_count(void) { |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 6976 | int err_code = ERR_NONE; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6977 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6978 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6979 | 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] | 6980 | err_code = ERR_ABORT; |
| 6981 | } |
| 6982 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6983 | } |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6984 | #endif |
| 6985 | |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6986 | /* "show fd" helper to dump ssl internals. Warning: the output buffer is often |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6987 | * the common trash! It returns non-zero if the connection entry looks suspicious. |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6988 | */ |
Willy Tarreau | 8050efe | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 6989 | static int ssl_sock_show_fd(struct buffer *buf, const struct connection *conn, const void *ctx) |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6990 | { |
| 6991 | const struct ssl_sock_ctx *sctx = ctx; |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6992 | int ret = 0; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6993 | |
| 6994 | if (!sctx) |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6995 | return ret; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6996 | |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6997 | if (sctx->conn != conn) { |
| 6998 | chunk_appendf(&trash, " xctx.conn=%p(BOGUS)", sctx->conn); |
| 6999 | ret = 1; |
| 7000 | } |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7001 | chunk_appendf(&trash, " xctx.st=%d", sctx->xprt_st); |
| 7002 | |
| 7003 | if (sctx->xprt) { |
| 7004 | chunk_appendf(&trash, " .xprt=%s", sctx->xprt->name); |
| 7005 | if (sctx->xprt_ctx) |
| 7006 | chunk_appendf(&trash, " .xctx=%p", sctx->xprt_ctx); |
| 7007 | } |
| 7008 | |
| 7009 | chunk_appendf(&trash, " .wait.ev=%d", sctx->wait_event.events); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7010 | |
| 7011 | /* as soon as a shutdown is reported the lower layer unregisters its |
| 7012 | * subscriber, so the situations below are transient and rare enough to |
| 7013 | * be reported as suspicious. In any case they shouldn't last. |
| 7014 | */ |
| 7015 | if ((sctx->wait_event.events & 1) && (conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_ERROR))) |
| 7016 | ret = 1; |
| 7017 | if ((sctx->wait_event.events & 2) && (conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR))) |
| 7018 | ret = 1; |
| 7019 | |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7020 | chunk_appendf(&trash, " .subs=%p", sctx->subs); |
| 7021 | if (sctx->subs) { |
| 7022 | chunk_appendf(&trash, "(ev=%d tl=%p", sctx->subs->events, sctx->subs->tasklet); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7023 | if (sctx->subs->tasklet->calls >= 1000000) |
| 7024 | ret = 1; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7025 | chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 7026 | sctx->subs->tasklet->calls, |
| 7027 | sctx->subs->tasklet->context); |
| 7028 | resolve_sym_name(&trash, NULL, sctx->subs->tasklet->process); |
| 7029 | chunk_appendf(&trash, ")"); |
| 7030 | } |
| 7031 | chunk_appendf(&trash, " .sent_early=%d", sctx->sent_early_data); |
| 7032 | chunk_appendf(&trash, " .early_in=%d", (int)sctx->early_buf.data); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7033 | return ret; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7034 | } |
| 7035 | |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7036 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7037 | /* This function is used with TLS ticket keys management. It permits to browse |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7038 | * each reference. The variable <ref> must point to the current node's list |
| 7039 | * element (which starts by the root), and <end> must point to the root node. |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7040 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7041 | static inline |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7042 | struct tls_keys_ref *tlskeys_list_get_next(struct list *ref, struct list *end) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7043 | { |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7044 | /* Get next list entry. */ |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7045 | ref = ref->n; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7046 | |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7047 | /* If the entry is the last of the list, return NULL. */ |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7048 | if (ref == end) |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7049 | return NULL; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7050 | |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7051 | return LIST_ELEM(ref, struct tls_keys_ref *, list); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7052 | } |
| 7053 | |
| 7054 | static inline |
| 7055 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 7056 | { |
| 7057 | int id; |
| 7058 | char *error; |
| 7059 | |
| 7060 | /* If the reference starts by a '#', this is numeric id. */ |
| 7061 | if (reference[0] == '#') { |
| 7062 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 7063 | id = strtol(reference + 1, &error, 10); |
| 7064 | if (*error != '\0') |
| 7065 | return NULL; |
| 7066 | |
| 7067 | /* Perform the unique id lookup. */ |
| 7068 | return tlskeys_ref_lookupid(id); |
| 7069 | } |
| 7070 | |
| 7071 | /* Perform the string lookup. */ |
| 7072 | return tlskeys_ref_lookup(reference); |
| 7073 | } |
| 7074 | #endif |
| 7075 | |
| 7076 | |
| 7077 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7078 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7079 | /* dumps all tls keys. Relies on the show_keys_ctx context from the appctx. */ |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7080 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) |
| 7081 | { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7082 | struct show_keys_ctx *ctx = appctx->svcctx; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7083 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7084 | switch (ctx->state) { |
| 7085 | case SHOW_KEYS_INIT: |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7086 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7087 | * quit the function with returning 0. The function is called |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7088 | * later and restart at the state "SHOW_KEYS_INIT". |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7089 | */ |
| 7090 | chunk_reset(&trash); |
| 7091 | |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7092 | if (ctx->dump_entries) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7093 | chunk_appendf(&trash, "# id secret\n"); |
| 7094 | else |
| 7095 | chunk_appendf(&trash, "# id (file)\n"); |
| 7096 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7097 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7098 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7099 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7100 | /* Now, we start the browsing of the references lists. |
| 7101 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 7102 | * available field of this pointer is <list>. It is used with the function |
| 7103 | * tlskeys_list_get_next() for retruning the first available entry |
| 7104 | */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7105 | if (ctx->next_ref == NULL) |
| 7106 | ctx->next_ref = tlskeys_list_get_next(&tlskeys_reference, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7107 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7108 | ctx->state = SHOW_KEYS_LIST; |
Willy Tarreau | a551f4f | 2022-11-14 07:34:43 +0100 | [diff] [blame] | 7109 | __fallthrough; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7110 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7111 | case SHOW_KEYS_LIST: |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7112 | while (ctx->next_ref) { |
| 7113 | struct tls_keys_ref *ref = ctx->next_ref; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7114 | |
| 7115 | chunk_reset(&trash); |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7116 | if (ctx->dump_entries && ctx->next_index == 0) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7117 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7118 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7119 | if (ctx->next_index == 0) |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7120 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 7121 | |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7122 | if (ctx->dump_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7123 | int head; |
| 7124 | |
| 7125 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 7126 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7127 | while (ctx->next_index < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7128 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7129 | |
| 7130 | chunk_reset(t2); |
| 7131 | /* 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] | 7132 | if (ref->key_size_bits == 128) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7133 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + ctx->next_index) % TLS_TICKETS_NO), |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7134 | sizeof(struct tls_sess_key_128), |
| 7135 | t2->area, t2->size); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7136 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, ctx->next_index, |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7137 | t2->area); |
| 7138 | } |
| 7139 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7140 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + ctx->next_index) % TLS_TICKETS_NO), |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7141 | sizeof(struct tls_sess_key_256), |
| 7142 | t2->area, t2->size); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7143 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, ctx->next_index, |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7144 | t2->area); |
| 7145 | } |
| 7146 | else { |
| 7147 | /* This case should never happen */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7148 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, ctx->next_index); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7149 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7150 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7151 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7152 | /* let's try again later from this stream. We add ourselves into |
| 7153 | * this stream's users so that it can remove us upon termination. |
| 7154 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7155 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7156 | return 0; |
| 7157 | } |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7158 | ctx->next_index++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7159 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7160 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7161 | ctx->next_index = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7162 | } |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7163 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7164 | /* let's try again later from this stream. We add ourselves into |
| 7165 | * this stream's users so that it can remove us upon termination. |
| 7166 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7167 | return 0; |
| 7168 | } |
| 7169 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7170 | if (ctx->names_only == 0) /* don't display everything if not necessary */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7171 | break; |
| 7172 | |
| 7173 | /* get next list entry and check the end of the list */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7174 | ctx->next_ref = tlskeys_list_get_next(&ref->list, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7175 | } |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7176 | ctx->state = SHOW_KEYS_DONE; |
Willy Tarreau | a551f4f | 2022-11-14 07:34:43 +0100 | [diff] [blame] | 7177 | __fallthrough; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7178 | |
| 7179 | default: |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7180 | return 1; |
| 7181 | } |
| 7182 | return 0; |
| 7183 | } |
| 7184 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7185 | /* Prepares a "show_keys_ctx" and sets the appropriate io_handler if needed */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7186 | 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] | 7187 | { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7188 | struct show_keys_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 7189 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7190 | /* no parameter, shows only file list */ |
| 7191 | if (!*args[2]) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7192 | ctx->names_only = 1; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7193 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7194 | } |
| 7195 | |
| 7196 | if (args[2][0] == '*') { |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7197 | /* list every TLS ticket keys */ |
| 7198 | ctx->names_only = 1; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7199 | } else { |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7200 | ctx->next_ref = tlskeys_ref_lookup_ref(args[2]); |
| 7201 | if (!ctx->next_ref) |
| 7202 | return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n"); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7203 | } |
| 7204 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7205 | ctx->dump_entries = 1; |
| 7206 | return 0; |
| 7207 | } |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7208 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7209 | static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
| 7210 | { |
| 7211 | struct tls_keys_ref *ref; |
| 7212 | int ret; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7213 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7214 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 7215 | if (!*args[3] || !*args[4]) |
| 7216 | return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n"); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7217 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7218 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 7219 | if (!ref) |
| 7220 | return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n"); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7221 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7222 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
| 7223 | if (ret < 0) |
| 7224 | return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n"); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7225 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7226 | trash.data = ret; |
| 7227 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 7228 | return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n"); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7229 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7230 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7231 | } |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7232 | #endif |
| 7233 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7234 | |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7235 | #ifdef HAVE_SSL_PROVIDERS |
| 7236 | struct provider_name { |
| 7237 | const char *name; |
| 7238 | struct list list; |
| 7239 | }; |
| 7240 | |
| 7241 | |
| 7242 | static int ssl_provider_get_name_cb(OSSL_PROVIDER *provider, void *cbdata) |
| 7243 | { |
| 7244 | struct list *provider_names = cbdata; |
| 7245 | struct provider_name *item = NULL; |
| 7246 | const char *name = OSSL_PROVIDER_get0_name(provider); |
| 7247 | |
| 7248 | if (!provider_names) |
| 7249 | return 0; |
| 7250 | |
| 7251 | item = calloc(1, sizeof(*item)); |
| 7252 | |
| 7253 | if (!item) |
| 7254 | return 0; |
| 7255 | |
| 7256 | item->name = name; |
| 7257 | LIST_APPEND(provider_names, &item->list); |
| 7258 | |
| 7259 | return 1; |
| 7260 | } |
| 7261 | |
| 7262 | static void ssl_provider_get_name_list(struct list *provider_names) |
| 7263 | { |
| 7264 | if (!provider_names) |
| 7265 | return; |
| 7266 | |
| 7267 | OSSL_PROVIDER_do_all(NULL, ssl_provider_get_name_cb, provider_names); |
| 7268 | } |
| 7269 | |
| 7270 | static void ssl_provider_clear_name_list(struct list *provider_names) |
| 7271 | { |
| 7272 | struct provider_name *item = NULL, *item_s = NULL; |
| 7273 | |
| 7274 | if (provider_names) { |
| 7275 | list_for_each_entry_safe(item, item_s, provider_names, list) { |
| 7276 | LIST_DELETE(&item->list); |
| 7277 | free(item); |
| 7278 | } |
| 7279 | } |
| 7280 | } |
| 7281 | |
| 7282 | static int cli_io_handler_show_providers(struct appctx *appctx) |
| 7283 | { |
| 7284 | struct buffer *trash = get_trash_chunk(); |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7285 | struct list provider_names; |
| 7286 | struct provider_name *name; |
| 7287 | |
| 7288 | LIST_INIT(&provider_names); |
| 7289 | |
| 7290 | chunk_appendf(trash, "Loaded providers : \n"); |
| 7291 | |
| 7292 | ssl_provider_get_name_list(&provider_names); |
| 7293 | |
| 7294 | list_for_each_entry(name, &provider_names, list) { |
| 7295 | chunk_appendf(trash, "\t- %s\n", name->name); |
| 7296 | } |
| 7297 | |
| 7298 | ssl_provider_clear_name_list(&provider_names); |
| 7299 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7300 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7301 | goto yield; |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7302 | |
| 7303 | return 1; |
| 7304 | |
| 7305 | yield: |
| 7306 | return 0; |
| 7307 | } |
| 7308 | #endif |
Remi Tricot-Le Breton | eeaa29b | 2022-12-20 11:11:07 +0100 | [diff] [blame] | 7309 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7310 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7311 | /* register cli keywords */ |
| 7312 | static struct cli_kw_list cli_kws = {{ },{ |
| 7313 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
Remi Tricot-Le Breton | eeaa29b | 2022-12-20 11:11:07 +0100 | [diff] [blame] | 7314 | { { "show", "tls-keys", NULL }, "show tls-keys [id|*] : show tls keys references or dump tls ticket keys when id specified", cli_parse_show_tlskeys, cli_io_handler_tlskeys_files }, |
| 7315 | { { "set", "ssl", "tls-key", NULL }, "set ssl tls-key [id|file] <key> : set the next TLS key for the <id> or <file> listener to <key>", cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7316 | #endif |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7317 | #ifdef HAVE_SSL_PROVIDERS |
| 7318 | { { "show", "ssl", "providers", NULL }, "show ssl providers : show loaded SSL providers", NULL, cli_io_handler_show_providers }, |
| 7319 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7320 | { { NULL }, NULL, NULL, NULL } |
| 7321 | }}; |
| 7322 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7323 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7324 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 7325 | /* transport-layer operations for SSL sockets */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 7326 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7327 | .snd_buf = ssl_sock_from_buf, |
| 7328 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 7329 | .subscribe = ssl_subscribe, |
| 7330 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 7331 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 7332 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7333 | .rcv_pipe = NULL, |
| 7334 | .snd_pipe = NULL, |
| 7335 | .shutr = NULL, |
| 7336 | .shutw = ssl_sock_shutw, |
| 7337 | .close = ssl_sock_close, |
| 7338 | .init = ssl_sock_init, |
Olivier Houchard | bc5ce92 | 2021-03-05 23:47:00 +0100 | [diff] [blame] | 7339 | .start = ssl_sock_start, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 7340 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 7341 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7342 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 7343 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7344 | .get_alpn = ssl_sock_get_alpn, |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 7345 | .takeover = ssl_takeover, |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 7346 | .set_idle = ssl_set_idle, |
| 7347 | .set_used = ssl_set_used, |
Willy Tarreau | de82795 | 2022-04-11 10:43:28 +0200 | [diff] [blame] | 7348 | .get_ssl_sock_ctx = ssl_sock_get_ctx, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 7349 | .name = "SSL", |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7350 | .show_fd = ssl_sock_show_fd, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7351 | }; |
| 7352 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7353 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 7354 | struct session *sess, struct stream *s, int flags) |
| 7355 | { |
| 7356 | struct connection *conn; |
| 7357 | |
| 7358 | conn = objt_conn(sess->origin); |
| 7359 | |
Willy Tarreau | b52d4d2 | 2022-05-27 10:44:39 +0200 | [diff] [blame] | 7360 | if (conn) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7361 | if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) { |
Willy Tarreau | b52d4d2 | 2022-05-27 10:44:39 +0200 | [diff] [blame] | 7362 | sc_ep_set(s->scf, SE_FL_WAIT_FOR_HS); |
Christopher Faulet | 6e1bbc4 | 2022-12-12 08:08:15 +0100 | [diff] [blame] | 7363 | s->req.flags |= CF_READ_EVENT; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7364 | return ACT_RET_YIELD; |
| 7365 | } |
| 7366 | } |
| 7367 | return (ACT_RET_CONT); |
| 7368 | } |
| 7369 | |
| 7370 | 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) |
| 7371 | { |
| 7372 | rule->action_ptr = ssl_action_wait_for_hs; |
| 7373 | |
| 7374 | return ACT_RET_PRS_OK; |
| 7375 | } |
| 7376 | |
| 7377 | static struct action_kw_list http_req_actions = {ILH, { |
| 7378 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 7379 | { /* END */ } |
| 7380 | }}; |
| 7381 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7382 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 7383 | |
Ilya Shipitsin | f00cdb1 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 7384 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7385 | |
| 7386 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7387 | { |
| 7388 | if (ptr) { |
| 7389 | chunk_destroy(ptr); |
| 7390 | free(ptr); |
| 7391 | } |
| 7392 | } |
| 7393 | |
| 7394 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7395 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7396 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7397 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7398 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7399 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7400 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7401 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 7402 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7403 | static void ssl_sock_keylog_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7404 | { |
| 7405 | struct ssl_keylog *keylog; |
| 7406 | |
| 7407 | if (!ptr) |
| 7408 | return; |
| 7409 | |
| 7410 | keylog = ptr; |
| 7411 | |
| 7412 | pool_free(pool_head_ssl_keylog_str, keylog->client_random); |
| 7413 | pool_free(pool_head_ssl_keylog_str, keylog->client_early_traffic_secret); |
| 7414 | pool_free(pool_head_ssl_keylog_str, keylog->client_handshake_traffic_secret); |
| 7415 | pool_free(pool_head_ssl_keylog_str, keylog->server_handshake_traffic_secret); |
| 7416 | pool_free(pool_head_ssl_keylog_str, keylog->client_traffic_secret_0); |
| 7417 | pool_free(pool_head_ssl_keylog_str, keylog->server_traffic_secret_0); |
| 7418 | pool_free(pool_head_ssl_keylog_str, keylog->exporter_secret); |
| 7419 | pool_free(pool_head_ssl_keylog_str, keylog->early_exporter_secret); |
| 7420 | |
| 7421 | pool_free(pool_head_ssl_keylog, ptr); |
| 7422 | } |
| 7423 | #endif |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7424 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 7425 | static void ssl_sock_clt_crt_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7426 | { |
| 7427 | if (!ptr) |
| 7428 | return; |
| 7429 | |
| 7430 | X509_free((X509*)ptr); |
| 7431 | } |
| 7432 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 7433 | static void ssl_sock_clt_sni_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7434 | { |
| 7435 | pool_free(ssl_sock_client_sni_pool, ptr); |
| 7436 | } |
| 7437 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7438 | static void __ssl_sock_init(void) |
| 7439 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7440 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7441 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7442 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7443 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7444 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7445 | if (global_ssl.listen_default_ciphers) |
| 7446 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 7447 | if (global_ssl.connect_default_ciphers) |
| 7448 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 7449 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7450 | if (global_ssl.listen_default_ciphersuites) |
| 7451 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 7452 | if (global_ssl.connect_default_ciphersuites) |
| 7453 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 7454 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 7455 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 7456 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7457 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7458 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7459 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7460 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7461 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7462 | n = sk_SSL_COMP_num(cm); |
| 7463 | while (n--) { |
| 7464 | (void) sk_SSL_COMP_pop(cm); |
| 7465 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7466 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7467 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7468 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7469 | ssl_locking_init(); |
| 7470 | #endif |
Ilya Shipitsin | f00cdb1 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 7471 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7472 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 7473 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7474 | |
| 7475 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 7476 | ocsp_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_ocsp_free_func); |
| 7477 | #endif |
| 7478 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 7479 | 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] | 7480 | ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 7481 | #ifdef USE_QUIC |
| 7482 | ssl_qc_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7483 | #endif /* USE_QUIC */ |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 7484 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7485 | ssl_keylog_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_keylog_free_func); |
| 7486 | #endif |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 7487 | ssl_client_crt_ref_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_clt_crt_free_func); |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 7488 | ssl_client_sni_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_clt_sni_free_func); |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 7489 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7490 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7491 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7492 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 7493 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7494 | hap_register_post_check(tlskeys_finalize_config); |
| 7495 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7496 | |
| 7497 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 7498 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 7499 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 7500 | hap_register_post_deinit(ssl_free_global_issuers); |
| 7501 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7502 | #ifndef OPENSSL_NO_DH |
| 7503 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7504 | hap_register_post_deinit(ssl_free_dh); |
| 7505 | #endif |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 7506 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7507 | hap_register_post_deinit(ssl_free_engines); |
| 7508 | #endif |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 7509 | #ifdef HAVE_SSL_PROVIDERS |
| 7510 | hap_register_post_deinit(ssl_unload_providers); |
| 7511 | #endif |
Remi Tricot-Le Breton | 78a36e3 | 2022-02-11 12:04:45 +0100 | [diff] [blame] | 7512 | #if HA_OPENSSL_VERSION_NUMBER < 0x3000000fL |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7513 | /* Load SSL string for the verbose & debug mode. */ |
| 7514 | ERR_load_SSL_strings(); |
Remi Tricot-Le Breton | 78a36e3 | 2022-02-11 12:04:45 +0100 | [diff] [blame] | 7515 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 7516 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 7517 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 7518 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 7519 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 7520 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 7521 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 7522 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 7523 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 7524 | |
| 7525 | HA_SPIN_INIT(&ckch_lock); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 7526 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7527 | HA_SPIN_INIT(&ocsp_tree_lock); |
| 7528 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 7529 | /* Try to register dedicated SSL/TLS protocol message callbacks for |
| 7530 | * heartbleed attack (CVE-2014-0160) and clienthello. |
| 7531 | */ |
| 7532 | hap_register_post_check(ssl_sock_register_msg_callbacks); |
| 7533 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 7534 | /* Try to free all callbacks that were registered by using |
| 7535 | * ssl_sock_register_msg_callback(). |
| 7536 | */ |
| 7537 | hap_register_post_deinit(ssl_sock_unregister_msg_callbacks); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7538 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7539 | INITCALL0(STG_REGISTER, __ssl_sock_init); |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7540 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7541 | /* Compute and register the version string */ |
| 7542 | static void ssl_register_build_options() |
| 7543 | { |
| 7544 | char *ptr = NULL; |
| 7545 | int i; |
| 7546 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7547 | memprintf(&ptr, "Built with OpenSSL version : " |
| 7548 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 7549 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7550 | #else /* OPENSSL_IS_BORINGSSL */ |
| 7551 | OPENSSL_VERSION_TEXT |
| 7552 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7553 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 7554 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7555 | #endif |
| 7556 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7557 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7558 | "no (library version too old)" |
| 7559 | #elif defined(OPENSSL_NO_TLSEXT) |
| 7560 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 7561 | #else |
| 7562 | "yes" |
| 7563 | #endif |
| 7564 | "", ptr); |
| 7565 | |
| 7566 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 7567 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7568 | "yes" |
| 7569 | #else |
| 7570 | #ifdef OPENSSL_NO_TLSEXT |
| 7571 | "no (because of OPENSSL_NO_TLSEXT)" |
| 7572 | #else |
| 7573 | "no (version might be too old, 0.9.8f min needed)" |
| 7574 | #endif |
| 7575 | #endif |
| 7576 | "", ptr); |
| 7577 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 7578 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 7579 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7580 | if (methodVersions[i].option) |
| 7581 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 7582 | |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7583 | #ifdef HAVE_SSL_PROVIDERS |
| 7584 | { |
| 7585 | struct list provider_names; |
| 7586 | struct provider_name *name; |
| 7587 | LIST_INIT(&provider_names); |
| 7588 | ssl_provider_get_name_list(&provider_names); |
| 7589 | |
| 7590 | memprintf(&ptr, "%s\nOpenSSL providers loaded :", ptr); |
| 7591 | |
| 7592 | list_for_each_entry(name, &provider_names, list) { |
| 7593 | memprintf(&ptr, "%s %s", ptr, name->name); |
| 7594 | } |
| 7595 | |
| 7596 | ssl_provider_clear_name_list(&provider_names); |
| 7597 | } |
| 7598 | #endif |
| 7599 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7600 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7601 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7602 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7603 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 7604 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 7605 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7606 | void ssl_free_engines(void) { |
| 7607 | struct ssl_engine_list *wl, *wlb; |
| 7608 | /* free up engine list */ |
| 7609 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 7610 | ENGINE_finish(wl->e); |
| 7611 | ENGINE_free(wl->e); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 7612 | LIST_DELETE(&wl->list); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7613 | free(wl); |
| 7614 | } |
| 7615 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7616 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7617 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 7618 | #ifdef HAVE_SSL_PROVIDERS |
| 7619 | void ssl_unload_providers(void) { |
| 7620 | struct ssl_provider_list *prov, *provb; |
| 7621 | list_for_each_entry_safe(prov, provb, &openssl_providers, list) { |
| 7622 | OSSL_PROVIDER_unload(prov->provider); |
| 7623 | LIST_DELETE(&prov->list); |
| 7624 | free(prov); |
| 7625 | } |
| 7626 | } |
| 7627 | #endif |
| 7628 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7629 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7630 | void ssl_free_dh(void) { |
| 7631 | if (local_dh_1024) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7632 | HASSL_DH_free(local_dh_1024); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7633 | local_dh_1024 = NULL; |
| 7634 | } |
| 7635 | if (local_dh_2048) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7636 | HASSL_DH_free(local_dh_2048); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7637 | local_dh_2048 = NULL; |
| 7638 | } |
| 7639 | if (local_dh_4096) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7640 | HASSL_DH_free(local_dh_4096); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7641 | local_dh_4096 = NULL; |
| 7642 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7643 | if (global_dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7644 | HASSL_DH_free(global_dh); |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7645 | global_dh = NULL; |
| 7646 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7647 | } |
| 7648 | #endif |
| 7649 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7650 | static void __ssl_sock_deinit(void) |
| 7651 | { |
| 7652 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7653 | if (ssl_ctx_lru_tree) { |
| 7654 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7655 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7656 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7657 | #endif |
| 7658 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7659 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7660 | ERR_remove_state(0); |
| 7661 | ERR_free_strings(); |
| 7662 | |
| 7663 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7664 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7665 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7666 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7667 | CRYPTO_cleanup_all_ex_data(); |
| 7668 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 7669 | BIO_meth_free(ha_meth); |
Remi Tricot-Le Breton | 6477bbd | 2022-12-20 11:11:13 +0100 | [diff] [blame] | 7670 | |
| 7671 | #if !defined OPENSSL_NO_OCSP |
| 7672 | ssl_destroy_ocsp_update_task(); |
| 7673 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7674 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7675 | REGISTER_POST_DEINIT(__ssl_sock_deinit); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7676 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 7677 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7678 | /* |
| 7679 | * Local variables: |
| 7680 | * c-indent-level: 8 |
| 7681 | * c-basic-offset: 8 |
| 7682 | * End: |
| 7683 | */ |