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; |
Willy Tarreau | ac78c4f | 2023-03-17 16:13:05 +0100 | [diff] [blame] | 217 | uint flags; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 218 | int ret; |
| 219 | |
| 220 | ctx = BIO_get_data(h); |
| 221 | tmpbuf.size = num; |
| 222 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 223 | tmpbuf.data = num; |
| 224 | tmpbuf.head = 0; |
Willy Tarreau | ac78c4f | 2023-03-17 16:13:05 +0100 | [diff] [blame] | 225 | flags = (ctx->xprt_st & SSL_SOCK_SEND_MORE) ? CO_SFL_MSG_MORE : 0; |
| 226 | ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, flags); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 227 | 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] | 228 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 229 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 230 | } else if (ret == 0) |
| 231 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 232 | return ret; |
| 233 | } |
| 234 | |
| 235 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 236 | { |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | static int ha_ssl_puts(BIO *h, const char *str) |
| 242 | { |
| 243 | |
| 244 | return ha_ssl_write(h, str, strlen(str)); |
| 245 | } |
| 246 | |
| 247 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 248 | { |
| 249 | struct buffer tmpbuf; |
| 250 | struct ssl_sock_ctx *ctx; |
| 251 | int ret; |
| 252 | |
| 253 | ctx = BIO_get_data(h); |
| 254 | tmpbuf.size = size; |
| 255 | tmpbuf.area = buf; |
| 256 | tmpbuf.data = 0; |
| 257 | tmpbuf.head = 0; |
| 258 | 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] | 259 | 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] | 260 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 261 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 262 | } else if (ret == 0) |
| 263 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 264 | |
| 265 | return ret; |
| 266 | } |
| 267 | |
| 268 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 269 | { |
| 270 | int ret = 0; |
| 271 | switch (cmd) { |
| 272 | case BIO_CTRL_DUP: |
| 273 | case BIO_CTRL_FLUSH: |
| 274 | ret = 1; |
| 275 | break; |
| 276 | } |
| 277 | return ret; |
| 278 | } |
| 279 | |
| 280 | static int ha_ssl_new(BIO *h) |
| 281 | { |
| 282 | BIO_set_init(h, 1); |
| 283 | BIO_set_data(h, NULL); |
| 284 | BIO_clear_flags(h, ~0); |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | static int ha_ssl_free(BIO *data) |
| 289 | { |
| 290 | |
| 291 | return 1; |
| 292 | } |
| 293 | |
| 294 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 295 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 296 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 297 | static HA_RWLOCK_T *ssl_rwlocks; |
| 298 | |
| 299 | |
| 300 | unsigned long ssl_id_function(void) |
| 301 | { |
| 302 | return (unsigned long)tid; |
| 303 | } |
| 304 | |
| 305 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 306 | { |
| 307 | if (mode & CRYPTO_LOCK) { |
| 308 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 309 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 310 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 311 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 312 | } |
| 313 | else { |
| 314 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 315 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 316 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 317 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | |
| 321 | static int ssl_locking_init(void) |
| 322 | { |
| 323 | int i; |
| 324 | |
| 325 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 326 | if (!ssl_rwlocks) |
| 327 | return -1; |
| 328 | |
| 329 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 330 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 331 | |
| 332 | CRYPTO_set_id_callback(ssl_id_function); |
| 333 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 334 | |
| 335 | return 0; |
| 336 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 337 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 338 | #endif |
| 339 | |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 340 | __decl_thread(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 341 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 342 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 343 | |
| 344 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 345 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 346 | { |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 347 | X509_STORE *store = NULL; |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 348 | struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0); |
| 349 | if (ca_e) |
| 350 | store = ca_e->ca_store; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 351 | if (store_ctx && store) { |
| 352 | int i; |
| 353 | X509_OBJECT *obj; |
| 354 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 355 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 356 | obj = sk_X509_OBJECT_value(objs, i); |
| 357 | switch (X509_OBJECT_get_type(obj)) { |
| 358 | case X509_LU_X509: |
| 359 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 360 | break; |
| 361 | case X509_LU_CRL: |
| 362 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 363 | break; |
| 364 | default: |
| 365 | break; |
| 366 | } |
| 367 | } |
| 368 | return 1; |
| 369 | } |
| 370 | return 0; |
| 371 | } |
| 372 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 373 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 374 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 375 | { |
| 376 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 377 | return ssl_set_cert_crl_file(store_ctx, path); |
| 378 | } |
| 379 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 380 | /* |
| 381 | Extract CA_list from CA_file already in tree. |
| 382 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 383 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 384 | */ |
| 385 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 386 | { |
| 387 | struct ebmb_node *eb; |
| 388 | struct cafile_entry *ca_e; |
| 389 | |
| 390 | eb = ebst_lookup(&cafile_tree, path); |
| 391 | if (!eb) |
| 392 | return NULL; |
| 393 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 394 | |
| 395 | if (ca_e->ca_list == NULL) { |
| 396 | int i; |
| 397 | unsigned long key; |
| 398 | struct eb_root ca_name_tree = EB_ROOT; |
| 399 | struct eb64_node *node, *back; |
| 400 | struct { |
| 401 | struct eb64_node node; |
| 402 | X509_NAME *xname; |
| 403 | } *ca_name; |
| 404 | STACK_OF(X509_OBJECT) *objs; |
| 405 | STACK_OF(X509_NAME) *skn; |
| 406 | X509 *x; |
| 407 | X509_NAME *xn; |
| 408 | |
| 409 | skn = sk_X509_NAME_new_null(); |
| 410 | /* take x509 from cafile_tree */ |
| 411 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 412 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 413 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 414 | if (!x) |
| 415 | continue; |
| 416 | xn = X509_get_subject_name(x); |
| 417 | if (!xn) |
| 418 | continue; |
| 419 | /* Check for duplicates. */ |
| 420 | key = X509_NAME_hash(xn); |
| 421 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 422 | node && ca_name == NULL; |
| 423 | node = eb64_next(node)) { |
| 424 | ca_name = container_of(node, typeof(*ca_name), node); |
| 425 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 426 | ca_name = NULL; |
| 427 | } |
| 428 | /* find a duplicate */ |
| 429 | if (ca_name) |
| 430 | continue; |
| 431 | ca_name = calloc(1, sizeof *ca_name); |
| 432 | xn = X509_NAME_dup(xn); |
| 433 | if (!ca_name || |
| 434 | !xn || |
| 435 | !sk_X509_NAME_push(skn, xn)) { |
| 436 | free(ca_name); |
| 437 | X509_NAME_free(xn); |
| 438 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 439 | sk_X509_NAME_free(skn); |
| 440 | skn = NULL; |
| 441 | break; |
| 442 | } |
| 443 | ca_name->node.key = key; |
| 444 | ca_name->xname = xn; |
| 445 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 446 | } |
| 447 | ca_e->ca_list = skn; |
| 448 | /* remove temporary ca_name tree */ |
| 449 | node = eb64_first(&ca_name_tree); |
| 450 | while (node) { |
| 451 | ca_name = container_of(node, typeof(*ca_name), node); |
| 452 | back = eb64_next(node); |
| 453 | eb64_delete(node); |
| 454 | free(ca_name); |
| 455 | node = back; |
| 456 | } |
| 457 | } |
| 458 | return ca_e->ca_list; |
| 459 | } |
| 460 | |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 461 | struct pool_head *pool_head_ssl_capture __read_mostly = NULL; |
William Lallemand | 15e1694 | 2020-05-15 00:25:08 +0200 | [diff] [blame] | 462 | int ssl_capture_ptr_index = -1; |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 463 | int ssl_app_data_index = -1; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 464 | #ifdef USE_QUIC |
| 465 | int ssl_qc_app_data_index = -1; |
| 466 | #endif /* USE_QUIC */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 467 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 468 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 469 | int ssl_keylog_index = -1; |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 470 | struct pool_head *pool_head_ssl_keylog __read_mostly = NULL; |
| 471 | struct pool_head *pool_head_ssl_keylog_str __read_mostly = NULL; |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 472 | #endif |
| 473 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 474 | int ssl_client_crt_ref_index = -1; |
| 475 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 476 | /* Used to store the client's SNI in case of ClientHello callback error */ |
| 477 | int ssl_client_sni_index = -1; |
| 478 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 479 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 480 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 481 | #endif |
| 482 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 483 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 484 | unsigned int openssl_engines_initialized; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 485 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 486 | struct ssl_engine_list { |
| 487 | struct list list; |
| 488 | ENGINE *e; |
| 489 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 490 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 491 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 492 | #ifdef HAVE_SSL_PROVIDERS |
| 493 | struct list openssl_providers = LIST_HEAD_INIT(openssl_providers); |
| 494 | struct ssl_provider_list { |
| 495 | struct list list; |
| 496 | OSSL_PROVIDER *provider; |
| 497 | }; |
| 498 | #endif |
| 499 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 500 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 501 | static int ssl_dh_ptr_index = -1; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 502 | static HASSL_DH *global_dh = NULL; |
| 503 | static HASSL_DH *local_dh_1024 = NULL; |
| 504 | static HASSL_DH *local_dh_2048 = NULL; |
| 505 | static HASSL_DH *local_dh_4096 = NULL; |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 506 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 507 | 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] | 508 | #else |
| 509 | static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey); |
| 510 | #endif |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 511 | #endif /* OPENSSL_NO_DH */ |
| 512 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 513 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 514 | /* X509V3 Extensions that will be added on generated certificates */ |
| 515 | #define X509V3_EXT_SIZE 5 |
| 516 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 517 | "basicConstraints", |
| 518 | "nsComment", |
| 519 | "subjectKeyIdentifier", |
| 520 | "authorityKeyIdentifier", |
| 521 | "keyUsage", |
| 522 | }; |
| 523 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 524 | "CA:FALSE", |
| 525 | "\"OpenSSL Generated Certificate\"", |
| 526 | "hash", |
| 527 | "keyid,issuer:always", |
| 528 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 529 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 530 | /* LRU cache to store generated certificate */ |
| 531 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 532 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 533 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 534 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 535 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 536 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 537 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 538 | /* The order here matters for picking a default context, |
| 539 | * keep the most common keytype at the bottom of the list |
| 540 | */ |
| 541 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 542 | "dsa", |
| 543 | "ecdsa", |
| 544 | "rsa" |
| 545 | }; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 546 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 547 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 548 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 549 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 550 | /* Dedicated callback functions for heartbeat and clienthello. |
| 551 | */ |
| 552 | #ifdef TLS1_RT_HEARTBEAT |
| 553 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 554 | int content_type, const void *buf, size_t len, |
| 555 | SSL *ssl); |
| 556 | #endif |
| 557 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 558 | int content_type, const void *buf, size_t len, |
| 559 | SSL *ssl); |
| 560 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 561 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 562 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 563 | int content_type, const void *buf, size_t len, |
| 564 | SSL *ssl); |
| 565 | #endif |
| 566 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 567 | /* List head of all registered SSL/TLS protocol message callbacks. */ |
| 568 | struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks); |
| 569 | |
| 570 | /* Registers the function <func> in order to be called on SSL/TLS protocol |
| 571 | * message processing. It will return 0 if the function <func> is not set |
| 572 | * or if it fails to allocate memory. |
| 573 | */ |
| 574 | int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func) |
| 575 | { |
| 576 | struct ssl_sock_msg_callback *cbk; |
| 577 | |
| 578 | if (!func) |
| 579 | return 0; |
| 580 | |
| 581 | cbk = calloc(1, sizeof(*cbk)); |
| 582 | if (!cbk) { |
| 583 | ha_alert("out of memory in ssl_sock_register_msg_callback().\n"); |
| 584 | return 0; |
| 585 | } |
| 586 | |
| 587 | cbk->func = func; |
| 588 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 589 | LIST_APPEND(&ssl_sock_msg_callbacks, &cbk->list); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 590 | |
| 591 | return 1; |
| 592 | } |
| 593 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 594 | /* Used to register dedicated SSL/TLS protocol message callbacks. |
| 595 | */ |
| 596 | static int ssl_sock_register_msg_callbacks(void) |
| 597 | { |
| 598 | #ifdef TLS1_RT_HEARTBEAT |
| 599 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat)) |
| 600 | return ERR_ABORT; |
| 601 | #endif |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 602 | if (global_ssl.capture_buffer_size > 0) { |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 603 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello)) |
| 604 | return ERR_ABORT; |
| 605 | } |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 606 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 607 | if (global_ssl.keylog > 0) { |
| 608 | if (!ssl_sock_register_msg_callback(ssl_init_keylog)) |
| 609 | return ERR_ABORT; |
| 610 | } |
| 611 | #endif |
| 612 | |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 613 | return ERR_NONE; |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 614 | } |
| 615 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 616 | /* Used to free all SSL/TLS protocol message callbacks that were |
| 617 | * registered by using ssl_sock_register_msg_callback(). |
| 618 | */ |
| 619 | static void ssl_sock_unregister_msg_callbacks(void) |
| 620 | { |
| 621 | struct ssl_sock_msg_callback *cbk, *cbkback; |
| 622 | |
| 623 | list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 624 | LIST_DELETE(&cbk->list); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 625 | free(cbk); |
| 626 | } |
| 627 | } |
| 628 | |
Willy Tarreau | de82795 | 2022-04-11 10:43:28 +0200 | [diff] [blame] | 629 | static struct ssl_sock_ctx *ssl_sock_get_ctx(struct connection *conn) |
| 630 | { |
| 631 | if (!conn || conn->xprt != xprt_get(XPRT_SSL) || !conn->xprt_ctx) |
| 632 | return NULL; |
| 633 | |
| 634 | return (struct ssl_sock_ctx *)conn->xprt_ctx; |
| 635 | } |
| 636 | |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 637 | SSL *ssl_sock_get_ssl_object(struct connection *conn) |
| 638 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 639 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 640 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 641 | return ctx ? ctx->ssl : NULL; |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 642 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 643 | /* |
| 644 | * This function gives the detail of the SSL error. It is used only |
| 645 | * if the debug mode and the verbose mode are activated. It dump all |
| 646 | * the SSL error until the stack was empty. |
| 647 | */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 648 | static forceinline void ssl_sock_dump_errors(struct connection *conn, |
| 649 | struct quic_conn *qc) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 650 | { |
| 651 | unsigned long ret; |
| 652 | |
| 653 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 654 | while(1) { |
Remi Tricot-Le Breton | 1effd9a | 2022-02-11 12:04:44 +0100 | [diff] [blame] | 655 | const char *func = NULL; |
| 656 | ERR_peek_error_func(&func); |
| 657 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 658 | ret = ERR_get_error(); |
| 659 | if (ret == 0) |
| 660 | return; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 661 | if (conn) { |
| 662 | fprintf(stderr, "fd[%#x] OpenSSL error[0x%lx] %s: %s\n", |
| 663 | conn_fd(conn), ret, |
| 664 | func, ERR_reason_error_string(ret)); |
| 665 | } |
| 666 | #ifdef USE_QUIC |
| 667 | else { |
| 668 | /* TODO: we are not sure <conn> is always initialized for QUIC connections */ |
| 669 | fprintf(stderr, "qc @%p OpenSSL error[0x%lx] %s: %s\n", qc, ret, |
| 670 | func, ERR_reason_error_string(ret)); |
| 671 | } |
| 672 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 673 | } |
| 674 | } |
| 675 | } |
| 676 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 677 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 678 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 679 | 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] | 680 | { |
| 681 | int err_code = ERR_ABORT; |
| 682 | ENGINE *engine; |
| 683 | struct ssl_engine_list *el; |
| 684 | |
| 685 | /* grab the structural reference to the engine */ |
| 686 | engine = ENGINE_by_id(engine_id); |
| 687 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 688 | 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] | 689 | goto fail_get; |
| 690 | } |
| 691 | |
| 692 | if (!ENGINE_init(engine)) { |
| 693 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 694 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 695 | goto fail_init; |
| 696 | } |
| 697 | |
| 698 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 699 | 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] | 700 | goto fail_set_method; |
| 701 | } |
| 702 | |
| 703 | el = calloc(1, sizeof(*el)); |
Remi Tricot-Le Breton | 612b2c3 | 2021-05-12 17:45:21 +0200 | [diff] [blame] | 704 | if (!el) |
| 705 | goto fail_alloc; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 706 | el->e = engine; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 707 | LIST_INSERT(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 708 | nb_engines++; |
| 709 | if (global_ssl.async) |
| 710 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 711 | return 0; |
| 712 | |
Remi Tricot-Le Breton | 612b2c3 | 2021-05-12 17:45:21 +0200 | [diff] [blame] | 713 | fail_alloc: |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 714 | fail_set_method: |
| 715 | /* release the functional reference from ENGINE_init() */ |
| 716 | ENGINE_finish(engine); |
| 717 | |
| 718 | fail_init: |
| 719 | /* release the structural reference from ENGINE_by_id() */ |
| 720 | ENGINE_free(engine); |
| 721 | |
| 722 | fail_get: |
| 723 | return err_code; |
| 724 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 725 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 726 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 727 | #ifdef HAVE_SSL_PROVIDERS |
| 728 | int ssl_init_provider(const char *provider_name) |
| 729 | { |
| 730 | int err_code = ERR_ABORT; |
| 731 | struct ssl_provider_list *prov = NULL; |
| 732 | |
| 733 | prov = calloc(1, sizeof(*prov)); |
| 734 | if (!prov) { |
| 735 | ha_alert("ssl-provider %s: memory allocation failure\n", provider_name); |
| 736 | goto error; |
| 737 | } |
| 738 | |
| 739 | if ((prov->provider = OSSL_PROVIDER_load(NULL, provider_name)) == NULL) { |
| 740 | ha_alert("ssl-provider %s: unknown provider\n", provider_name); |
| 741 | goto error; |
| 742 | } |
| 743 | |
| 744 | LIST_INSERT(&openssl_providers, &prov->list); |
| 745 | |
| 746 | return 0; |
| 747 | |
| 748 | error: |
| 749 | ha_free(&prov); |
| 750 | return err_code; |
| 751 | } |
| 752 | #endif /* HAVE_SSL_PROVIDERS */ |
| 753 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 754 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 755 | /* |
| 756 | * openssl async fd handler |
| 757 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 758 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 759 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 760 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 761 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 762 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 763 | * to poll this fd until it is requested |
| 764 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 765 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 766 | fd_cant_recv(fd); |
| 767 | |
| 768 | /* crypto engine is available, let's notify the associated |
| 769 | * connection that it can pursue its processing. |
| 770 | */ |
Olivier Houchard | a459826 | 2020-09-15 22:16:02 +0200 | [diff] [blame] | 771 | tasklet_wakeup(ctx->wait_event.tasklet); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 774 | /* |
| 775 | * openssl async delayed SSL_free handler |
| 776 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 777 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 778 | { |
| 779 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 780 | OSSL_ASYNC_FD all_fd[32]; |
| 781 | size_t num_all_fds = 0; |
| 782 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 783 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 784 | /* We suppose that the async job for a same SSL * |
| 785 | * are serialized. So if we are awake it is |
| 786 | * because the running job has just finished |
| 787 | * and we can remove all async fds safely |
| 788 | */ |
| 789 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 790 | if (num_all_fds > 32) { |
| 791 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 792 | return; |
| 793 | } |
| 794 | |
| 795 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 796 | for (i=0 ; i < num_all_fds ; i++) { |
| 797 | /* We want to remove the fd from the fdtab |
| 798 | * but we flag it to disown because the |
| 799 | * close is performed by the engine itself |
| 800 | */ |
| 801 | fdtab[all_fd[i]].state |= FD_DISOWN; |
| 802 | fd_delete(all_fd[i]); |
| 803 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 804 | |
| 805 | /* 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] | 806 | SSL_free(ssl); |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 807 | _HA_ATOMIC_DEC(&global.sslconns); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 808 | _HA_ATOMIC_DEC(&jobs); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 809 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 810 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 811 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 812 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 813 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 814 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 815 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 816 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 817 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 818 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 819 | size_t num_add_fds = 0; |
| 820 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 821 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 822 | |
| 823 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 824 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 825 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 826 | 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] | 827 | return; |
| 828 | } |
| 829 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 830 | 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] | 831 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 832 | /* We remove unused fds from the fdtab */ |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 833 | for (i=0 ; i < num_del_fds ; i++) { |
| 834 | /* We want to remove the fd from the fdtab |
| 835 | * but we flag it to disown because the |
| 836 | * close is performed by the engine itself |
| 837 | */ |
| 838 | fdtab[del_fd[i]].state |= FD_DISOWN; |
| 839 | fd_delete(del_fd[i]); |
| 840 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 841 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 842 | /* We add new fds to the fdtab */ |
| 843 | for (i=0 ; i < num_add_fds ; i++) { |
Willy Tarreau | 27a3245 | 2022-07-07 08:29:00 +0200 | [diff] [blame] | 844 | 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] | 845 | } |
| 846 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 847 | num_add_fds = 0; |
| 848 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 849 | if (num_add_fds > 32) { |
| 850 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 851 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 852 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 853 | |
| 854 | /* We activate the polling for all known async fds */ |
| 855 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 856 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 857 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 858 | /* To ensure that the fd cache won't be used |
| 859 | * We'll prefer to catch a real RD event |
| 860 | * because handling an EAGAIN on this fd will |
| 861 | * result in a context switch and also |
| 862 | * some engines uses a fd in blocking mode. |
| 863 | */ |
| 864 | fd_cant_recv(add_fd[i]); |
| 865 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 866 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 867 | } |
| 868 | #endif |
| 869 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 870 | |
| 871 | /* |
| 872 | * Initialize an HMAC context <hctx> using the <key> and <md> parameters. |
| 873 | * Returns -1 in case of error, 1 otherwise. |
| 874 | */ |
| 875 | static int ssl_hmac_init(MAC_CTX *hctx, unsigned char *key, int key_len, const EVP_MD *md) |
| 876 | { |
| 877 | #ifdef HAVE_OSSL_PARAM |
| 878 | OSSL_PARAM params[3]; |
| 879 | |
| 880 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, key, key_len); |
| 881 | params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, (char*)EVP_MD_name(md), 0); |
| 882 | params[2] = OSSL_PARAM_construct_end(); |
| 883 | if (EVP_MAC_CTX_set_params(hctx, params) == 0) |
| 884 | return -1; /* error in mac initialisation */ |
| 885 | |
| 886 | #else |
| 887 | HMAC_Init_ex(hctx, key, key_len, md, NULL); |
| 888 | #endif |
| 889 | return 1; |
| 890 | } |
| 891 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 892 | #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] | 893 | |
| 894 | 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] | 895 | { |
Frédéric Lécaille | 6aec1f3 | 2022-09-06 17:04:55 +0200 | [diff] [blame] | 896 | struct tls_keys_ref *ref = NULL; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 897 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 898 | int head; |
| 899 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 900 | int ret = -1; /* error by default */ |
Frédéric Lécaille | 6aec1f3 | 2022-09-06 17:04:55 +0200 | [diff] [blame] | 901 | struct connection *conn = SSL_get_ex_data(s, ssl_app_data_index); |
| 902 | #ifdef USE_QUIC |
| 903 | struct quic_conn *qc = SSL_get_ex_data(s, ssl_qc_app_data_index); |
| 904 | #endif |
| 905 | |
| 906 | if (conn) |
| 907 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
| 908 | #ifdef USE_QUIC |
| 909 | else if (qc) |
| 910 | ref = qc->li->bind_conf->keys_ref; |
| 911 | #endif |
| 912 | |
| 913 | if (!ref) { |
| 914 | /* must never happen */ |
| 915 | ABORT_NOW(); |
| 916 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 917 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 918 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 919 | |
| 920 | keys = ref->tlskeys; |
| 921 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 922 | |
| 923 | if (enc) { |
| 924 | memcpy(key_name, keys[head].name, 16); |
| 925 | |
| 926 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 927 | goto end; |
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 (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 930 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 931 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 932 | goto end; |
| 933 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 934 | if (ssl_hmac_init(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0) |
| 935 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 936 | ret = 1; |
| 937 | } |
| 938 | else if (ref->key_size_bits == 256 ) { |
| 939 | |
| 940 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 941 | goto end; |
| 942 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 943 | if (ssl_hmac_init(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0) |
| 944 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 945 | ret = 1; |
| 946 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 947 | } else { |
| 948 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 949 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 950 | goto found; |
| 951 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 952 | ret = 0; |
| 953 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 954 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 955 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 956 | if (ref->key_size_bits == 128) { |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 957 | if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0) |
| 958 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 959 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 960 | goto end; |
| 961 | /* 2 for key renewal, 1 if current key is still valid */ |
| 962 | ret = i ? 2 : 1; |
| 963 | } |
| 964 | else if (ref->key_size_bits == 256) { |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 965 | if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0) |
| 966 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 967 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 968 | goto end; |
| 969 | /* 2 for key renewal, 1 if current key is still valid */ |
| 970 | ret = i ? 2 : 1; |
| 971 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 972 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 973 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 974 | end: |
| 975 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 976 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 980 | { |
| 981 | struct tls_keys_ref *ref; |
| 982 | |
| 983 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 984 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 985 | return ref; |
| 986 | return NULL; |
| 987 | } |
| 988 | |
| 989 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 990 | { |
| 991 | struct tls_keys_ref *ref; |
| 992 | |
| 993 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 994 | if (ref->unique_id == unique_id) |
| 995 | return ref; |
| 996 | return NULL; |
| 997 | } |
| 998 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 999 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1000 | * match existing ones, this function returns -1 |
| 1001 | * else it returns 0 on success. |
| 1002 | */ |
| 1003 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1004 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1005 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1006 | if (ref->key_size_bits == 128) { |
| 1007 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1008 | return -1; |
| 1009 | } |
| 1010 | else if (ref->key_size_bits == 256) { |
| 1011 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1012 | return -1; |
| 1013 | } |
| 1014 | else |
| 1015 | return -1; |
| 1016 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1017 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1018 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1019 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1020 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1021 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1022 | |
| 1023 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1024 | } |
| 1025 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1026 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1027 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1028 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1029 | |
| 1030 | if(!ref) { |
| 1031 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1032 | return 1; |
| 1033 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1034 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1035 | memprintf(err, "Invalid key size"); |
| 1036 | return 1; |
| 1037 | } |
| 1038 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1039 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1040 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1041 | |
| 1042 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1043 | * automatic ids. It's called just after the basic checks. It returns |
| 1044 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1045 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1046 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1047 | { |
| 1048 | int i = 0; |
| 1049 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1050 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1051 | |
| 1052 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1053 | if (ref->unique_id == -1) { |
| 1054 | /* Look for the first free id. */ |
| 1055 | while (1) { |
| 1056 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1057 | if (ref2->unique_id == i) { |
| 1058 | i++; |
| 1059 | break; |
| 1060 | } |
| 1061 | } |
| 1062 | if (&ref2->list == &tlskeys_reference) |
| 1063 | break; |
| 1064 | } |
| 1065 | |
| 1066 | /* Uses the unique id and increment it for the next entry. */ |
| 1067 | ref->unique_id = i; |
| 1068 | i++; |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | /* This sort the reference list by id. */ |
| 1073 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1074 | LIST_DELETE(&ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1075 | list_for_each_entry(ref3, &tkr, list) { |
| 1076 | if (ref->unique_id < ref3->unique_id) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1077 | LIST_APPEND(&ref3->list, &ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1078 | break; |
| 1079 | } |
| 1080 | } |
| 1081 | if (&ref3->list == &tkr) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1082 | LIST_APPEND(&tkr, &ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | /* swap root */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1086 | LIST_INSERT(&tkr, &tlskeys_reference); |
| 1087 | LIST_DELETE(&tkr); |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1088 | return ERR_NONE; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1089 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1090 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1091 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1092 | |
Ilya Shipitsin | b3201a3 | 2020-10-18 09:11:50 +0500 | [diff] [blame] | 1093 | #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] | 1094 | /* |
| 1095 | * 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] | 1096 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1097 | * status extension, the issuer's certificate is mandatory. It should be |
| 1098 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1099 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1100 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1101 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1102 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1103 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1104 | * |
| 1105 | * 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] | 1106 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1107 | */ |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 1108 | 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] | 1109 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1110 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1111 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1112 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1113 | char *warn = NULL; |
| 1114 | unsigned char *p; |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1115 | #ifndef USE_OPENSSL_WOLFSSL |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1116 | void (*callback) (void); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1117 | #else |
| 1118 | tlsextStatusCb callback; |
| 1119 | #endif |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1120 | struct buffer *ocsp_uri = get_trash_chunk(); |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame] | 1121 | char *err = NULL; |
Willy Tarreau | 6d4c0c2 | 2023-04-07 17:49:37 +0200 | [diff] [blame] | 1122 | size_t path_len; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1123 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 1124 | x = data->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1125 | if (!x) |
| 1126 | goto out; |
| 1127 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame] | 1128 | ssl_ocsp_get_uri_from_cert(x, ocsp_uri, &err); |
| 1129 | /* We should have an "OCSP URI" field in order for auto update to work. */ |
| 1130 | if (data->ocsp_update_mode == SSL_SOCK_OCSP_UPDATE_ON && b_data(ocsp_uri) == 0) |
| 1131 | goto out; |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1132 | |
| 1133 | /* In case of ocsp update mode set to 'on', this function might be |
| 1134 | * called with no known ocsp response. If no ocsp uri can be found in |
| 1135 | * the certificate, nothing needs to be done here. */ |
| 1136 | if (!data->ocsp_response) { |
| 1137 | if (data->ocsp_update_mode != SSL_SOCK_OCSP_UPDATE_ON || b_data(ocsp_uri) == 0) { |
| 1138 | ret = 0; |
| 1139 | goto out; |
| 1140 | } |
| 1141 | } |
| 1142 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 1143 | issuer = data->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1144 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1145 | if (chain) { |
| 1146 | /* check if one of the certificate of the chain is the issuer */ |
| 1147 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1148 | X509 *ti = sk_X509_value(chain, i); |
| 1149 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1150 | issuer = ti; |
| 1151 | break; |
| 1152 | } |
| 1153 | } |
| 1154 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1155 | if (!issuer) |
| 1156 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1157 | |
Remi Tricot-Le Breton | 2d1daa8 | 2023-01-09 12:02:47 +0100 | [diff] [blame] | 1158 | if (!data->ocsp_cid) |
| 1159 | data->ocsp_cid = OCSP_cert_to_id(0, x, issuer); |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1160 | if (!data->ocsp_cid) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1161 | goto out; |
| 1162 | |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1163 | i = i2d_OCSP_CERTID(data->ocsp_cid, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1164 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1165 | goto out; |
| 1166 | |
Willy Tarreau | 6d4c0c2 | 2023-04-07 17:49:37 +0200 | [diff] [blame] | 1167 | path_len = strlen(path); |
| 1168 | ocsp = calloc(1, sizeof(*ocsp) + path_len + 1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1169 | if (!ocsp) |
| 1170 | goto out; |
| 1171 | |
| 1172 | p = ocsp->key_data; |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1173 | ocsp->key_length = i2d_OCSP_CERTID(data->ocsp_cid, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1174 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1175 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1176 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1177 | if (iocsp == ocsp) |
| 1178 | ocsp = NULL; |
| 1179 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1180 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1181 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1182 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1183 | #endif |
| 1184 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1185 | |
| 1186 | if (!callback) { |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1187 | struct ocsp_cbk_arg *cb_arg; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1188 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1189 | |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1190 | cb_arg = calloc(1, sizeof(*cb_arg)); |
| 1191 | if (!cb_arg) |
| 1192 | goto out; |
| 1193 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1194 | cb_arg->is_single = 1; |
| 1195 | cb_arg->s_ocsp = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1196 | iocsp->refcount++; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1197 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1198 | pkey = X509_get_pubkey(x); |
| 1199 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1200 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1201 | |
| 1202 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1203 | 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 */ |
| 1204 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1205 | } else { |
| 1206 | /* |
| 1207 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1208 | * Update that cb_arg with the new cert's staple |
| 1209 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1210 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1211 | struct certificate_ocsp *tmp_ocsp; |
| 1212 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1213 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1214 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1215 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1216 | cb_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1217 | |
| 1218 | /* |
| 1219 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1220 | * the order of operations below matter, take care when changing it |
| 1221 | */ |
| 1222 | tmp_ocsp = cb_arg->s_ocsp; |
| 1223 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1224 | cb_arg->s_ocsp = NULL; |
| 1225 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1226 | cb_arg->is_single = 0; |
| 1227 | cb_arg->single_kt = 0; |
| 1228 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1229 | pkey = X509_get_pubkey(x); |
| 1230 | key_type = EVP_PKEY_base_id(pkey); |
| 1231 | EVP_PKEY_free(pkey); |
| 1232 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1233 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1234 | if (index >= 0 && !cb_arg->m_ocsp[index]) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1235 | cb_arg->m_ocsp[index] = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1236 | iocsp->refcount++; |
| 1237 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1238 | } |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1239 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1240 | |
| 1241 | ret = 0; |
| 1242 | |
| 1243 | warn = NULL; |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1244 | 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] | 1245 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1246 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1247 | } |
| 1248 | |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1249 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame] | 1250 | /* Do not insert the same certificate_ocsp structure in the |
| 1251 | * update tree more than once. */ |
| 1252 | if (!ocsp) { |
| 1253 | /* Issuer certificate is not included in the certificate |
| 1254 | * chain, it will have to be treated separately during |
| 1255 | * ocsp response validation. */ |
| 1256 | if (issuer == data->ocsp_issuer) { |
| 1257 | iocsp->issuer = issuer; |
| 1258 | X509_up_ref(issuer); |
| 1259 | } |
| 1260 | if (data->chain) |
| 1261 | iocsp->chain = X509_chain_up_ref(data->chain); |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1262 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame] | 1263 | iocsp->uri = calloc(1, sizeof(*iocsp->uri)); |
| 1264 | if (!chunk_dup(iocsp->uri, ocsp_uri)) { |
| 1265 | ha_free(&iocsp->uri); |
| 1266 | goto out; |
| 1267 | } |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1268 | |
Willy Tarreau | 6d4c0c2 | 2023-04-07 17:49:37 +0200 | [diff] [blame] | 1269 | /* Note: if we arrive here, ocsp==NULL because iocsp==ocsp |
| 1270 | * after the ebmb_insert(), which indicates that we've |
| 1271 | * just inserted this new node and that it's the one for |
| 1272 | * which we previously allocated enough room for path_len+1 |
| 1273 | * chars. |
| 1274 | */ |
| 1275 | memcpy(iocsp->path, path, path_len + 1); |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 1276 | |
Remi Tricot-Le Breton | a6c0a59 | 2023-03-13 15:56:32 +0100 | [diff] [blame] | 1277 | if (data->ocsp_update_mode == SSL_SOCK_OCSP_UPDATE_ON) { |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1278 | ssl_ocsp_update_insert(iocsp); |
Remi Tricot-Le Breton | 86d1e0b | 2023-03-02 15:49:53 +0100 | [diff] [blame] | 1279 | /* If we are during init the update task is not |
| 1280 | * scheduled yet so a wakeup won't do anything. |
| 1281 | * Otherwise, if the OCSP was added through the CLI, we |
| 1282 | * wake the task up to manage the case of a new entry |
| 1283 | * that needs to be updated before the previous first |
| 1284 | * entry. |
| 1285 | */ |
| 1286 | if (ocsp_update_task) |
| 1287 | task_wakeup(ocsp_update_task, TASK_WOKEN_MSG); |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1288 | } |
| 1289 | } |
| 1290 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1291 | out: |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1292 | if (ret && data->ocsp_cid) |
| 1293 | OCSP_CERTID_free(data->ocsp_cid); |
| 1294 | |
| 1295 | if (!ret && data->ocsp_response) { |
| 1296 | ha_free(&data->ocsp_response->area); |
| 1297 | ha_free(&data->ocsp_response); |
| 1298 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1299 | |
| 1300 | if (ocsp) |
Remi Tricot-Le Breton | aa529f7 | 2022-11-03 15:16:49 +0100 | [diff] [blame] | 1301 | ssl_sock_free_ocsp(ocsp); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1302 | |
| 1303 | if (warn) |
| 1304 | free(warn); |
| 1305 | |
Tim Duesterhus | b39c24b | 2023-03-19 16:07:47 +0100 | [diff] [blame] | 1306 | free(err); |
| 1307 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1308 | return ret; |
| 1309 | } |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 1310 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 1311 | #endif |
| 1312 | |
| 1313 | #ifdef OPENSSL_IS_BORINGSSL |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 1314 | 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] | 1315 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1316 | 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] | 1317 | } |
| 1318 | #endif |
| 1319 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1320 | |
Ilya Shipitsin | 7bbf586 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 1321 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1322 | |
| 1323 | #define CT_EXTENSION_TYPE 18 |
| 1324 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1325 | int sctl_ex_index = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1326 | |
| 1327 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1328 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1329 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1330 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1331 | *out = (unsigned char *) sctl->area; |
| 1332 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1333 | |
| 1334 | return 1; |
| 1335 | } |
| 1336 | |
| 1337 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1338 | { |
| 1339 | return 1; |
| 1340 | } |
| 1341 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1342 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1343 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1344 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1345 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1346 | 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] | 1347 | goto out; |
| 1348 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1349 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1350 | |
| 1351 | ret = 0; |
| 1352 | |
| 1353 | out: |
| 1354 | return ret; |
| 1355 | } |
| 1356 | |
| 1357 | #endif |
| 1358 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1359 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1360 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1361 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1362 | #ifdef USE_QUIC |
| 1363 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1364 | #endif /* USE_QUIC */ |
| 1365 | struct ssl_sock_ctx *ctx = NULL; |
| 1366 | |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1367 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1368 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1369 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1370 | if (conn) |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 1371 | ctx = conn_get_ssl_sock_ctx(conn); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1372 | #ifdef USE_QUIC |
| 1373 | else if (qc) |
| 1374 | ctx = qc->xprt_ctx; |
| 1375 | #endif /* USE_QUIC */ |
Amaury Denoyelle | 7c564bf | 2022-01-24 11:04:05 +0100 | [diff] [blame] | 1376 | |
| 1377 | if (!ctx) { |
| 1378 | /* must never happen */ |
| 1379 | ABORT_NOW(); |
| 1380 | return; |
| 1381 | } |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1382 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1383 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1384 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1385 | * change this to #if and do not assign a default value to this macro! |
| 1386 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1387 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1388 | /* Disable renegotiation (CVE-2009-3555) */ |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1389 | 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] | 1390 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1391 | conn->err_code = CO_ER_SSL_RENEG; |
| 1392 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1393 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1394 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1395 | |
| 1396 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1397 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1398 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1399 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1400 | consider that the buffering was activated, |
| 1401 | so we rise the output buffer size from 4k |
| 1402 | to 16k */ |
| 1403 | write_bio = SSL_get_wbio(ssl); |
| 1404 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1405 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1406 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1407 | } |
| 1408 | } |
| 1409 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1410 | } |
| 1411 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1412 | /* Callback is called for each certificate of the chain during a verify |
| 1413 | ok is set to 1 if preverify detect no error on current certificate. |
| 1414 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1415 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1416 | { |
| 1417 | SSL *ssl; |
| 1418 | struct connection *conn; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1419 | struct ssl_sock_ctx *ctx = NULL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1420 | int err, depth; |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1421 | X509 *client_crt; |
| 1422 | STACK_OF(X509) *certs; |
Frédéric Lécaille | 48e46f9 | 2022-10-14 09:34:00 +0200 | [diff] [blame] | 1423 | struct bind_conf *bind_conf = NULL; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1424 | struct quic_conn *qc = NULL; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1425 | |
| 1426 | 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] | 1427 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1428 | client_crt = SSL_get_ex_data(ssl, ssl_client_crt_ref_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1429 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1430 | if (conn) { |
| 1431 | bind_conf = __objt_listener(conn->target)->bind_conf; |
| 1432 | ctx = __conn_get_ssl_sock_ctx(conn); |
| 1433 | } |
| 1434 | #ifdef USE_QUIC |
| 1435 | else { |
| 1436 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Amaury Denoyelle | ba303de | 2022-10-17 18:46:49 +0200 | [diff] [blame] | 1437 | BUG_ON(!qc); /* Must never happen */ |
| 1438 | bind_conf = qc->li->bind_conf; |
| 1439 | ctx = qc->xprt_ctx; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1440 | } |
| 1441 | #endif |
| 1442 | |
Frédéric Lécaille | 48e46f9 | 2022-10-14 09:34:00 +0200 | [diff] [blame] | 1443 | BUG_ON(!ctx || !bind_conf); |
Christopher Faulet | 881cce9 | 2022-11-23 09:27:13 +0100 | [diff] [blame] | 1444 | ALREADY_CHECKED(ctx); |
| 1445 | ALREADY_CHECKED(bind_conf); |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1446 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1447 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1448 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1449 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1450 | err = X509_STORE_CTX_get_error(x_store); |
| 1451 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1452 | if (ok) /* no errors */ |
| 1453 | return ok; |
| 1454 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1455 | /* Keep a reference to the client's certificate in order to be able to |
| 1456 | * dump some fetches values in a log even when the verification process |
| 1457 | * fails. */ |
| 1458 | if (depth == 0) { |
| 1459 | X509_free(client_crt); |
| 1460 | client_crt = X509_STORE_CTX_get0_cert(x_store); |
| 1461 | if (client_crt) { |
| 1462 | X509_up_ref(client_crt); |
| 1463 | SSL_set_ex_data(ssl, ssl_client_crt_ref_index, client_crt); |
| 1464 | } |
| 1465 | } |
| 1466 | else { |
| 1467 | /* An error occurred on a CA certificate of the certificate |
| 1468 | * chain, we might never call this verify callback on the client |
| 1469 | * certificate's depth (which is 0) so we try to store the |
| 1470 | * reference right now. */ |
Remi Tricot-Le Breton | f95c295 | 2021-08-20 09:51:23 +0200 | [diff] [blame] | 1471 | certs = X509_STORE_CTX_get1_chain(x_store); |
| 1472 | if (certs) { |
| 1473 | client_crt = sk_X509_value(certs, 0); |
| 1474 | if (client_crt) { |
| 1475 | X509_up_ref(client_crt); |
| 1476 | 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] | 1477 | } |
| 1478 | sk_X509_pop_free(certs, X509_free); |
| 1479 | } |
| 1480 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1481 | |
| 1482 | /* check if CA error needs to be ignored */ |
| 1483 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1484 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1485 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1486 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1487 | } |
| 1488 | |
Remi Tricot-Le Breton | 9b25982 | 2022-11-10 10:48:58 +0100 | [diff] [blame] | 1489 | if (err <= SSL_MAX_VFY_ERROR_CODE && |
William Lallemand | 4639689 | 2022-11-10 16:45:24 +0100 | [diff] [blame] | 1490 | 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] | 1491 | goto err_ignored; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1492 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1493 | /* TODO: for QUIC connection, this error code is lost */ |
| 1494 | if (conn) |
| 1495 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1496 | return 0; |
| 1497 | } |
| 1498 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1499 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1500 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1501 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1502 | /* check if certificate error needs to be ignored */ |
Remi Tricot-Le Breton | 9b25982 | 2022-11-10 10:48:58 +0100 | [diff] [blame] | 1503 | if (err <= SSL_MAX_VFY_ERROR_CODE && |
William Lallemand | 4639689 | 2022-11-10 16:45:24 +0100 | [diff] [blame] | 1504 | 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] | 1505 | goto err_ignored; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1506 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1507 | /* TODO: for QUIC connection, this error code is lost */ |
| 1508 | if (conn) |
| 1509 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1510 | return 0; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1511 | |
| 1512 | err_ignored: |
| 1513 | ssl_sock_dump_errors(conn, qc); |
| 1514 | ERR_clear_error(); |
| 1515 | return 1; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1516 | } |
| 1517 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1518 | #ifdef TLS1_RT_HEARTBEAT |
| 1519 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 1520 | int content_type, const void *buf, size_t len, |
| 1521 | SSL *ssl) |
| 1522 | { |
| 1523 | /* test heartbeat received (write_p is set to 0 |
| 1524 | for a received record) */ |
| 1525 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 1526 | struct ssl_sock_ctx *ctx = __conn_get_ssl_sock_ctx(conn); |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1527 | const unsigned char *p = buf; |
| 1528 | unsigned int payload; |
| 1529 | |
| 1530 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
| 1531 | |
| 1532 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1533 | if (*p != TLS1_HB_REQUEST) |
| 1534 | return; |
| 1535 | |
| 1536 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
| 1537 | goto kill_it; |
| 1538 | |
| 1539 | payload = (p[1] * 256) + p[2]; |
| 1540 | if (3 + payload + 16 <= len) |
| 1541 | return; /* OK no problem */ |
| 1542 | kill_it: |
| 1543 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1544 | * advertised payload is larger than the advertised packet |
| 1545 | * length, so we have garbage in the buffer between the |
| 1546 | * payload and the end of the buffer (p+len). We can't know |
| 1547 | * if the SSL stack is patched, and we don't know if we can |
| 1548 | * safely wipe out the area between p+3+len and payload. |
| 1549 | * So instead, we prevent the response from being sent by |
| 1550 | * setting the max_send_fragment to 0 and we report an SSL |
| 1551 | * error, which will kill this connection. It will be reported |
| 1552 | * above as SSL_ERROR_SSL while an other handshake failure with |
| 1553 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1554 | */ |
| 1555 | ssl->max_send_fragment = 0; |
| 1556 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1557 | } |
| 1558 | } |
| 1559 | #endif |
| 1560 | |
| 1561 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 1562 | int content_type, const void *buf, size_t len, |
| 1563 | SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1564 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1565 | struct ssl_capture *capture; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1566 | uchar *msg; |
| 1567 | uchar *end; |
| 1568 | uchar *extensions_end; |
| 1569 | uchar *ec_start = NULL; |
| 1570 | uchar *ec_formats_start = NULL; |
| 1571 | uchar *list_end; |
| 1572 | ushort protocol_version; |
| 1573 | ushort extension_id; |
| 1574 | ushort ec_len = 0; |
| 1575 | uchar ec_formats_len = 0; |
| 1576 | int offset = 0; |
| 1577 | int rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1578 | |
| 1579 | /* This function is called for "from client" and "to server" |
| 1580 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1581 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1582 | */ |
| 1583 | |
| 1584 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1585 | * otherwise it is set to 1. |
| 1586 | */ |
| 1587 | if (write_p != 0) |
| 1588 | return; |
| 1589 | |
| 1590 | /* content_type contains the type of message received or sent |
| 1591 | * according with the SSL/TLS protocol spec. This message is |
| 1592 | * encoded with one byte. The value 256 (two bytes) is used |
| 1593 | * for designing the SSL/TLS record layer. According with the |
| 1594 | * rfc6101, the expected message (other than 256) are: |
| 1595 | * - change_cipher_spec(20) |
| 1596 | * - alert(21) |
| 1597 | * - handshake(22) |
| 1598 | * - application_data(23) |
| 1599 | * - (255) |
| 1600 | * We are interessed by the handshake and specially the client |
| 1601 | * hello. |
| 1602 | */ |
| 1603 | if (content_type != 22) |
| 1604 | return; |
| 1605 | |
| 1606 | /* The message length is at least 4 bytes, containing the |
| 1607 | * message type and the message length. |
| 1608 | */ |
| 1609 | if (len < 4) |
| 1610 | return; |
| 1611 | |
| 1612 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1613 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1614 | * - hello_request(0) |
| 1615 | * - client_hello(1) |
| 1616 | * - server_hello(2) |
| 1617 | * - certificate(11) |
| 1618 | * - server_key_exchange (12) |
| 1619 | * - certificate_request(13) |
| 1620 | * - server_hello_done(14) |
| 1621 | * We are interested by the client hello. |
| 1622 | */ |
| 1623 | msg = (unsigned char *)buf; |
| 1624 | if (msg[0] != 1) |
| 1625 | return; |
| 1626 | |
| 1627 | /* Next three bytes are the length of the message. The total length |
| 1628 | * must be this decoded length + 4. If the length given as argument |
| 1629 | * is not the same, we abort the protocol dissector. |
| 1630 | */ |
| 1631 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1632 | if (len < rec_len + 4) |
| 1633 | return; |
| 1634 | msg += 4; |
| 1635 | end = msg + rec_len; |
| 1636 | if (end < msg) |
| 1637 | return; |
| 1638 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1639 | /* Expect 2 bytes for protocol version |
| 1640 | * (1 byte for major and 1 byte for minor) |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1641 | */ |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1642 | if (msg + 2 > end) |
| 1643 | return; |
| 1644 | protocol_version = (msg[0] << 8) + msg[1]; |
| 1645 | msg += 2; |
| 1646 | |
| 1647 | /* Expect the random, composed by 4 bytes for the unix time and |
| 1648 | * 28 bytes for unix payload. So we jump 4 + 28. |
| 1649 | */ |
| 1650 | msg += 4 + 28; |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1651 | if (msg > end) |
| 1652 | return; |
| 1653 | |
| 1654 | /* Next, is session id: |
| 1655 | * if present, we have to jump by length + 1 for the size information |
| 1656 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1657 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1658 | if (msg[0] > 0) |
| 1659 | msg += msg[0]; |
| 1660 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1661 | if (msg > end) |
| 1662 | return; |
| 1663 | |
| 1664 | /* Next two bytes are the ciphersuite length. */ |
| 1665 | if (msg + 2 > end) |
| 1666 | return; |
| 1667 | rec_len = (msg[0] << 8) + msg[1]; |
| 1668 | msg += 2; |
| 1669 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1670 | return; |
| 1671 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1672 | capture = pool_zalloc(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1673 | if (!capture) |
| 1674 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1675 | /* Compute the xxh64 of the ciphersuite. */ |
| 1676 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1677 | |
| 1678 | /* Capture the ciphersuite. */ |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1679 | capture->ciphersuite_len = MIN(global_ssl.capture_buffer_size, rec_len); |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1680 | capture->ciphersuite_offset = 0; |
| 1681 | memcpy(capture->data, msg, capture->ciphersuite_len); |
| 1682 | msg += rec_len; |
| 1683 | offset += capture->ciphersuite_len; |
| 1684 | |
| 1685 | /* Initialize other data */ |
| 1686 | capture->protocol_version = protocol_version; |
| 1687 | |
| 1688 | /* Next, compression methods: |
| 1689 | * if present, we have to jump by length + 1 for the size information |
| 1690 | * if not present, we have to jump by 1 only |
| 1691 | */ |
| 1692 | if (msg[0] > 0) |
| 1693 | msg += msg[0]; |
| 1694 | msg += 1; |
| 1695 | if (msg > end) |
| 1696 | goto store_capture; |
| 1697 | |
| 1698 | /* We reached extensions */ |
| 1699 | if (msg + 2 > end) |
| 1700 | goto store_capture; |
| 1701 | rec_len = (msg[0] << 8) + msg[1]; |
| 1702 | msg += 2; |
| 1703 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1704 | goto store_capture; |
| 1705 | extensions_end = msg + rec_len; |
| 1706 | capture->extensions_offset = offset; |
| 1707 | |
| 1708 | /* Parse each extension */ |
| 1709 | while (msg + 4 < extensions_end) { |
| 1710 | /* Add 2 bytes of extension_id */ |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1711 | if (global_ssl.capture_buffer_size >= offset + 2) { |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1712 | capture->data[offset++] = msg[0]; |
| 1713 | capture->data[offset++] = msg[1]; |
| 1714 | capture->extensions_len += 2; |
| 1715 | } |
| 1716 | else |
| 1717 | break; |
| 1718 | extension_id = (msg[0] << 8) + msg[1]; |
| 1719 | /* Length of the extension */ |
| 1720 | rec_len = (msg[2] << 8) + msg[3]; |
| 1721 | |
| 1722 | /* Expect 2 bytes extension id + 2 bytes extension size */ |
| 1723 | msg += 2 + 2; |
| 1724 | if (msg + rec_len > extensions_end || msg + rec_len < msg) |
| 1725 | goto store_capture; |
| 1726 | /* TLS Extensions |
| 1727 | * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */ |
| 1728 | if (extension_id == 0x000a) { |
| 1729 | /* Elliptic Curves: |
| 1730 | * https://www.rfc-editor.org/rfc/rfc8422.html |
| 1731 | * https://www.rfc-editor.org/rfc/rfc7919.html */ |
| 1732 | list_end = msg + rec_len; |
| 1733 | if (msg + 2 > list_end) |
| 1734 | goto store_capture; |
| 1735 | rec_len = (msg[0] << 8) + msg[1]; |
| 1736 | msg += 2; |
| 1737 | |
| 1738 | if (msg + rec_len > list_end || msg + rec_len < msg) |
| 1739 | goto store_capture; |
| 1740 | /* Store location/size of the list */ |
| 1741 | ec_start = msg; |
| 1742 | ec_len = rec_len; |
| 1743 | } |
| 1744 | else if (extension_id == 0x000b) { |
| 1745 | /* Elliptic Curves Point Formats: |
| 1746 | * https://www.rfc-editor.org/rfc/rfc8422.html */ |
| 1747 | list_end = msg + rec_len; |
| 1748 | if (msg + 1 > list_end) |
| 1749 | goto store_capture; |
| 1750 | rec_len = msg[0]; |
| 1751 | msg += 1; |
| 1752 | |
| 1753 | if (msg + rec_len > list_end || msg + rec_len < msg) |
| 1754 | goto store_capture; |
| 1755 | /* Store location/size of the list */ |
| 1756 | ec_formats_start = msg; |
| 1757 | ec_formats_len = rec_len; |
| 1758 | } |
| 1759 | msg += rec_len; |
| 1760 | } |
| 1761 | |
| 1762 | if (ec_start) { |
| 1763 | rec_len = ec_len; |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1764 | if (offset + rec_len > global_ssl.capture_buffer_size) |
| 1765 | rec_len = global_ssl.capture_buffer_size - offset; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1766 | memcpy(capture->data + offset, ec_start, rec_len); |
| 1767 | capture->ec_offset = offset; |
| 1768 | capture->ec_len = rec_len; |
| 1769 | offset += rec_len; |
| 1770 | } |
| 1771 | if (ec_formats_start) { |
| 1772 | rec_len = ec_formats_len; |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1773 | if (offset + rec_len > global_ssl.capture_buffer_size) |
| 1774 | rec_len = global_ssl.capture_buffer_size - offset; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1775 | memcpy(capture->data + offset, ec_formats_start, rec_len); |
| 1776 | capture->ec_formats_offset = offset; |
| 1777 | capture->ec_formats_len = rec_len; |
| 1778 | offset += rec_len; |
| 1779 | } |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1780 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1781 | store_capture: |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1782 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1783 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1784 | |
| 1785 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 1786 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1787 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 1788 | int content_type, const void *buf, size_t len, |
| 1789 | SSL *ssl) |
| 1790 | { |
| 1791 | struct ssl_keylog *keylog; |
| 1792 | |
| 1793 | if (SSL_get_ex_data(ssl, ssl_keylog_index)) |
| 1794 | return; |
| 1795 | |
Willy Tarreau | f208ac0 | 2021-03-22 21:10:12 +0100 | [diff] [blame] | 1796 | keylog = pool_zalloc(pool_head_ssl_keylog); |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1797 | if (!keylog) |
| 1798 | return; |
| 1799 | |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1800 | if (!SSL_set_ex_data(ssl, ssl_keylog_index, keylog)) { |
| 1801 | pool_free(pool_head_ssl_keylog, keylog); |
| 1802 | return; |
| 1803 | } |
| 1804 | } |
| 1805 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1806 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1807 | /* Callback is called for ssl protocol analyse */ |
| 1808 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1809 | { |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1810 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1811 | struct ssl_sock_msg_callback *cbk; |
| 1812 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1813 | /* Try to call all callback functions that were registered by using |
| 1814 | * ssl_sock_register_msg_callback(). |
| 1815 | */ |
| 1816 | list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) { |
| 1817 | cbk->func(conn, write_p, version, content_type, buf, len, ssl); |
| 1818 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1819 | } |
| 1820 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1821 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1822 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1823 | const unsigned char *in, unsigned int inlen, |
| 1824 | void *arg) |
| 1825 | { |
| 1826 | struct server *srv = arg; |
| 1827 | |
| 1828 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1829 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1830 | return SSL_TLSEXT_ERR_OK; |
| 1831 | return SSL_TLSEXT_ERR_NOACK; |
| 1832 | } |
| 1833 | #endif |
| 1834 | |
| 1835 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1836 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1837 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1838 | */ |
| 1839 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1840 | unsigned int *len, void *arg) |
| 1841 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1842 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1843 | |
| 1844 | *data = (const unsigned char *)conf->npn_str; |
| 1845 | *len = conf->npn_len; |
| 1846 | return SSL_TLSEXT_ERR_OK; |
| 1847 | } |
| 1848 | #endif |
| 1849 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1850 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1851 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1852 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1853 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1854 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1855 | unsigned char *outlen, |
| 1856 | const unsigned char *server, |
| 1857 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1858 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1859 | struct ssl_bind_conf *conf = arg; |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 1860 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1861 | 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] | 1862 | #endif |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1863 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1864 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1865 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1866 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1867 | if (qc) |
| 1868 | 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] | 1869 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1870 | return SSL_TLSEXT_ERR_NOACK; |
| 1871 | } |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 1872 | |
| 1873 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1874 | if (qc && !quic_set_app_ops(qc, *out, *outlen)) { |
| 1875 | 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] | 1876 | return SSL_TLSEXT_ERR_NOACK; |
| 1877 | } |
| 1878 | #endif |
| 1879 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1880 | return SSL_TLSEXT_ERR_OK; |
| 1881 | } |
| 1882 | #endif |
| 1883 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1884 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1885 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1886 | |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 1887 | /* Configure a DNS SAN extension on a certificate. */ |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1888 | int ssl_sock_add_san_ext(X509V3_CTX* ctx, X509* cert, const char *servername) { |
| 1889 | int failure = 0; |
| 1890 | X509_EXTENSION *san_ext = NULL; |
| 1891 | CONF *conf = NULL; |
| 1892 | struct buffer *san_name = get_trash_chunk(); |
| 1893 | |
| 1894 | conf = NCONF_new(NULL); |
| 1895 | if (!conf) { |
| 1896 | failure = 1; |
| 1897 | goto cleanup; |
| 1898 | } |
| 1899 | |
| 1900 | /* Build an extension based on the DNS entry above */ |
| 1901 | chunk_appendf(san_name, "DNS:%s", servername); |
| 1902 | san_ext = X509V3_EXT_nconf_nid(conf, ctx, NID_subject_alt_name, san_name->area); |
| 1903 | if (!san_ext) { |
| 1904 | failure = 1; |
| 1905 | goto cleanup; |
| 1906 | } |
| 1907 | |
| 1908 | /* Add the extension */ |
| 1909 | if (!X509_add_ext(cert, san_ext, -1 /* Add to end */)) { |
| 1910 | failure = 1; |
| 1911 | goto cleanup; |
| 1912 | } |
| 1913 | |
| 1914 | /* Success */ |
| 1915 | failure = 0; |
| 1916 | |
| 1917 | cleanup: |
| 1918 | if (NULL != san_ext) X509_EXTENSION_free(san_ext); |
| 1919 | if (NULL != conf) NCONF_free(conf); |
| 1920 | |
| 1921 | return failure; |
| 1922 | } |
| 1923 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1924 | /* Create a X509 certificate with the specified servername and serial. This |
| 1925 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1926 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1927 | 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] | 1928 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 1929 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
| 1930 | EVP_PKEY *capkey = bind_conf->ca_sign_ckch->key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1931 | SSL_CTX *ssl_ctx = NULL; |
| 1932 | X509 *newcrt = NULL; |
| 1933 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1934 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1935 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1936 | X509_NAME *name; |
| 1937 | const EVP_MD *digest; |
| 1938 | X509V3_CTX ctx; |
| 1939 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1940 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1941 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1942 | /* Get the private key of the default certificate and use it */ |
Ilya Shipitsin | af20488 | 2020-12-19 03:12:12 +0500 | [diff] [blame] | 1943 | #ifdef HAVE_SSL_CTX_get0_privatekey |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1944 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1945 | #else |
| 1946 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1947 | if (tmp_ssl) |
| 1948 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1949 | #endif |
| 1950 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1951 | goto mkcert_error; |
| 1952 | |
| 1953 | /* Create the certificate */ |
| 1954 | if (!(newcrt = X509_new())) |
| 1955 | goto mkcert_error; |
| 1956 | |
| 1957 | /* Set version number for the certificate (X509v3) and the serial |
| 1958 | * number */ |
| 1959 | if (X509_set_version(newcrt, 2L) != 1) |
| 1960 | goto mkcert_error; |
Willy Tarreau | 1db4273 | 2021-04-06 11:44:07 +0200 | [diff] [blame] | 1961 | 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] | 1962 | |
| 1963 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1964 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1965 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1966 | goto mkcert_error; |
| 1967 | |
| 1968 | /* set public key in the certificate */ |
| 1969 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1970 | goto mkcert_error; |
| 1971 | |
| 1972 | /* Set issuer name from the CA */ |
| 1973 | if (!(name = X509_get_subject_name(cacert))) |
| 1974 | goto mkcert_error; |
| 1975 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1976 | goto mkcert_error; |
| 1977 | |
| 1978 | /* Set the subject name using the same, but the CN */ |
| 1979 | name = X509_NAME_dup(name); |
| 1980 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1981 | (const unsigned char *)servername, |
| 1982 | -1, -1, 0) != 1) { |
| 1983 | X509_NAME_free(name); |
| 1984 | goto mkcert_error; |
| 1985 | } |
| 1986 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1987 | X509_NAME_free(name); |
| 1988 | goto mkcert_error; |
| 1989 | } |
| 1990 | X509_NAME_free(name); |
| 1991 | |
| 1992 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1993 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1994 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1995 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1996 | X509_EXTENSION *ext; |
| 1997 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1998 | 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] | 1999 | goto mkcert_error; |
| 2000 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 2001 | X509_EXTENSION_free(ext); |
| 2002 | goto mkcert_error; |
| 2003 | } |
| 2004 | X509_EXTENSION_free(ext); |
| 2005 | } |
| 2006 | |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 2007 | /* Add SAN extension */ |
| 2008 | if (ssl_sock_add_san_ext(&ctx, newcrt, servername)) { |
| 2009 | goto mkcert_error; |
| 2010 | } |
| 2011 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2012 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2013 | |
| 2014 | key_type = EVP_PKEY_base_id(capkey); |
| 2015 | |
| 2016 | if (key_type == EVP_PKEY_DSA) |
| 2017 | digest = EVP_sha1(); |
| 2018 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2019 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2020 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2021 | digest = EVP_sha256(); |
| 2022 | else { |
Ilya Shipitsin | ec36c91 | 2021-01-07 11:57:42 +0500 | [diff] [blame] | 2023 | #ifdef ASN1_PKEY_CTRL_DEFAULT_MD_NID |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2024 | int nid; |
| 2025 | |
| 2026 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 2027 | goto mkcert_error; |
| 2028 | if (!(digest = EVP_get_digestbynid(nid))) |
| 2029 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 2030 | #else |
| 2031 | goto mkcert_error; |
| 2032 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2033 | } |
| 2034 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2035 | if (!(X509_sign(newcrt, capkey, digest))) |
| 2036 | goto mkcert_error; |
| 2037 | |
| 2038 | /* Create and set the new SSL_CTX */ |
| 2039 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 2040 | goto mkcert_error; |
| 2041 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 2042 | goto mkcert_error; |
| 2043 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 2044 | goto mkcert_error; |
| 2045 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 2046 | goto mkcert_error; |
| 2047 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2048 | /* Build chaining the CA cert and the rest of the chain, keep these order */ |
| 2049 | #if defined(SSL_CTX_add1_chain_cert) |
| 2050 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, bind_conf->ca_sign_ckch->cert)) { |
| 2051 | goto mkcert_error; |
| 2052 | } |
| 2053 | |
| 2054 | if (bind_conf->ca_sign_ckch->chain) { |
| 2055 | for (i = 0; i < sk_X509_num(bind_conf->ca_sign_ckch->chain); i++) { |
| 2056 | X509 *chain_cert = sk_X509_value(bind_conf->ca_sign_ckch->chain, i); |
| 2057 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, chain_cert)) { |
| 2058 | goto mkcert_error; |
| 2059 | } |
| 2060 | } |
| 2061 | } |
| 2062 | #endif |
| 2063 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2064 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2065 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2066 | #ifndef OPENSSL_NO_DH |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 2067 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 2068 | 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] | 2069 | #else |
| 2070 | ssl_sock_set_tmp_dh_from_pkey(ssl_ctx, pkey); |
| 2071 | #endif |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2072 | #endif |
Remi Tricot-Le Breton | c11e7e1 | 2022-02-08 17:45:56 +0100 | [diff] [blame] | 2073 | |
| 2074 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 2075 | #if defined(SSL_CTX_set1_curves_list) |
| 2076 | { |
| 2077 | const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
| 2078 | if (!SSL_CTX_set1_curves_list(ssl_ctx, ecdhe)) |
| 2079 | goto end; |
| 2080 | } |
| 2081 | #endif |
| 2082 | #else |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2083 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2084 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2085 | 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] | 2086 | EC_KEY *ecc; |
| 2087 | int nid; |
| 2088 | |
| 2089 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2090 | goto end; |
| 2091 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2092 | goto end; |
| 2093 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2094 | EC_KEY_free(ecc); |
| 2095 | } |
Remi Tricot-Le Breton | c11e7e1 | 2022-02-08 17:45:56 +0100 | [diff] [blame] | 2096 | #endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */ |
| 2097 | #endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */ |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2098 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2099 | return ssl_ctx; |
| 2100 | |
| 2101 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2102 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2103 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2104 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2105 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2106 | return NULL; |
| 2107 | } |
| 2108 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2109 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2110 | /* 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] | 2111 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2112 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2113 | 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] | 2114 | { |
| 2115 | struct lru64 *lru = NULL; |
| 2116 | |
| 2117 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2118 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2119 | 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] | 2120 | if (lru && lru->domain) { |
| 2121 | if (ssl) |
| 2122 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2123 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2124 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2125 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2126 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2127 | } |
| 2128 | return NULL; |
| 2129 | } |
| 2130 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2131 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2132 | * function is not thread-safe, it should only be used to check if a certificate |
| 2133 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2134 | * thread). It is kept for backward compatibility. */ |
| 2135 | SSL_CTX * |
| 2136 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2137 | { |
| 2138 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2139 | } |
| 2140 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2141 | /* Set a certificate int the LRU cache used to store generated |
| 2142 | * certificate. Return 0 on success, otherwise -1 */ |
| 2143 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2144 | 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] | 2145 | { |
| 2146 | struct lru64 *lru = NULL; |
| 2147 | |
| 2148 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2149 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2150 | 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] | 2151 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2152 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2153 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2154 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2155 | if (lru->domain && lru->data) |
| 2156 | lru->free((SSL_CTX *)lru->data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2157 | 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] | 2158 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2159 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2160 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2161 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2162 | } |
| 2163 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2164 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2165 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2166 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2167 | { |
| 2168 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2169 | } |
| 2170 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2171 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2172 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2173 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2174 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2175 | 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] | 2176 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2177 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2178 | SSL_CTX *ssl_ctx = NULL; |
| 2179 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2180 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2181 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2182 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2183 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2184 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2185 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2186 | if (lru && lru->domain) |
| 2187 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2188 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2189 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2190 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2191 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2192 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2193 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2194 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2195 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2196 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2197 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2198 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2199 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2200 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2201 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2202 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2203 | return 0; |
| 2204 | } |
| 2205 | static int |
| 2206 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2207 | { |
| 2208 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2209 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2210 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2211 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2212 | 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] | 2213 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2214 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2215 | } |
| 2216 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2217 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2218 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2219 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2220 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2221 | |
| 2222 | 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] | 2223 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2224 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2225 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2226 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2227 | #endif |
| 2228 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2229 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2230 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2231 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2232 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2233 | 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] | 2234 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2235 | 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] | 2236 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2237 | #endif |
| 2238 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2239 | 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] | 2240 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2241 | 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] | 2242 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2243 | #endif |
| 2244 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2245 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2246 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2247 | /* Unusable in this context. */ |
| 2248 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2249 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2250 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2251 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2252 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2253 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2254 | |
| 2255 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2256 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2257 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2258 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2259 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2260 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2261 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2262 | } |
| 2263 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2264 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2265 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2266 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2267 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2268 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2269 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2270 | } |
| 2271 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2272 | 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] | 2273 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2274 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2275 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2276 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2277 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2278 | } |
| 2279 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2280 | 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] | 2281 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2282 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2283 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2284 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2285 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2286 | } |
| 2287 | 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] | 2288 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2289 | 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] | 2290 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2291 | #endif |
| 2292 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2293 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
William Lallemand | f22b032 | 2021-06-02 16:09:11 +0200 | [diff] [blame] | 2294 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2295 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2296 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2297 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2298 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2299 | #endif |
| 2300 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2301 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2302 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 2303 | struct methodVersions methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2304 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2305 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2306 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2307 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2308 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2309 | {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] | 2310 | }; |
| 2311 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2312 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2313 | { |
| 2314 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2315 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2316 | SSL_set_SSL_CTX(ssl, ctx); |
| 2317 | } |
| 2318 | |
Ilya Shipitsin | 1fc44d4 | 2021-01-23 00:09:14 +0500 | [diff] [blame] | 2319 | #ifdef HAVE_SSL_CLIENT_HELLO_CB |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2320 | |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2321 | int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2322 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2323 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2324 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2325 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2326 | 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] | 2327 | return SSL_TLSEXT_ERR_OK; |
| 2328 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2329 | } |
| 2330 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2331 | #ifdef OPENSSL_IS_BORINGSSL |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2332 | int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2333 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2334 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2335 | #else |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2336 | int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2337 | { |
| 2338 | #endif |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2339 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 2340 | #ifdef USE_QUIC |
| 2341 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 2342 | #endif /* USE_QUIC */ |
| 2343 | struct bind_conf *s = NULL; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2344 | const uint8_t *extension_data; |
| 2345 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2346 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2347 | |
| 2348 | char *wildp = NULL; |
| 2349 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2350 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2351 | 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] | 2352 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2353 | int i; |
| 2354 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2355 | if (conn) |
| 2356 | s = __objt_listener(conn->target)->bind_conf; |
| 2357 | #ifdef USE_QUIC |
| 2358 | else if (qc) |
| 2359 | s = qc->li->bind_conf; |
| 2360 | #endif /* USE_QUIC */ |
Amaury Denoyelle | 7c564bf | 2022-01-24 11:04:05 +0100 | [diff] [blame] | 2361 | |
| 2362 | if (!s) { |
| 2363 | /* must never happen */ |
| 2364 | ABORT_NOW(); |
| 2365 | return 0; |
| 2366 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2367 | |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2368 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2369 | if (qc) { |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2370 | /* Look for the QUIC transport parameters. */ |
| 2371 | #ifdef OPENSSL_IS_BORINGSSL |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2372 | 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] | 2373 | &extension_data, &extension_len)) |
| 2374 | #else |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2375 | 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] | 2376 | &extension_data, &extension_len)) |
| 2377 | #endif |
Frédéric Lécaille | b5b5247 | 2021-11-22 15:55:16 +0100 | [diff] [blame] | 2378 | { |
| 2379 | /* This is not redundant. It we only return 0 without setting |
| 2380 | * <*al>, this has as side effect to generate another TLS alert |
| 2381 | * which would be set after calling quic_set_tls_alert(). |
| 2382 | */ |
| 2383 | *al = SSL_AD_MISSING_EXTENSION; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2384 | quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION); |
Frédéric Lécaille | b5b5247 | 2021-11-22 15:55:16 +0100 | [diff] [blame] | 2385 | return 0; |
| 2386 | } |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2387 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2388 | if (!quic_transport_params_store(qc, 0, extension_data, |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2389 | extension_data + extension_len)) |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2390 | goto abort; |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2391 | |
| 2392 | qc->flags |= QUIC_FL_CONN_TX_TP_RECEIVED; |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2393 | } |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2394 | #endif /* USE_QUIC */ |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2395 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2396 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2397 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2398 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2399 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2400 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2401 | #else |
| 2402 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2403 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2404 | /* |
| 2405 | * The server_name extension was given too much extensibility when it |
| 2406 | * was written, so parsing the normal case is a bit complex. |
| 2407 | */ |
| 2408 | size_t len; |
| 2409 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2410 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2411 | /* Extract the length of the supplied list of names. */ |
| 2412 | len = (*extension_data++) << 8; |
| 2413 | len |= *extension_data++; |
| 2414 | if (len + 2 != extension_len) |
| 2415 | goto abort; |
| 2416 | /* |
| 2417 | * The list in practice only has a single element, so we only consider |
| 2418 | * the first one. |
| 2419 | */ |
| 2420 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2421 | goto abort; |
| 2422 | extension_len = len - 1; |
| 2423 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2424 | if (extension_len <= 2) |
| 2425 | goto abort; |
| 2426 | len = (*extension_data++) << 8; |
| 2427 | len |= *extension_data++; |
| 2428 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2429 | || memchr(extension_data, 0, len) != NULL) |
| 2430 | goto abort; |
| 2431 | servername = extension_data; |
| 2432 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2433 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2434 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2435 | 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] | 2436 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2437 | } |
| 2438 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2439 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2440 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2441 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2442 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2443 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2444 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2445 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2446 | goto abort; |
| 2447 | } |
| 2448 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2449 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2450 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2451 | 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] | 2452 | #else |
| 2453 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2454 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2455 | uint8_t sign; |
| 2456 | size_t len; |
| 2457 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2458 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2459 | len = (*extension_data++) << 8; |
| 2460 | len |= *extension_data++; |
| 2461 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2462 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2463 | if (len % 2 != 0) |
| 2464 | goto abort; |
| 2465 | for (; len > 0; len -= 2) { |
| 2466 | extension_data++; /* hash */ |
| 2467 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2468 | switch (sign) { |
| 2469 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2470 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2471 | break; |
| 2472 | case TLSEXT_signature_ecdsa: |
| 2473 | has_ecdsa_sig = 1; |
| 2474 | break; |
| 2475 | default: |
| 2476 | continue; |
| 2477 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2478 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2479 | break; |
| 2480 | } |
| 2481 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2482 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2483 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2484 | } |
| 2485 | 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] | 2486 | const SSL_CIPHER *cipher; |
| 2487 | size_t len; |
| 2488 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2489 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2490 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2491 | len = ctx->cipher_suites_len; |
| 2492 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2493 | #else |
| 2494 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2495 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2496 | if (len % 2 != 0) |
| 2497 | goto abort; |
| 2498 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2499 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2500 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2501 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2502 | #else |
| 2503 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2504 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2505 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2506 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2507 | break; |
| 2508 | } |
| 2509 | } |
| 2510 | } |
| 2511 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2512 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2513 | trash.area[i] = tolower(servername[i]); |
| 2514 | if (!wildp && (trash.area[i] == '.')) |
| 2515 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2516 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2517 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2518 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2519 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2520 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2521 | /* Look for an ECDSA, RSA and DSA certificate, first in the single |
| 2522 | * name and if not found in the wildcard */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2523 | for (i = 0; i < 2; i++) { |
| 2524 | if (i == 0) /* lookup in full qualified names */ |
| 2525 | node = ebst_lookup(&s->sni_ctx, trash.area); |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2526 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2527 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2528 | else |
| 2529 | break; |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2530 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2531 | for (n = node; n; n = ebmb_next_dup(n)) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2532 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2533 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2534 | if (!container_of(n, struct sni_ctx, name)->neg) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2535 | struct sni_ctx *sni, *sni_tmp; |
| 2536 | int skip = 0; |
| 2537 | |
| 2538 | if (i == 1 && wildp) { /* wildcard */ |
| 2539 | /* If this is a wildcard, look for an exclusion on the same crt-list line */ |
| 2540 | sni = container_of(n, struct sni_ctx, name); |
| 2541 | 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] | 2542 | 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] | 2543 | skip = 1; |
| 2544 | break; |
| 2545 | } |
| 2546 | } |
| 2547 | if (skip) |
| 2548 | continue; |
| 2549 | } |
| 2550 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2551 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2552 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2553 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2554 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2555 | break; |
| 2556 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2557 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2558 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2559 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2560 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2561 | if (!node_anonymous) |
| 2562 | node_anonymous = n; |
| 2563 | break; |
| 2564 | } |
| 2565 | } |
| 2566 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2567 | } |
| 2568 | /* Once the certificates are found, select them depending on what is |
| 2569 | * supported in the client and by key_signature priority order: EDSA > |
| 2570 | * RSA > DSA */ |
William Lallemand | 5b1d1f6 | 2020-08-14 15:30:13 +0200 | [diff] [blame] | 2571 | if (has_ecdsa_sig && node_ecdsa) |
| 2572 | node = node_ecdsa; |
| 2573 | else if (has_rsa_sig && node_rsa) |
| 2574 | node = node_rsa; |
| 2575 | else if (node_anonymous) |
| 2576 | node = node_anonymous; |
| 2577 | else if (node_ecdsa) |
| 2578 | node = node_ecdsa; /* no ecdsa signature case (< TLSv1.2) */ |
| 2579 | else |
| 2580 | node = node_rsa; /* no rsa signature case (far far away) */ |
| 2581 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2582 | if (node) { |
| 2583 | /* switch ctx */ |
| 2584 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2585 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 2586 | if (conf) { |
| 2587 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2588 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2589 | if (conf->early_data) |
| 2590 | allow_early = 1; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2591 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2592 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
| 2593 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2594 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2595 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2596 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2597 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2598 | 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] | 2599 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2600 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2601 | } |
| 2602 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2603 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2604 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2605 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2606 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2607 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2608 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2609 | } |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2610 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 2611 | /* We are about to raise an handshake error so the servername extension |
| 2612 | * callback will never be called and the SNI will never be stored in the |
| 2613 | * SSL context. In order for the ssl_fc_sni sample fetch to still work |
| 2614 | * in such a case, we store the SNI ourselves as an ex_data information |
| 2615 | * in the SSL context. |
| 2616 | */ |
| 2617 | { |
| 2618 | char *client_sni = pool_alloc(ssl_sock_client_sni_pool); |
| 2619 | if (client_sni) { |
| 2620 | strncpy(client_sni, trash.area, TLSEXT_MAXLEN_host_name); |
| 2621 | client_sni[TLSEXT_MAXLEN_host_name] = '\0'; |
| 2622 | SSL_set_ex_data(ssl, ssl_client_sni_index, client_sni); |
| 2623 | } |
| 2624 | } |
| 2625 | |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2626 | /* other cases fallback on abort, if strict-sni is set but no node was found */ |
| 2627 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2628 | abort: |
| 2629 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2630 | if (conn) |
| 2631 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2632 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2633 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2634 | #else |
| 2635 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2636 | return 0; |
| 2637 | #endif |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2638 | |
| 2639 | allow_early: |
| 2640 | #ifdef OPENSSL_IS_BORINGSSL |
| 2641 | if (allow_early) |
| 2642 | SSL_set_early_data_enabled(ssl, 1); |
| 2643 | #else |
| 2644 | if (!allow_early) |
| 2645 | SSL_set_max_early_data(ssl, 0); |
| 2646 | #endif |
| 2647 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2648 | } |
| 2649 | |
William Lallemand | 002e206 | 2021-11-18 15:25:16 +0100 | [diff] [blame] | 2650 | #else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2651 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2652 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2653 | * warning when no match is found, which implies the default (first) cert |
| 2654 | * will keep being used. |
| 2655 | */ |
William Lallemand | 844009d | 2022-09-02 15:27:32 +0200 | [diff] [blame] | 2656 | int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2657 | { |
| 2658 | const char *servername; |
| 2659 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2660 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2661 | struct bind_conf *s = priv; |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2662 | #ifdef USE_QUIC |
| 2663 | const uint8_t *extension_data; |
| 2664 | size_t extension_len; |
| 2665 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 2666 | #endif /* USE_QUIC */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2667 | int i; |
| 2668 | (void)al; /* shut gcc stupid warning */ |
| 2669 | |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2670 | #ifdef USE_QUIC |
| 2671 | if (qc) { |
| 2672 | |
| 2673 | /* Look for the QUIC transport parameters. */ |
| 2674 | SSL_get_peer_quic_transport_params(ssl, &extension_data, &extension_len); |
| 2675 | if (extension_len == 0) { |
| 2676 | /* This is not redundant. It we only return 0 without setting |
| 2677 | * <*al>, this has as side effect to generate another TLS alert |
| 2678 | * which would be set after calling quic_set_tls_alert(). |
| 2679 | */ |
| 2680 | *al = SSL_AD_MISSING_EXTENSION; |
| 2681 | quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION); |
| 2682 | return SSL_TLSEXT_ERR_NOACK; |
| 2683 | } |
| 2684 | |
| 2685 | if (!quic_transport_params_store(qc, 0, extension_data, |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2686 | extension_data + extension_len)) |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2687 | return SSL_TLSEXT_ERR_NOACK; |
Frédéric Lécaille | af25a69 | 2023-02-01 17:56:57 +0100 | [diff] [blame] | 2688 | |
| 2689 | qc->flags |= QUIC_FL_CONN_TX_TP_RECEIVED; |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2690 | } |
| 2691 | #endif /* USE_QUIC */ |
| 2692 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2693 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2694 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2695 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2696 | 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] | 2697 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2698 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2699 | if (s->strict_sni) |
| 2700 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2701 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2702 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2703 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2704 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2705 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2706 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2707 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2708 | if (!servername[i]) |
| 2709 | break; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 2710 | trash.area[i] = tolower((unsigned char)servername[i]); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2711 | if (!wildp && (trash.area[i] == '.')) |
| 2712 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2713 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2714 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2715 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2716 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2717 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2718 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2719 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2720 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2721 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2722 | node = n; |
| 2723 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2724 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2725 | } |
| 2726 | if (!node && wildp) { |
| 2727 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2728 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2729 | /* lookup a not neg filter */ |
| 2730 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2731 | node = n; |
| 2732 | break; |
| 2733 | } |
| 2734 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2735 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2736 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2737 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2738 | 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] | 2739 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2740 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2741 | return SSL_TLSEXT_ERR_OK; |
| 2742 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2743 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2744 | if (s->strict_sni) { |
| 2745 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2746 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2747 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2748 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2749 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2750 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2751 | } |
| 2752 | |
| 2753 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2754 | 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] | 2755 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2756 | return SSL_TLSEXT_ERR_OK; |
| 2757 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2758 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2759 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2760 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2761 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2762 | |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2763 | static inline HASSL_DH *ssl_new_dh_fromdata(BIGNUM *p, BIGNUM *g) |
| 2764 | { |
| 2765 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 2766 | OSSL_PARAM_BLD *tmpl = NULL; |
| 2767 | OSSL_PARAM *params = NULL; |
| 2768 | EVP_PKEY_CTX *ctx = NULL; |
| 2769 | EVP_PKEY *pkey = NULL; |
| 2770 | |
| 2771 | if ((tmpl = OSSL_PARAM_BLD_new()) == NULL |
| 2772 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
| 2773 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g) |
| 2774 | || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
| 2775 | goto end; |
| 2776 | } |
| 2777 | ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 2778 | if (ctx == NULL |
| 2779 | || !EVP_PKEY_fromdata_init(ctx) |
| 2780 | || !EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params)) { |
| 2781 | goto end; |
| 2782 | } |
| 2783 | |
| 2784 | end: |
| 2785 | EVP_PKEY_CTX_free(ctx); |
| 2786 | OSSL_PARAM_free(params); |
| 2787 | OSSL_PARAM_BLD_free(tmpl); |
Remi Tricot-Le Breton | a2c21db | 2022-11-03 15:16:47 +0100 | [diff] [blame] | 2788 | BN_free(p); |
| 2789 | BN_free(g); |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2790 | return pkey; |
| 2791 | #else |
| 2792 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2793 | HASSL_DH *dh = DH_new(); |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2794 | |
| 2795 | if (!dh) |
| 2796 | return NULL; |
| 2797 | |
| 2798 | DH_set0_pqg(dh, p, NULL, g); |
| 2799 | |
| 2800 | return dh; |
| 2801 | #endif |
| 2802 | } |
| 2803 | |
Remi Tricot-Le Breton | c69be7c | 2022-04-20 18:30:17 +0200 | [diff] [blame] | 2804 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2805 | static inline HASSL_DH *ssl_get_dh_by_nid(int nid) |
| 2806 | { |
| 2807 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 2808 | OSSL_PARAM params[2]; |
| 2809 | EVP_PKEY *pkey = NULL; |
| 2810 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 2811 | const char *named_group = NULL; |
| 2812 | |
| 2813 | if (!pctx) |
| 2814 | goto end; |
| 2815 | |
| 2816 | named_group = OBJ_nid2ln(nid); |
| 2817 | |
| 2818 | if (!named_group) |
| 2819 | goto end; |
| 2820 | |
| 2821 | params[0] = OSSL_PARAM_construct_utf8_string("group", (char*)named_group, 0); |
| 2822 | params[1] = OSSL_PARAM_construct_end(); |
| 2823 | |
| 2824 | if (EVP_PKEY_keygen_init(pctx) && EVP_PKEY_CTX_set_params(pctx, params)) |
| 2825 | EVP_PKEY_generate(pctx, &pkey); |
| 2826 | |
| 2827 | end: |
| 2828 | EVP_PKEY_CTX_free(pctx); |
| 2829 | return pkey; |
| 2830 | #else |
| 2831 | |
| 2832 | HASSL_DH *dh = NULL; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2833 | dh = DH_new_by_nid(nid); |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2834 | return dh; |
| 2835 | #endif |
| 2836 | } |
Remi Tricot-Le Breton | c69be7c | 2022-04-20 18:30:17 +0200 | [diff] [blame] | 2837 | #endif |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2838 | |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2839 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2840 | static HASSL_DH * ssl_get_dh_1024(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2841 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2842 | static unsigned char dh1024_p[]={ |
| 2843 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2844 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2845 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2846 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2847 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2848 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2849 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2850 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2851 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2852 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2853 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2854 | }; |
| 2855 | static unsigned char dh1024_g[]={ |
| 2856 | 0x02, |
| 2857 | }; |
| 2858 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2859 | BIGNUM *p; |
| 2860 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2861 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2862 | HASSL_DH *dh = NULL; |
| 2863 | |
| 2864 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2865 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
| 2866 | |
| 2867 | if (p && g) |
| 2868 | dh = ssl_new_dh_fromdata(p, g); |
| 2869 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2870 | return dh; |
| 2871 | } |
| 2872 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2873 | static HASSL_DH *ssl_get_dh_2048(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2874 | { |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2875 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2876 | static unsigned char dh2048_p[]={ |
| 2877 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2878 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2879 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2880 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2881 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2882 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2883 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2884 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2885 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2886 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2887 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2888 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2889 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2890 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2891 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2892 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2893 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2894 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2895 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2896 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2897 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2898 | 0xB7,0x1F,0x77,0xF3, |
| 2899 | }; |
| 2900 | static unsigned char dh2048_g[]={ |
| 2901 | 0x02, |
| 2902 | }; |
| 2903 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2904 | BIGNUM *p; |
| 2905 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2906 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2907 | HASSL_DH *dh = NULL; |
| 2908 | |
| 2909 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2910 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
| 2911 | |
| 2912 | if (p && g) |
| 2913 | dh = ssl_new_dh_fromdata(p, g); |
| 2914 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2915 | return dh; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2916 | #else |
| 2917 | return ssl_get_dh_by_nid(NID_ffdhe2048); |
| 2918 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2919 | } |
| 2920 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2921 | static HASSL_DH *ssl_get_dh_4096(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2922 | { |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2923 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2924 | static unsigned char dh4096_p[]={ |
| 2925 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2926 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2927 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2928 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2929 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2930 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2931 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2932 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2933 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2934 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2935 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2936 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2937 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2938 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2939 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2940 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2941 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2942 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2943 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2944 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2945 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2946 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2947 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2948 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2949 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2950 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2951 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2952 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2953 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2954 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2955 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2956 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2957 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2958 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2959 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2960 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2961 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2962 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2963 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2964 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2965 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2966 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2967 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2968 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2969 | static unsigned char dh4096_g[]={ |
| 2970 | 0x02, |
| 2971 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2972 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2973 | BIGNUM *p; |
| 2974 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2975 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2976 | HASSL_DH *dh = NULL; |
| 2977 | |
| 2978 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2979 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
| 2980 | |
| 2981 | if (p && g) |
| 2982 | dh = ssl_new_dh_fromdata(p, g); |
| 2983 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2984 | return dh; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2985 | #else |
| 2986 | return ssl_get_dh_by_nid(NID_ffdhe4096); |
| 2987 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2988 | } |
| 2989 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2990 | static HASSL_DH *ssl_get_tmp_dh(EVP_PKEY *pkey) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2991 | { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2992 | HASSL_DH *dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2993 | int type; |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 2994 | int keylen = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2995 | |
| 2996 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2997 | |
Ilya Shipitsin | 52f2ff5 | 2022-07-23 23:55:19 +0500 | [diff] [blame] | 2998 | if (type == EVP_PKEY_EC) { |
| 2999 | keylen = global_ssl.default_dh_param; |
| 3000 | } |
| 3001 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3002 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 3003 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 3004 | */ |
| 3005 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 3006 | keylen = EVP_PKEY_bits(pkey); |
| 3007 | } |
| 3008 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3009 | if (keylen > global_ssl.default_dh_param) { |
| 3010 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3011 | } |
| 3012 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3013 | if (keylen >= 4096) { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3014 | if (!local_dh_4096) |
| 3015 | local_dh_4096 = ssl_get_dh_4096(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3016 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3017 | } |
| 3018 | else if (keylen >= 2048) { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3019 | if (!local_dh_2048) |
| 3020 | local_dh_2048 = ssl_get_dh_2048(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3021 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3022 | } |
| 3023 | else { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3024 | if (!local_dh_1024) |
| 3025 | local_dh_1024 = ssl_get_dh_1024(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3026 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | return dh; |
| 3030 | } |
| 3031 | |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3032 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3033 | /* Returns Diffie-Hellman parameters matching the private key length |
| 3034 | but not exceeding global_ssl.default_dh_param */ |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3035 | 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] | 3036 | { |
| 3037 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
| 3038 | |
| 3039 | return ssl_get_tmp_dh(pkey); |
| 3040 | } |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3041 | #endif |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3042 | |
Remi Tricot-Le Breton | 846eda9 | 2022-02-11 12:04:50 +0100 | [diff] [blame] | 3043 | static int ssl_sock_set_tmp_dh(SSL_CTX *ctx, HASSL_DH *dh) |
| 3044 | { |
| 3045 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
| 3046 | return SSL_CTX_set_tmp_dh(ctx, dh); |
| 3047 | #else |
| 3048 | int retval = 0; |
| 3049 | HASSL_DH_up_ref(dh); |
| 3050 | |
| 3051 | retval = SSL_CTX_set0_tmp_dh_pkey(ctx, dh); |
| 3052 | |
| 3053 | if (!retval) |
| 3054 | HASSL_DH_free(dh); |
| 3055 | |
| 3056 | return retval; |
| 3057 | #endif |
| 3058 | } |
| 3059 | |
Remi Tricot-Le Breton | 5f17930 | 2022-02-11 12:04:51 +0100 | [diff] [blame] | 3060 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3061 | static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey) |
| 3062 | { |
| 3063 | HASSL_DH *dh = NULL; |
| 3064 | if (pkey && (dh = ssl_get_tmp_dh(pkey))) { |
| 3065 | HASSL_DH_up_ref(dh); |
| 3066 | if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dh)) |
| 3067 | HASSL_DH_free(dh); |
| 3068 | } |
Remi Tricot-Le Breton | 5f17930 | 2022-02-11 12:04:51 +0100 | [diff] [blame] | 3069 | } |
| 3070 | #endif |
| 3071 | |
Remi Tricot-Le Breton | 09ebb33 | 2022-02-11 12:04:48 +0100 | [diff] [blame] | 3072 | HASSL_DH *ssl_sock_get_dh_from_bio(BIO *bio) |
| 3073 | { |
| 3074 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3075 | HASSL_DH *dh = NULL; |
| 3076 | OSSL_DECODER_CTX *dctx = NULL; |
| 3077 | const char *format = "PEM"; |
| 3078 | const char *keytype = "DH"; |
| 3079 | |
| 3080 | dctx = OSSL_DECODER_CTX_new_for_pkey(&dh, format, NULL, keytype, |
| 3081 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
| 3082 | NULL, NULL); |
| 3083 | |
| 3084 | if (dctx == NULL || OSSL_DECODER_CTX_get_num_decoders(dctx) == 0) |
| 3085 | goto end; |
| 3086 | |
| 3087 | /* The DH parameters might not be the first section found in the PEM |
| 3088 | * file so we need to iterate over all of them until we find the right |
| 3089 | * one. |
| 3090 | */ |
| 3091 | while (!BIO_eof(bio) && !dh) |
| 3092 | OSSL_DECODER_from_bio(dctx, bio); |
| 3093 | |
| 3094 | end: |
| 3095 | OSSL_DECODER_CTX_free(dctx); |
| 3096 | return dh; |
| 3097 | #else |
| 3098 | HASSL_DH *dh = NULL; |
| 3099 | |
| 3100 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 3101 | |
| 3102 | return dh; |
| 3103 | #endif |
| 3104 | } |
| 3105 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3106 | static HASSL_DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3107 | { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3108 | HASSL_DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3109 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3110 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3111 | if (in == NULL) |
| 3112 | goto end; |
| 3113 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3114 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3115 | goto end; |
| 3116 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3117 | dh = ssl_sock_get_dh_from_bio(in); |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3118 | |
| 3119 | end: |
| 3120 | if (in) |
| 3121 | BIO_free(in); |
| 3122 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 3123 | ERR_clear_error(); |
| 3124 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3125 | return dh; |
| 3126 | } |
| 3127 | |
| 3128 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 3129 | { |
| 3130 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 3131 | |
| 3132 | if (global_dh) { |
| 3133 | return 0; |
| 3134 | } |
| 3135 | |
| 3136 | return -1; |
| 3137 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3138 | #endif |
| 3139 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3140 | /* 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] | 3141 | 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] | 3142 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 3143 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3144 | { |
| 3145 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3146 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3147 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3148 | if (*name == '!') { |
| 3149 | neg = 1; |
| 3150 | name++; |
| 3151 | } |
| 3152 | if (*name == '*') { |
| 3153 | wild = 1; |
| 3154 | name++; |
| 3155 | } |
| 3156 | /* !* filter is a nop */ |
| 3157 | if (neg && wild) |
| 3158 | return order; |
| 3159 | if (*name) { |
| 3160 | int j, len; |
| 3161 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3162 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 3163 | trash.area[j] = tolower((unsigned char)name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3164 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3165 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3166 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3167 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3168 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 3169 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3170 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3171 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3172 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3173 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3174 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3175 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3176 | sc->order = order++; |
| 3177 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3178 | sc->wild = wild; |
| 3179 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3180 | sc->ckch_inst = ckch_inst; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3181 | LIST_APPEND(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3182 | } |
| 3183 | return order; |
| 3184 | } |
| 3185 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3186 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3187 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 3188 | * This function can't return an error. |
| 3189 | * |
| 3190 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 3191 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3192 | 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] | 3193 | { |
| 3194 | |
| 3195 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 3196 | struct ebmb_node *node; |
| 3197 | |
| 3198 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 3199 | |
| 3200 | /* ignore if sc0 was already inserted in a tree */ |
| 3201 | if (sc0->name.node.leaf_p) |
| 3202 | continue; |
| 3203 | |
| 3204 | /* Check for duplicates. */ |
| 3205 | if (sc0->wild) |
| 3206 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 3207 | else |
| 3208 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 3209 | |
| 3210 | for (; node; node = ebmb_next_dup(node)) { |
| 3211 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 3212 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 3213 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 3214 | /* it's a duplicate, we should remove and free it */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3215 | LIST_DELETE(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3216 | SSL_CTX_free(sc0->ctx); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3217 | ha_free(&sc0); |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 3218 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3219 | } |
| 3220 | } |
| 3221 | |
| 3222 | /* if duplicate, ignore the insertion */ |
| 3223 | if (!sc0) |
| 3224 | continue; |
| 3225 | |
| 3226 | if (sc0->wild) |
| 3227 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 3228 | else |
| 3229 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3230 | } |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3231 | |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3232 | /* replace the default_ctx if required with the instance's ctx. */ |
| 3233 | if (ckch_inst->is_default) { |
| 3234 | SSL_CTX_free(bind_conf->default_ctx); |
| 3235 | SSL_CTX_up_ref(ckch_inst->ctx); |
| 3236 | bind_conf->default_ctx = ckch_inst->ctx; |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 3237 | bind_conf->default_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3238 | } |
| 3239 | } |
| 3240 | |
| 3241 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3242 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3243 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3244 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3245 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3246 | /* tree of crtlist (crt-list/directory) */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3247 | struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3248 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3249 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 3250 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3251 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 3252 | * DH parameter available in ckchs nor in global, the default |
| 3253 | * DH parameters are applied on the SSL_CTX. |
| 3254 | * Returns a bitfield containing the flags: |
| 3255 | * ERR_FATAL in any fatal error case |
| 3256 | * ERR_ALERT if a reason of the error is availabine in err |
| 3257 | * ERR_WARN if a warning is available into err |
| 3258 | * The value 0 means there is no error nor warning and |
| 3259 | * the operation succeed. |
| 3260 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3261 | #ifndef OPENSSL_NO_DH |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3262 | 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] | 3263 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3264 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3265 | int ret = 0; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3266 | HASSL_DH *dh = NULL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3267 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3268 | if (data && data->dh) { |
| 3269 | dh = data->dh; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3270 | if (!ssl_sock_set_tmp_dh(ctx, dh)) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3271 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 3272 | err && *err ? *err : "", path); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3273 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3274 | err && *err ? *err : ""); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3275 | ret |= ERR_WARN; |
| 3276 | goto end; |
| 3277 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3278 | |
| 3279 | if (ssl_dh_ptr_index >= 0) { |
| 3280 | /* store a pointer to the DH params to avoid complaining about |
| 3281 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 3282 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 3283 | } |
| 3284 | } |
| 3285 | else if (global_dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3286 | if (!ssl_sock_set_tmp_dh(ctx, global_dh)) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3287 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 3288 | err && *err ? *err : "", path); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3289 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3290 | err && *err ? *err : ""); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3291 | ret |= ERR_WARN; |
| 3292 | goto end; |
| 3293 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3294 | } |
| 3295 | else { |
| 3296 | /* Clear openssl global errors stack */ |
| 3297 | ERR_clear_error(); |
| 3298 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3299 | /* We do not want DHE ciphers to be added to the cipher list |
| 3300 | * unless there is an explicit global dh option in the conf. |
| 3301 | */ |
| 3302 | if (global_ssl.default_dh_param) { |
| 3303 | if (global_ssl.default_dh_param <= 1024) { |
| 3304 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3305 | if (local_dh_1024 == NULL) |
| 3306 | local_dh_1024 = ssl_get_dh_1024(); |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3307 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3308 | if (local_dh_1024 == NULL) { |
| 3309 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3310 | err && *err ? *err : "", path); |
| 3311 | ret |= ERR_ALERT | ERR_FATAL; |
| 3312 | goto end; |
| 3313 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3314 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3315 | if (!ssl_sock_set_tmp_dh(ctx, local_dh_1024)) { |
| 3316 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3317 | err && *err ? *err : "", path); |
| 3318 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3319 | err && *err ? *err : ""); |
| 3320 | ret |= ERR_WARN; |
| 3321 | goto end; |
| 3322 | } |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3323 | } |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3324 | else { |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3325 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3326 | 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] | 3327 | #else |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3328 | 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] | 3329 | #endif |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3330 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3331 | } |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3332 | } |
| 3333 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3334 | end: |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3335 | ERR_clear_error(); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3336 | return ret; |
| 3337 | } |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3338 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3339 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3340 | |
| 3341 | /* Load a certificate chain into an SSL context. |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3342 | * Returns a bitfield containing the flags: |
| 3343 | * ERR_FATAL in any fatal error case |
| 3344 | * ERR_ALERT if the reason of the error is available in err |
| 3345 | * ERR_WARN if a warning is available into err |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3346 | * The caller is responsible of freeing the newly built or newly refcounted |
| 3347 | * find_chain element. |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3348 | * The value 0 means there is no error nor warning and |
| 3349 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3350 | */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3351 | 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] | 3352 | SSL_CTX *ctx, STACK_OF(X509) **find_chain, char **err) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3353 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3354 | int errcode = 0; |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3355 | int ret; |
| 3356 | |
| 3357 | ERR_clear_error(); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3358 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3359 | if (find_chain == NULL) { |
| 3360 | errcode |= ERR_FATAL; |
| 3361 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3362 | } |
| 3363 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3364 | if (!SSL_CTX_use_certificate(ctx, data->cert)) { |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3365 | ret = ERR_get_error(); |
| 3366 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s': %s.\n", |
| 3367 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3368 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3369 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3370 | } |
| 3371 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3372 | if (data->chain) { |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3373 | *find_chain = X509_chain_up_ref(data->chain); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3374 | } else { |
| 3375 | /* Find Certificate Chain in global */ |
| 3376 | struct issuer_chain *issuer; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3377 | issuer = ssl_get0_issuer_chain(data->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3378 | if (issuer) |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3379 | *find_chain = X509_chain_up_ref(issuer->chain); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3380 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3381 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3382 | if (!*find_chain) { |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3383 | /* always put a null chain stack in the SSL_CTX so it does not |
| 3384 | * try to build the chain from the verify store */ |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3385 | *find_chain = sk_X509_new_null(); |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3386 | } |
| 3387 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3388 | /* 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] | 3389 | #ifdef SSL_CTX_set1_chain |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3390 | if (!SSL_CTX_set1_chain(ctx, *find_chain)) { |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3391 | ret = ERR_get_error(); |
William Lallemand | a21ca74 | 2023-04-17 14:32:25 +0200 | [diff] [blame] | 3392 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s': %s.\n", |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3393 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3394 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3395 | goto end; |
| 3396 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3397 | #else |
| 3398 | { /* legacy compat (< openssl 1.0.2) */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3399 | X509 *ca; |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3400 | while ((ca = sk_X509_shift(*find_chain))) |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3401 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3402 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3403 | err && *err ? *err : "", path); |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3404 | X509_free(ca); |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3405 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3406 | goto end; |
| 3407 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3408 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3409 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3410 | |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3411 | #ifdef SSL_CTX_build_cert_chain |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3412 | /* remove the Root CA from the SSL_CTX if the option is activated */ |
| 3413 | if (global_ssl.skip_self_issued_ca) { |
| 3414 | if (!SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT|SSL_BUILD_CHAIN_FLAG_UNTRUSTED|SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)) { |
| 3415 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3416 | err && *err ? *err : "", path); |
| 3417 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3418 | goto end; |
| 3419 | } |
| 3420 | } |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3421 | #endif |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3422 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3423 | end: |
| 3424 | return errcode; |
| 3425 | } |
| 3426 | |
| 3427 | |
| 3428 | /* Loads the info in ckch into ctx |
| 3429 | * Returns a bitfield containing the flags: |
| 3430 | * ERR_FATAL in any fatal error case |
| 3431 | * ERR_ALERT if the reason of the error is available in err |
| 3432 | * ERR_WARN if a warning is available into err |
| 3433 | * The value 0 means there is no error nor warning and |
| 3434 | * the operation succeed. |
| 3435 | */ |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 3436 | 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] | 3437 | { |
| 3438 | int errcode = 0; |
| 3439 | STACK_OF(X509) *find_chain = NULL; |
| 3440 | |
William Lallemand | 5de4951 | 2022-10-27 14:41:07 +0200 | [diff] [blame] | 3441 | ERR_clear_error(); |
| 3442 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3443 | if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) { |
William Lallemand | 5de4951 | 2022-10-27 14:41:07 +0200 | [diff] [blame] | 3444 | int ret; |
| 3445 | |
| 3446 | ret = ERR_get_error(); |
| 3447 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s': %s.\n", |
| 3448 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3449 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3450 | return errcode; |
| 3451 | } |
| 3452 | |
| 3453 | /* Load certificate chain */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3454 | 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] | 3455 | if (errcode & ERR_CODE) |
| 3456 | goto end; |
| 3457 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3458 | #ifndef OPENSSL_NO_DH |
| 3459 | /* store a NULL pointer to indicate we have not yet loaded |
| 3460 | a custom DH param file */ |
| 3461 | if (ssl_dh_ptr_index >= 0) { |
| 3462 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3463 | } |
| 3464 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3465 | errcode |= ssl_sock_load_dh_params(ctx, data, path, err); |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3466 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3467 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3468 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3469 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3470 | } |
| 3471 | #endif |
| 3472 | |
Ilya Shipitsin | 7bbf586 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 3473 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3474 | if (sctl_ex_index >= 0 && data->sctl) { |
| 3475 | if (ssl_sock_load_sctl(ctx, data->sctl) < 0) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3476 | 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] | 3477 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3478 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3479 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3480 | } |
| 3481 | } |
| 3482 | #endif |
| 3483 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 3484 | #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] | 3485 | /* Load OCSP Info into context |
| 3486 | * If OCSP update mode is set to 'on', an entry will be created in the |
| 3487 | * ocsp tree even if no ocsp_response was known during init, unless the |
Ilya Shipitsin | 07be66d | 2023-04-01 12:26:42 +0200 | [diff] [blame] | 3488 | * frontend's conf disables ocsp update explicitly. |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 3489 | */ |
Remi Tricot-Le Breton | 0c96ee4 | 2023-03-01 16:11:50 +0100 | [diff] [blame] | 3490 | 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] | 3491 | if (data->ocsp_response) |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3492 | 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] | 3493 | err && *err ? *err : "", path); |
| 3494 | else |
| 3495 | 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", |
| 3496 | err && *err ? *err : "", path); |
| 3497 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3498 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3499 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3500 | #endif |
| 3501 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3502 | end: |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3503 | sk_X509_pop_free(find_chain, X509_free); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3504 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3505 | } |
| 3506 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3507 | |
| 3508 | /* Loads the info of a ckch built out of a backend certificate into an SSL ctx |
| 3509 | * Returns a bitfield containing the flags: |
| 3510 | * ERR_FATAL in any fatal error case |
| 3511 | * ERR_ALERT if the reason of the error is available in err |
| 3512 | * ERR_WARN if a warning is available into err |
| 3513 | * The value 0 means there is no error nor warning and |
| 3514 | * the operation succeed. |
| 3515 | */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3516 | 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] | 3517 | SSL_CTX *ctx, char **err) |
| 3518 | { |
| 3519 | int errcode = 0; |
| 3520 | STACK_OF(X509) *find_chain = NULL; |
| 3521 | |
| 3522 | /* Load the private key */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3523 | if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) { |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3524 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3525 | err && *err ? *err : "", path); |
| 3526 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3527 | } |
| 3528 | |
| 3529 | /* Load certificate chain */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3530 | 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] | 3531 | if (errcode & ERR_CODE) |
| 3532 | goto end; |
| 3533 | |
| 3534 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3535 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3536 | err && *err ? *err : "", path); |
| 3537 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3538 | } |
| 3539 | |
| 3540 | end: |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3541 | sk_X509_pop_free(find_chain, X509_free); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3542 | return errcode; |
| 3543 | } |
| 3544 | |
| 3545 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3546 | /* |
| 3547 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3548 | * |
| 3549 | * Returns a bitfield containing the flags: |
| 3550 | * ERR_FATAL in any fatal error case |
| 3551 | * ERR_ALERT if the reason of the error is available in err |
| 3552 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3553 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3554 | 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] | 3555 | 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] | 3556 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3557 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3558 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3559 | int order = 0; |
| 3560 | X509_NAME *xname; |
| 3561 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3562 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3563 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3564 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3565 | STACK_OF(GENERAL_NAME) *names; |
| 3566 | #endif |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3567 | struct ckch_data *data; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3568 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3569 | int errcode = 0; |
| 3570 | |
| 3571 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3572 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3573 | if (!ckchs || !ckchs->data) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3574 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3575 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3576 | data = ckchs->data; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3577 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3578 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3579 | if (!ctx) { |
| 3580 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3581 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3582 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3583 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3584 | } |
| 3585 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3586 | errcode |= ssl_sock_put_ckch_into_ctx(path, data, ctx, err); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3587 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3588 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3589 | |
| 3590 | ckch_inst = ckch_inst_new(); |
| 3591 | if (!ckch_inst) { |
| 3592 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3593 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3594 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3595 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3596 | } |
| 3597 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3598 | pkey = X509_get_pubkey(data->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3599 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3600 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3601 | switch(EVP_PKEY_base_id(pkey)) { |
| 3602 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3603 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3604 | break; |
| 3605 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3606 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3607 | break; |
| 3608 | case EVP_PKEY_DSA: |
| 3609 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3610 | break; |
| 3611 | } |
| 3612 | EVP_PKEY_free(pkey); |
| 3613 | } |
| 3614 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3615 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3616 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3617 | 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] | 3618 | if (order < 0) { |
| 3619 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3620 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3621 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3622 | } |
| 3623 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3624 | } |
| 3625 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3626 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3627 | 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] | 3628 | if (names) { |
| 3629 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3630 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3631 | if (name->type == GEN_DNS) { |
| 3632 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3633 | 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] | 3634 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3635 | if (order < 0) { |
| 3636 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3637 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3638 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3639 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3640 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3641 | } |
| 3642 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3643 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3644 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3645 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3646 | xname = X509_get_subject_name(data->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3647 | i = -1; |
| 3648 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3649 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3650 | ASN1_STRING *value; |
| 3651 | |
| 3652 | value = X509_NAME_ENTRY_get_data(entry); |
| 3653 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3654 | 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] | 3655 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3656 | if (order < 0) { |
| 3657 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3658 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3659 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3660 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3661 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3662 | } |
| 3663 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3664 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3665 | * the tree, so it will be discovered and cleaned in time. |
| 3666 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3667 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3668 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3669 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3670 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3671 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3672 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3673 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3674 | } |
| 3675 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3676 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3677 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3678 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3679 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3680 | SSL_CTX_up_ref(ctx); |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 3681 | bind_conf->default_inst = ckch_inst; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3682 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3683 | |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3684 | /* Always keep a reference to the newly constructed SSL_CTX in the |
| 3685 | * instance. This way if the instance has no SNIs, the SSL_CTX will |
| 3686 | * still be linked. */ |
| 3687 | SSL_CTX_up_ref(ctx); |
| 3688 | ckch_inst->ctx = ctx; |
| 3689 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3690 | /* everything succeed, the ckch instance can be used */ |
| 3691 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3692 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3693 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3694 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3695 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3696 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3697 | *ckchi = ckch_inst; |
| 3698 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3699 | |
| 3700 | error: |
| 3701 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3702 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3703 | if (ckch_inst->is_default) |
| 3704 | SSL_CTX_free(ctx); |
| 3705 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3706 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3707 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3708 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3709 | SSL_CTX_free(ctx); |
| 3710 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3711 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3712 | } |
| 3713 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3714 | |
| 3715 | /* |
| 3716 | * This function allocate a ckch_inst that will be used on the backend side |
| 3717 | * (server line) |
| 3718 | * |
| 3719 | * Returns a bitfield containing the flags: |
| 3720 | * ERR_FATAL in any fatal error case |
| 3721 | * ERR_ALERT if the reason of the error is available in err |
| 3722 | * ERR_WARN if a warning is available into err |
| 3723 | */ |
| 3724 | 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] | 3725 | struct ckch_inst **ckchi, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3726 | { |
| 3727 | SSL_CTX *ctx; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3728 | struct ckch_data *data; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3729 | struct ckch_inst *ckch_inst = NULL; |
| 3730 | int errcode = 0; |
| 3731 | |
| 3732 | *ckchi = NULL; |
| 3733 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3734 | if (!ckchs || !ckchs->data) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3735 | return ERR_FATAL; |
| 3736 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3737 | data = ckchs->data; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3738 | |
| 3739 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 3740 | if (!ctx) { |
| 3741 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3742 | err && *err ? *err : "", path); |
| 3743 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3744 | goto error; |
| 3745 | } |
| 3746 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3747 | 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] | 3748 | if (errcode & ERR_CODE) |
| 3749 | goto error; |
| 3750 | |
| 3751 | ckch_inst = ckch_inst_new(); |
| 3752 | if (!ckch_inst) { |
| 3753 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3754 | err && *err ? *err : "", path); |
| 3755 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3756 | goto error; |
| 3757 | } |
| 3758 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3759 | /* everything succeed, the ckch instance can be used */ |
| 3760 | ckch_inst->bind_conf = NULL; |
| 3761 | ckch_inst->ssl_conf = NULL; |
| 3762 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 3763 | ckch_inst->ctx = ctx; |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3764 | ckch_inst->is_server_instance = 1; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3765 | |
| 3766 | *ckchi = ckch_inst; |
| 3767 | return errcode; |
| 3768 | |
| 3769 | error: |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3770 | SSL_CTX_free(ctx); |
| 3771 | |
| 3772 | return errcode; |
| 3773 | } |
| 3774 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 3775 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3776 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 3777 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3778 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3779 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3780 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3781 | |
| 3782 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | e7eb1fe | 2020-09-16 16:17:51 +0200 | [diff] [blame] | 3783 | 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] | 3784 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3785 | if (errcode & ERR_CODE) |
| 3786 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3787 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3788 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3789 | |
| 3790 | /* succeed, add the instance to the ckch_store's list of instance */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3791 | LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3792 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3793 | } |
| 3794 | |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3795 | /* This function generates a <struct ckch_inst *> for a <struct server *>, and |
| 3796 | * fill the SSL_CTX of the server. |
| 3797 | * |
| 3798 | * 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] | 3799 | 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] | 3800 | struct server *server, struct ckch_inst **ckch_inst, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3801 | { |
| 3802 | int errcode = 0; |
| 3803 | |
| 3804 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 3805 | 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] | 3806 | |
| 3807 | if (errcode & ERR_CODE) |
| 3808 | return errcode; |
| 3809 | |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3810 | (*ckch_inst)->server = server; |
| 3811 | /* Keep the reference to the SSL_CTX in the server. */ |
| 3812 | SSL_CTX_up_ref((*ckch_inst)->ctx); |
| 3813 | server->ssl_ctx.ctx = (*ckch_inst)->ctx; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3814 | /* succeed, add the instance to the ckch_store's list of instance */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3815 | LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3816 | return errcode; |
| 3817 | } |
| 3818 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3819 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3820 | |
| 3821 | |
| 3822 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3823 | * done once. Zero is returned if the operation fails. No error is returned |
| 3824 | * if the random is said as not implemented, because we expect that openssl |
| 3825 | * will use another method once needed. |
| 3826 | */ |
Amaury Denoyelle | c593bcd | 2021-05-19 15:35:29 +0200 | [diff] [blame] | 3827 | int ssl_initialize_random(void) |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3828 | { |
| 3829 | unsigned char random; |
| 3830 | static int random_initialized = 0; |
| 3831 | |
| 3832 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3833 | random_initialized = 1; |
| 3834 | |
| 3835 | return random_initialized; |
| 3836 | } |
| 3837 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3838 | /* Load a crt-list file, this is done in 2 parts: |
| 3839 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 3840 | * - generate the instances by iterating on entries in the crtlist struct |
| 3841 | * |
| 3842 | * Nothing is locked there, this function is used in the configuration parser. |
| 3843 | * |
| 3844 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3845 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3846 | 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] | 3847 | { |
| 3848 | struct crtlist *crtlist = NULL; |
| 3849 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3850 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3851 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3852 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3853 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3854 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3855 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 3856 | if (!bind_conf_node) { |
| 3857 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 3858 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3859 | goto error; |
| 3860 | } |
| 3861 | bind_conf_node->next = NULL; |
| 3862 | bind_conf_node->bind_conf = bind_conf; |
| 3863 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3864 | /* strip trailing slashes, including first one */ |
| 3865 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 3866 | *end = 0; |
| 3867 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3868 | /* look for an existing crtlist or create one */ |
| 3869 | eb = ebst_lookup(&crtlists_tree, file); |
| 3870 | if (eb) { |
| 3871 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 3872 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3873 | /* load a crt-list OR a directory */ |
| 3874 | if (dir) |
| 3875 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 3876 | else |
| 3877 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 3878 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3879 | if (!(cfgerr & ERR_CODE)) |
| 3880 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 3881 | } |
| 3882 | |
| 3883 | if (cfgerr & ERR_CODE) { |
| 3884 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3885 | goto error; |
| 3886 | } |
| 3887 | |
| 3888 | /* generates ckch instance from the crtlist_entry */ |
| 3889 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3890 | struct ckch_store *store; |
| 3891 | struct ckch_inst *ckch_inst = NULL; |
| 3892 | |
| 3893 | store = entry->node.key; |
| 3894 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 3895 | if (cfgerr & ERR_CODE) { |
| 3896 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 3897 | goto error; |
| 3898 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3899 | LIST_APPEND(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 3900 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3901 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3902 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3903 | /* add the bind_conf to the list */ |
| 3904 | bind_conf_node->next = crtlist->bind_conf; |
| 3905 | crtlist->bind_conf = bind_conf_node; |
| 3906 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3907 | return cfgerr; |
| 3908 | error: |
| 3909 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3910 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3911 | struct ckch_inst *inst, *s_inst; |
| 3912 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3913 | lastentry = entry; /* which entry we tried to generate last */ |
| 3914 | if (lastentry) { |
| 3915 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3916 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 3917 | break; |
| 3918 | |
| 3919 | 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] | 3920 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3921 | /* this was not generated for this bind_conf, skip */ |
| 3922 | if (inst->bind_conf != bind_conf) |
| 3923 | continue; |
| 3924 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3925 | /* free the sni_ctx and instance */ |
| 3926 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3927 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3928 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3929 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3930 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3931 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3932 | return cfgerr; |
| 3933 | } |
| 3934 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3935 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 3936 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 3937 | { |
| 3938 | struct stat buf; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3939 | int cfgerr = 0; |
| 3940 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3941 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3942 | int found = 0; /* did we found a file to load ? */ |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3943 | |
| 3944 | if ((ckchs = ckchs_lookup(path))) { |
| 3945 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3946 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3947 | found++; |
| 3948 | } else if (stat(path, &buf) == 0) { |
| 3949 | found++; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3950 | if (S_ISDIR(buf.st_mode) == 0) { |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3951 | ckchs = ckchs_load_cert_file(path, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3952 | if (!ckchs) |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3953 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3954 | 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] | 3955 | } else { |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3956 | 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] | 3957 | } |
| 3958 | } else { |
| 3959 | /* stat failed, could be a bundle */ |
| 3960 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3961 | char fp[MAXPATHLEN+1] = {0}; |
| 3962 | int n = 0; |
| 3963 | |
| 3964 | /* Load all possible certs and keys in separate ckch_store */ |
| 3965 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3966 | struct stat buf; |
| 3967 | int ret; |
| 3968 | |
| 3969 | ret = snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3970 | if (ret > sizeof(fp)) |
| 3971 | continue; |
| 3972 | |
| 3973 | if ((ckchs = ckchs_lookup(fp))) { |
| 3974 | 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] | 3975 | found++; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3976 | } else { |
| 3977 | if (stat(fp, &buf) == 0) { |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3978 | found++; |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3979 | ckchs = ckchs_load_cert_file(fp, err); |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3980 | if (!ckchs) |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3981 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3982 | cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3983 | } |
| 3984 | } |
| 3985 | } |
William Lallemand | b7fdfdf | 2020-12-04 15:45:02 +0100 | [diff] [blame] | 3986 | #if HA_OPENSSL_VERSION_NUMBER < 0x10101000L |
| 3987 | if (found) { |
| 3988 | memprintf(err, "%sCan't load '%s'. Loading a multi certificates bundle requires OpenSSL >= 1.1.1\n", |
| 3989 | err && *err ? *err : "", path); |
| 3990 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3991 | } |
| 3992 | #endif |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3993 | } |
| 3994 | } |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3995 | if (!found) { |
| 3996 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3997 | err && *err ? *err : "", path, strerror(errno)); |
| 3998 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3999 | } |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4000 | |
| 4001 | return cfgerr; |
| 4002 | } |
| 4003 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4004 | |
| 4005 | /* Create a full ssl context and ckch instance that will be used for a specific |
| 4006 | * backend server (server configuration line). |
| 4007 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 4008 | */ |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4009 | 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] | 4010 | { |
| 4011 | struct stat buf; |
| 4012 | int cfgerr = 0; |
| 4013 | struct ckch_store *ckchs; |
| 4014 | int found = 0; /* did we found a file to load ? */ |
| 4015 | |
| 4016 | if ((ckchs = ckchs_lookup(path))) { |
| 4017 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 4018 | 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] | 4019 | found++; |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4020 | } else { |
| 4021 | if (!create_if_none) { |
| 4022 | memprintf(err, "%sunable to stat SSL certificate '%s'.\n", |
| 4023 | err && *err ? *err : "", path); |
| 4024 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4025 | goto out; |
| 4026 | } |
| 4027 | |
| 4028 | if (stat(path, &buf) == 0) { |
| 4029 | /* We do not manage directories on backend side. */ |
| 4030 | if (S_ISDIR(buf.st_mode) == 0) { |
| 4031 | ++found; |
| 4032 | ckchs = ckchs_load_cert_file(path, err); |
| 4033 | if (!ckchs) |
| 4034 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4035 | cfgerr |= ssl_sock_load_srv_ckchs(path, ckchs, server, &server->ssl_ctx.inst, err); |
| 4036 | } |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4037 | } |
| 4038 | } |
| 4039 | if (!found) { |
| 4040 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4041 | err && *err ? *err : "", path, strerror(errno)); |
| 4042 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4043 | } |
| 4044 | |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4045 | out: |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4046 | return cfgerr; |
| 4047 | } |
| 4048 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4049 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4050 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4051 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4052 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4053 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4054 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4055 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4056 | SSL_OP_NO_SSLv2 | |
| 4057 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4058 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4059 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4060 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 4061 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4062 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4063 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4064 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4065 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4066 | SSL_MODE_RELEASE_BUFFERS | |
| 4067 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 4068 | 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] | 4069 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4070 | int flags = MC_SSL_O_ALL; |
| 4071 | int cfgerr = 0; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 4072 | const int default_min_ver = CONF_TLSV12; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4073 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4074 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4075 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4076 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4077 | 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] | 4078 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 4079 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4080 | 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] | 4081 | else |
| 4082 | flags = conf_ssl_methods->flags; |
| 4083 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 4084 | min = conf_ssl_methods->min; |
| 4085 | max = conf_ssl_methods->max; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 4086 | |
| 4087 | /* default minimum is TLSV12, */ |
| 4088 | if (!min) { |
| 4089 | if (!max || (max >= default_min_ver)) { |
| 4090 | min = default_min_ver; |
| 4091 | } else { |
| 4092 | 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). " |
| 4093 | "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n", |
| 4094 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name); |
| 4095 | min = max; |
| 4096 | } |
| 4097 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4098 | /* 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] | 4099 | if (min) |
| 4100 | flags |= (methodVersions[min].flag - 1); |
| 4101 | if (max) |
| 4102 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4103 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4104 | min = max = CONF_TLSV_NONE; |
| 4105 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4106 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4107 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4108 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4109 | if (min) { |
| 4110 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4111 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 4112 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4113 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 4114 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4115 | hole = 0; |
| 4116 | } |
| 4117 | max = i; |
| 4118 | } |
| 4119 | else { |
| 4120 | min = max = i; |
| 4121 | } |
| 4122 | } |
| 4123 | else { |
| 4124 | if (min) |
| 4125 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4126 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4127 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4128 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4129 | 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] | 4130 | cfgerr += 1; |
| 4131 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4132 | /* save real min/max in bind_conf */ |
| 4133 | conf_ssl_methods->min = min; |
| 4134 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4135 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4136 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4137 | /* 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] | 4138 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4139 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4140 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4141 | else |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4142 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) { |
| 4143 | /* clear every version flags in case SSL_CTX_new() |
| 4144 | * returns an SSL_CTX with disabled versions */ |
| 4145 | SSL_CTX_clear_options(ctx, methodVersions[i].option); |
| 4146 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4147 | if (flags & methodVersions[i].flag) |
| 4148 | options |= methodVersions[i].option; |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4149 | |
| 4150 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4151 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4152 | /* 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] | 4153 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4154 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 4155 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4156 | |
| 4157 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 4158 | options |= SSL_OP_NO_TICKET; |
| 4159 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 4160 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 4161 | |
| 4162 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 4163 | options |= SSL_OP_NO_RENEGOTIATION; |
| 4164 | #endif |
| 4165 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4166 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4167 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 4168 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4169 | if (global_ssl.async) |
| 4170 | mode |= SSL_MODE_ASYNC; |
| 4171 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4172 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4173 | if (global_ssl.life_time) |
| 4174 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4175 | |
| 4176 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4177 | # ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4178 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 4179 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4180 | # elif defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 4181 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 4182 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 4183 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4184 | # endif /* ! SSL_OP_NO_ANTI_REPLAY */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 4185 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 4186 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4187 | # else /* ! OPENSSL_IS_BORINGSSL && ! HAVE_SSL_CLIENT_HELLO_CB */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4188 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4189 | # endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 4190 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4191 | #endif /* ! SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4192 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4193 | } |
| 4194 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4195 | |
| 4196 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 4197 | { |
| 4198 | if (first == block) { |
| 4199 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4200 | if (first->len > 0) |
| 4201 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 4202 | } |
| 4203 | } |
| 4204 | |
| 4205 | /* return first block from sh_ssl_sess */ |
| 4206 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 4207 | { |
Ilya Shipitsin | 2ca0158 | 2023-04-15 23:39:43 +0200 | [diff] [blame] | 4208 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - offsetof(struct shared_block, data)); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4209 | |
| 4210 | } |
| 4211 | |
| 4212 | /* store a session into the cache |
| 4213 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 4214 | * data: asn1 encoded session |
| 4215 | * data_len: asn1 encoded session length |
| 4216 | * Returns 1 id session was stored (else 0) |
| 4217 | */ |
| 4218 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 4219 | { |
| 4220 | struct shared_block *first; |
| 4221 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 4222 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4223 | 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] | 4224 | if (!first) { |
| 4225 | /* Could not retrieve enough free blocks to store that session */ |
| 4226 | return 0; |
| 4227 | } |
| 4228 | |
| 4229 | /* STORE the key in the first elem */ |
| 4230 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4231 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 4232 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4233 | |
| 4234 | /* it returns the already existing node |
| 4235 | or current node if none, never returns null */ |
| 4236 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4237 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4238 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4239 | /* release the reserved row */ |
William Lallemand | 222e5a2 | 2023-01-31 14:12:28 +0100 | [diff] [blame] | 4240 | 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] | 4241 | shctx_row_dec_hot(ssl_shctx, first); |
| 4242 | /* replace the previous session already in the tree */ |
| 4243 | sh_ssl_sess = oldsh_ssl_sess; |
| 4244 | /* ignore the previous session data, only use the header */ |
| 4245 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4246 | shctx_row_inc_hot(ssl_shctx, first); |
| 4247 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4248 | } |
| 4249 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4250 | 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] | 4251 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4252 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4256 | |
| 4257 | return 1; |
| 4258 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4259 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4260 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4261 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4262 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4263 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4264 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4265 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4266 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4267 | |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4268 | /* RWLOCK: only read lock the SSL cache even when writing in it because there is |
| 4269 | * one cache per thread, it only prevents to flush it from the CLI in |
| 4270 | * another thread */ |
| 4271 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4272 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4273 | int len; |
| 4274 | unsigned char *ptr; |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4275 | const char *sni; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4276 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4277 | len = i2d_SSL_SESSION(sess, NULL); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4278 | sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4279 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4280 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4281 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4282 | } else { |
Willy Tarreau | 3bda3f4 | 2021-02-26 21:05:08 +0100 | [diff] [blame] | 4283 | ptr = realloc(s->ssl_ctx.reused_sess[tid].ptr, len); |
| 4284 | s->ssl_ctx.reused_sess[tid].ptr = ptr; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4285 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4286 | } |
| 4287 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4288 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4289 | &ptr); |
| 4290 | } |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4291 | |
| 4292 | if (s->ssl_ctx.reused_sess[tid].sni) { |
| 4293 | /* if the new sni is empty or isn' t the same as the old one */ |
| 4294 | if ((!sni) || strcmp(s->ssl_ctx.reused_sess[tid].sni, sni) != 0) { |
| 4295 | ha_free(&s->ssl_ctx.reused_sess[tid].sni); |
| 4296 | if (sni) |
| 4297 | s->ssl_ctx.reused_sess[tid].sni = strdup(sni); |
| 4298 | } |
| 4299 | } else if (sni) { |
| 4300 | /* if there wasn't an old sni but there is a new one */ |
| 4301 | s->ssl_ctx.reused_sess[tid].sni = strdup(sni); |
| 4302 | } |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4303 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4304 | } else { |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4305 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4306 | ha_free(&s->ssl_ctx.reused_sess[tid].ptr); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4307 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4311 | } |
| 4312 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4313 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4314 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4315 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4316 | { |
| 4317 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4318 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4319 | unsigned char *p; |
| 4320 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4321 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4322 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4323 | |
| 4324 | /* 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] | 4325 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4326 | * note: SSL_SESSION_set1_id is using |
| 4327 | * a memcpy so we need to use a different pointer |
| 4328 | * than sid_data or sid_ctx_data to avoid valgrind |
| 4329 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4330 | */ |
| 4331 | |
| 4332 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4333 | |
| 4334 | /* copy value in an other buffer */ |
| 4335 | memcpy(encid, sid_data, sid_length); |
| 4336 | |
| 4337 | /* pad with 0 */ |
| 4338 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4339 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4340 | |
| 4341 | /* force length to zero to avoid ASN1 encoding */ |
| 4342 | SSL_SESSION_set1_id(sess, encid, 0); |
| 4343 | |
| 4344 | /* force length to zero to avoid ASN1 encoding */ |
| 4345 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4346 | |
| 4347 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4348 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4349 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4350 | goto err; |
| 4351 | |
| 4352 | p = encsess; |
| 4353 | |
| 4354 | /* process ASN1 session encoding before the lock */ |
| 4355 | i2d_SSL_SESSION(sess, &p); |
| 4356 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4357 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4358 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4359 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4360 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4361 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4362 | err: |
| 4363 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4364 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 4365 | 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] | 4366 | |
| 4367 | return 0; /* do not increment session reference count */ |
| 4368 | } |
| 4369 | |
| 4370 | /* 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] | 4371 | 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] | 4372 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4373 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4374 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4375 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4376 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4377 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4378 | |
Willy Tarreau | 4c19e99 | 2021-06-15 16:39:22 +0200 | [diff] [blame] | 4379 | _HA_ATOMIC_INC(&global.shctx_lookups); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4380 | |
| 4381 | /* allow the session to be freed automatically by openssl */ |
| 4382 | *do_copy = 0; |
| 4383 | |
| 4384 | /* tree key is zeros padded sessionid */ |
| 4385 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4386 | memcpy(tmpkey, key, key_len); |
| 4387 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4388 | key = tmpkey; |
| 4389 | } |
| 4390 | |
| 4391 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4392 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4393 | |
| 4394 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4395 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4396 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4397 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4398 | shctx_unlock(ssl_shctx); |
Willy Tarreau | 4c19e99 | 2021-06-15 16:39:22 +0200 | [diff] [blame] | 4399 | _HA_ATOMIC_INC(&global.shctx_misses); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4400 | return NULL; |
| 4401 | } |
| 4402 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4403 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4404 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4405 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4406 | 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] | 4407 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4408 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4409 | |
| 4410 | /* decode ASN1 session */ |
| 4411 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4412 | 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] | 4413 | /* Reset session id and session id contenxt */ |
| 4414 | if (sess) { |
| 4415 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4416 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4417 | } |
| 4418 | |
| 4419 | return sess; |
| 4420 | } |
| 4421 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4422 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4423 | /* 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] | 4424 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4425 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4426 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4427 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4428 | unsigned int sid_length; |
| 4429 | const unsigned char *sid_data; |
| 4430 | (void)ctx; |
| 4431 | |
| 4432 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4433 | /* tree key is zeros padded sessionid */ |
| 4434 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4435 | memcpy(tmpkey, sid_data, sid_length); |
| 4436 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4437 | sid_data = tmpkey; |
| 4438 | } |
| 4439 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4440 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4441 | |
| 4442 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4443 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4444 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4445 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4446 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4447 | } |
| 4448 | |
| 4449 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4450 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4451 | } |
| 4452 | |
| 4453 | /* Set session cache mode to server and disable openssl internal cache. |
| 4454 | * Set shared cache callbacks on an ssl context. |
| 4455 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4456 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4457 | { |
| 4458 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4459 | |
| 4460 | if (!ssl_shctx) { |
| 4461 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4462 | return; |
| 4463 | } |
| 4464 | |
| 4465 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4466 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4467 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4468 | |
| 4469 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4470 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4471 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4472 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4473 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4474 | |
| 4475 | /* |
| 4476 | * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format |
| 4477 | * |
| 4478 | * The format is: |
| 4479 | * * <Label> <space> <ClientRandom> <space> <Secret> |
| 4480 | * We only need to copy the secret as there is a sample fetch for the ClientRandom |
| 4481 | */ |
| 4482 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 4483 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4484 | void SSL_CTX_keylog(const SSL *ssl, const char *line) |
| 4485 | { |
| 4486 | struct ssl_keylog *keylog; |
| 4487 | char *lastarg = NULL; |
| 4488 | char *dst = NULL; |
| 4489 | |
| 4490 | keylog = SSL_get_ex_data(ssl, ssl_keylog_index); |
| 4491 | if (!keylog) |
| 4492 | return; |
| 4493 | |
| 4494 | lastarg = strrchr(line, ' '); |
| 4495 | if (lastarg == NULL || ++lastarg == NULL) |
| 4496 | return; |
| 4497 | |
| 4498 | dst = pool_alloc(pool_head_ssl_keylog_str); |
| 4499 | if (!dst) |
| 4500 | return; |
| 4501 | |
| 4502 | strncpy(dst, lastarg, SSL_KEYLOG_MAX_SECRET_SIZE-1); |
| 4503 | dst[SSL_KEYLOG_MAX_SECRET_SIZE-1] = '\0'; |
| 4504 | |
| 4505 | if (strncmp(line, "CLIENT_RANDOM ", strlen("CLIENT RANDOM ")) == 0) { |
| 4506 | if (keylog->client_random) |
| 4507 | goto error; |
| 4508 | keylog->client_random = dst; |
| 4509 | |
| 4510 | } else if (strncmp(line, "CLIENT_EARLY_TRAFFIC_SECRET ", strlen("CLIENT_EARLY_TRAFFIC_SECRET ")) == 0) { |
| 4511 | if (keylog->client_early_traffic_secret) |
| 4512 | goto error; |
| 4513 | keylog->client_early_traffic_secret = dst; |
| 4514 | |
| 4515 | } else if (strncmp(line, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ", strlen("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 4516 | if(keylog->client_handshake_traffic_secret) |
| 4517 | goto error; |
| 4518 | keylog->client_handshake_traffic_secret = dst; |
| 4519 | |
| 4520 | } else if (strncmp(line, "SERVER_HANDSHAKE_TRAFFIC_SECRET ", strlen("SERVER_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 4521 | if (keylog->server_handshake_traffic_secret) |
| 4522 | goto error; |
| 4523 | keylog->server_handshake_traffic_secret = dst; |
| 4524 | |
| 4525 | } else if (strncmp(line, "CLIENT_TRAFFIC_SECRET_0 ", strlen("CLIENT_TRAFFIC_SECRET_0 ")) == 0) { |
| 4526 | if (keylog->client_traffic_secret_0) |
| 4527 | goto error; |
| 4528 | keylog->client_traffic_secret_0 = dst; |
| 4529 | |
| 4530 | } else if (strncmp(line, "SERVER_TRAFFIC_SECRET_0 ", strlen("SERVER_TRAFFIC_SECRET_0 ")) == 0) { |
| 4531 | if (keylog->server_traffic_secret_0) |
| 4532 | goto error; |
| 4533 | keylog->server_traffic_secret_0 = dst; |
| 4534 | |
| 4535 | } else if (strncmp(line, "EARLY_EXPORTER_SECRET ", strlen("EARLY_EXPORTER_SECRET ")) == 0) { |
| 4536 | if (keylog->early_exporter_secret) |
| 4537 | goto error; |
| 4538 | keylog->early_exporter_secret = dst; |
| 4539 | |
| 4540 | } else if (strncmp(line, "EXPORTER_SECRET ", strlen("EXPORTER_SECRET ")) == 0) { |
| 4541 | if (keylog->exporter_secret) |
| 4542 | goto error; |
| 4543 | keylog->exporter_secret = dst; |
| 4544 | } else { |
| 4545 | goto error; |
| 4546 | } |
| 4547 | |
| 4548 | return; |
| 4549 | |
| 4550 | error: |
| 4551 | pool_free(pool_head_ssl_keylog_str, dst); |
| 4552 | |
| 4553 | return; |
| 4554 | } |
| 4555 | #endif |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4556 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4557 | /* |
| 4558 | * This function applies the SSL configuration on a SSL_CTX |
| 4559 | * It returns an error code and fills the <err> buffer |
| 4560 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4561 | 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] | 4562 | { |
| 4563 | struct proxy *curproxy = bind_conf->frontend; |
| 4564 | int cfgerr = 0; |
| 4565 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4566 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4567 | const char *conf_ciphers; |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 4568 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4569 | const char *conf_ciphersuites; |
| 4570 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4571 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4572 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4573 | if (ssl_conf) { |
| 4574 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4575 | int i, min, max; |
| 4576 | int flags = MC_SSL_O_ALL; |
| 4577 | |
| 4578 | /* 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] | 4579 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4580 | 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] | 4581 | if (min) |
| 4582 | flags |= (methodVersions[min].flag - 1); |
| 4583 | if (max) |
| 4584 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4585 | min = max = CONF_TLSV_NONE; |
| 4586 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4587 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4588 | if (min) |
| 4589 | max = i; |
| 4590 | else |
| 4591 | min = max = i; |
| 4592 | } |
| 4593 | /* save real min/max */ |
| 4594 | conf_ssl_methods->min = min; |
| 4595 | conf_ssl_methods->max = max; |
| 4596 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4597 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4598 | 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] | 4599 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4600 | } |
| 4601 | } |
| 4602 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4603 | 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] | 4604 | case SSL_SOCK_VERIFY_NONE: |
| 4605 | verify = SSL_VERIFY_NONE; |
| 4606 | break; |
| 4607 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4608 | verify = SSL_VERIFY_PEER; |
| 4609 | break; |
| 4610 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4611 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4612 | break; |
| 4613 | } |
| 4614 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4615 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4616 | 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] | 4617 | 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] | 4618 | 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] | 4619 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4620 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4621 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4622 | 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] | 4623 | 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] | 4624 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4625 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4626 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 4627 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 4628 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4629 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4630 | } |
| 4631 | 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] | 4632 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 4633 | 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] | 4634 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4635 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4636 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4637 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4638 | 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] | 4639 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4640 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4641 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4642 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4643 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4644 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4645 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4646 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4647 | 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] | 4648 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4649 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4650 | else { |
| 4651 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4652 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4653 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4654 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4655 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4656 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4657 | #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] | 4658 | if(bind_conf->keys_ref) { |
Remi Tricot-Le Breton | 8ea1f5f | 2022-02-08 17:45:58 +0100 | [diff] [blame] | 4659 | 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] | 4660 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4661 | 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] | 4662 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4663 | } |
| 4664 | } |
| 4665 | #endif |
| 4666 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4667 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4668 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4669 | if (conf_ciphers && |
| 4670 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4671 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4672 | 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] | 4673 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4674 | } |
| 4675 | |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 4676 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4677 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4678 | if (conf_ciphersuites && |
| 4679 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4680 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4681 | 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] | 4682 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4683 | } |
| 4684 | #endif |
| 4685 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4686 | #ifndef OPENSSL_NO_DH |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 4687 | if (!local_dh_1024) |
| 4688 | local_dh_1024 = ssl_get_dh_1024(); |
| 4689 | if (!local_dh_2048) |
| 4690 | local_dh_2048 = ssl_get_dh_2048(); |
| 4691 | if (!local_dh_4096) |
| 4692 | local_dh_4096 = ssl_get_dh_4096(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4693 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4694 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4695 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Ilya Shipitsin | 7ff7747 | 2021-02-08 16:55:06 +0500 | [diff] [blame] | 4696 | #ifdef SSL_CTRL_SET_MSG_CALLBACK |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4697 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4698 | #endif |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 4699 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | b60a77b | 2022-11-18 15:00:15 +0100 | [diff] [blame] | 4700 | /* only activate the keylog callback if it was required to prevent performance loss */ |
| 4701 | if (global_ssl.keylog > 0) |
| 4702 | SSL_CTX_set_keylog_callback(ctx, SSL_CTX_keylog); |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4703 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4704 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4705 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4706 | ssl_conf_cur = NULL; |
| 4707 | if (ssl_conf && ssl_conf->npn_str) |
| 4708 | ssl_conf_cur = ssl_conf; |
| 4709 | else if (bind_conf->ssl_conf.npn_str) |
| 4710 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4711 | if (ssl_conf_cur) |
| 4712 | 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] | 4713 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4714 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4715 | ssl_conf_cur = NULL; |
| 4716 | if (ssl_conf && ssl_conf->alpn_str) |
| 4717 | ssl_conf_cur = ssl_conf; |
| 4718 | else if (bind_conf->ssl_conf.alpn_str) |
| 4719 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4720 | if (ssl_conf_cur) |
| 4721 | 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] | 4722 | #endif |
Ilya Shipitsin | 0aa8c29 | 2020-11-04 00:39:07 +0500 | [diff] [blame] | 4723 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4724 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4725 | if (conf_curves) { |
| 4726 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4727 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4728 | 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] | 4729 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4730 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4731 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4732 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4733 | #endif /* defined(SSL_CTX_set1_curves_list) */ |
| 4734 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4735 | if (!conf_curves) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4736 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4737 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4738 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4739 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4740 | NULL); |
| 4741 | |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4742 | if (ecdhe && SSL_CTX_set1_curves_list(ctx, ecdhe) == 0) { |
| 4743 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4744 | err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4745 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4746 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4747 | #endif /* defined(SSL_CTX_set1_curves_list) */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4748 | #else |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4749 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 4750 | int i; |
| 4751 | EC_KEY *ecdh; |
| 4752 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4753 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4754 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4755 | ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4756 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4757 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4758 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4759 | 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] | 4760 | 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] | 4761 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4762 | } |
| 4763 | else { |
| 4764 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4765 | EC_KEY_free(ecdh); |
| 4766 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4767 | #endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */ |
| 4768 | #endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */ |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4769 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4770 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4771 | return cfgerr; |
| 4772 | } |
| 4773 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4774 | |
| 4775 | /* |
| 4776 | * Prepare the SSL_CTX based on the bind line configuration. |
| 4777 | * Since the CA file loading is made depending on the verify option of the bind |
| 4778 | * line, the link between the SSL_CTX and the CA file tree entry is made here. |
| 4779 | * If we want to create a link between the CA file entry and the corresponding |
| 4780 | * ckch instance (for CA file hot update), it needs to be done after |
| 4781 | * ssl_sock_prepare_ctx. |
| 4782 | * Returns 0 in case of success. |
| 4783 | */ |
| 4784 | int ssl_sock_prep_ctx_and_inst(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4785 | SSL_CTX *ctx, struct ckch_inst *ckch_inst, char **err) |
| 4786 | { |
| 4787 | int errcode = 0; |
| 4788 | |
| 4789 | errcode |= ssl_sock_prepare_ctx(bind_conf, ssl_conf, ctx, err); |
| 4790 | if (!errcode && ckch_inst) |
| 4791 | ckch_inst_add_cafile_link(ckch_inst, bind_conf, ssl_conf, NULL); |
| 4792 | |
| 4793 | return errcode; |
| 4794 | } |
| 4795 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4796 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4797 | { |
| 4798 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4799 | size_t prefixlen, suffixlen; |
| 4800 | |
| 4801 | /* Trivial case */ |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4802 | if (strcasecmp(pattern, hostname) == 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4803 | return 1; |
| 4804 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4805 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4806 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4807 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4808 | pattern_wildcard = NULL; |
| 4809 | pattern_left_label_end = pattern; |
| 4810 | while (*pattern_left_label_end != '.') { |
| 4811 | switch (*pattern_left_label_end) { |
| 4812 | case 0: |
| 4813 | /* End of label not found */ |
| 4814 | return 0; |
| 4815 | case '*': |
| 4816 | /* If there is more than one wildcards */ |
| 4817 | if (pattern_wildcard) |
| 4818 | return 0; |
| 4819 | pattern_wildcard = pattern_left_label_end; |
| 4820 | break; |
| 4821 | } |
| 4822 | pattern_left_label_end++; |
| 4823 | } |
| 4824 | |
| 4825 | /* If it's not trivial and there is no wildcard, it can't |
| 4826 | * match */ |
| 4827 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4828 | return 0; |
| 4829 | |
| 4830 | /* Make sure all labels match except the leftmost */ |
| 4831 | hostname_left_label_end = strchr(hostname, '.'); |
| 4832 | if (!hostname_left_label_end |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4833 | || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4834 | return 0; |
| 4835 | |
| 4836 | /* Make sure the leftmost label of the hostname is long enough |
| 4837 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4838 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4839 | return 0; |
| 4840 | |
| 4841 | /* Finally compare the string on either side of the |
| 4842 | * wildcard */ |
| 4843 | prefixlen = pattern_wildcard - pattern; |
| 4844 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4845 | if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0)) |
| 4846 | || (suffixlen && (strncasecmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4847 | return 0; |
| 4848 | |
| 4849 | return 1; |
| 4850 | } |
| 4851 | |
| 4852 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4853 | { |
| 4854 | SSL *ssl; |
| 4855 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4856 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4857 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4858 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4859 | |
| 4860 | int depth; |
| 4861 | X509 *cert; |
| 4862 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4863 | int i; |
| 4864 | X509_NAME *cert_subject; |
| 4865 | char *str; |
| 4866 | |
| 4867 | if (ok == 0) |
| 4868 | return ok; |
| 4869 | |
| 4870 | 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] | 4871 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 4872 | ssl_ctx = __conn_get_ssl_sock_ctx(conn); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4873 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4874 | /* We're checking if the provided hostnames match the desired one. The |
| 4875 | * desired hostname comes from the SNI we presented if any, or if not |
| 4876 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4877 | * directive. If neither is set, we don't care about the name so the |
| 4878 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4879 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4880 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4881 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4882 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4883 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4884 | if (!servername) |
| 4885 | return ok; |
| 4886 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4887 | |
| 4888 | /* We only need to verify the CN on the actual server cert, |
| 4889 | * not the indirect CAs */ |
| 4890 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4891 | if (depth != 0) |
| 4892 | return ok; |
| 4893 | |
| 4894 | /* At this point, the cert is *not* OK unless we can find a |
| 4895 | * hostname match */ |
| 4896 | ok = 0; |
| 4897 | |
| 4898 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4899 | /* It seems like this might happen if verify peer isn't set */ |
| 4900 | if (!cert) |
| 4901 | return ok; |
| 4902 | |
| 4903 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4904 | if (alt_names) { |
| 4905 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4906 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4907 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4908 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4909 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4910 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4911 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4912 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4913 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4914 | OPENSSL_free(str); |
| 4915 | } |
| 4916 | } |
| 4917 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4918 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4919 | } |
| 4920 | |
| 4921 | cert_subject = X509_get_subject_name(cert); |
| 4922 | i = -1; |
| 4923 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4924 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4925 | ASN1_STRING *value; |
| 4926 | value = X509_NAME_ENTRY_get_data(entry); |
| 4927 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4928 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4929 | OPENSSL_free(str); |
| 4930 | } |
| 4931 | } |
| 4932 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4933 | /* report the mismatch and indicate if SNI was used or not */ |
| 4934 | if (!ok && !conn->err_code) |
| 4935 | 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] | 4936 | return ok; |
| 4937 | } |
| 4938 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4939 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4940 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4941 | { |
| 4942 | int cfgerr = 0; |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 4943 | SSL_CTX *ctx; |
William Lallemand | 0adafb3 | 2022-12-13 18:17:44 +0100 | [diff] [blame] | 4944 | /* Automatic memory computations need to know we use SSL there |
| 4945 | * 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] | 4946 | if (!(srv->proxy->cap & PR_CAP_INT)) |
William Lallemand | 0adafb3 | 2022-12-13 18:17:44 +0100 | [diff] [blame] | 4947 | global.ssl_used_backend = 1; |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4948 | |
| 4949 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4950 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4951 | 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] | 4952 | ha_alert("out of memory.\n"); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4953 | cfgerr++; |
| 4954 | return cfgerr; |
| 4955 | } |
| 4956 | } |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 4957 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4958 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4959 | |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 4960 | if (srv->ssl_ctx.client_crt) { |
| 4961 | const int create_if_none = srv->flags & SRV_F_DYNAMIC ? 0 : 1; |
| 4962 | char *err = NULL; |
| 4963 | int err_code = 0; |
| 4964 | |
| 4965 | /* If there is a crt keyword there, the SSL_CTX will be created here. */ |
| 4966 | err_code = ssl_sock_load_srv_cert(srv->ssl_ctx.client_crt, srv, create_if_none, &err); |
| 4967 | if (err_code != ERR_NONE) { |
| 4968 | if ((err_code & ERR_WARN) && !(err_code & ERR_ALERT)) |
| 4969 | ha_warning("%s", err); |
| 4970 | else |
| 4971 | ha_alert("%s", err); |
| 4972 | |
| 4973 | if (err_code & (ERR_FATAL|ERR_ABORT)) |
| 4974 | cfgerr++; |
| 4975 | } |
| 4976 | ha_free(&err); |
| 4977 | } |
| 4978 | |
| 4979 | ctx = srv->ssl_ctx.ctx; |
| 4980 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4981 | /* The context will be uninitialized if there wasn't any "cert" option |
| 4982 | * in the server line. */ |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4983 | if (!ctx) { |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4984 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 4985 | if (!ctx) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 4986 | ha_alert("unable to allocate ssl context.\n"); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4987 | cfgerr++; |
| 4988 | return cfgerr; |
| 4989 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4990 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4991 | srv->ssl_ctx.ctx = ctx; |
| 4992 | } |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 4993 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4994 | 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] | 4995 | |
| 4996 | return cfgerr; |
| 4997 | } |
| 4998 | |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 4999 | /* Initialize an SSL context that will be used on the backend side. |
| 5000 | * Returns an error count. |
| 5001 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5002 | 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] | 5003 | { |
| 5004 | struct proxy *curproxy = srv->proxy; |
| 5005 | int cfgerr = 0; |
| 5006 | long options = |
| 5007 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5008 | SSL_OP_NO_SSLv2 | |
| 5009 | SSL_OP_NO_COMPRESSION; |
| 5010 | long mode = |
| 5011 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5012 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 5013 | SSL_MODE_RELEASE_BUFFERS | |
| 5014 | SSL_MODE_SMALL_BUFFERS; |
| 5015 | int verify = SSL_VERIFY_NONE; |
| 5016 | const struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
| 5017 | int i, min, max, hole; |
| 5018 | int flags = MC_SSL_O_ALL; |
| 5019 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5020 | 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] | 5021 | ha_warning("no-sslv3/no-tlsv1x are ignored for this server. " |
| 5022 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n"); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5023 | else |
| 5024 | flags = conf_ssl_methods->flags; |
| 5025 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5026 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5027 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5028 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5029 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5030 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5031 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5032 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5033 | min = max = CONF_TLSV_NONE; |
| 5034 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5035 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5036 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5037 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5038 | if (min) { |
| 5039 | if (hole) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 5040 | 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] | 5041 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5042 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5043 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5044 | hole = 0; |
| 5045 | } |
| 5046 | max = i; |
| 5047 | } |
| 5048 | else { |
| 5049 | min = max = i; |
| 5050 | } |
| 5051 | } |
| 5052 | else { |
| 5053 | if (min) |
| 5054 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5055 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5056 | if (!min) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 5057 | 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] | 5058 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5059 | cfgerr += 1; |
| 5060 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5061 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5062 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5063 | /* 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] | 5064 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5065 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5066 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5067 | else |
| 5068 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5069 | if (flags & methodVersions[i].flag) |
| 5070 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5071 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5072 | /* 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] | 5073 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5074 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5075 | #endif |
| 5076 | |
| 5077 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5078 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5079 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5080 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5081 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5082 | if (global_ssl.async) |
| 5083 | mode |= SSL_MODE_ASYNC; |
| 5084 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5085 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5086 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5087 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 5088 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5089 | switch (srv->ssl_ctx.verify) { |
| 5090 | case SSL_SOCK_VERIFY_NONE: |
| 5091 | verify = SSL_VERIFY_NONE; |
| 5092 | break; |
| 5093 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5094 | verify = SSL_VERIFY_PEER; |
| 5095 | break; |
| 5096 | } |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5097 | SSL_CTX_set_verify(ctx, verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5098 | (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] | 5099 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5100 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5101 | /* set CAfile to verify */ |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5102 | if (!ssl_set_verify_locations_file(ctx, srv->ssl_ctx.ca_file)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5103 | ha_alert("unable to set CA file '%s'.\n", |
| 5104 | srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5105 | cfgerr++; |
| 5106 | } |
| 5107 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5108 | else { |
| 5109 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5110 | 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] | 5111 | else |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5112 | ha_alert("verify is enabled but no CA file specified.\n"); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5113 | cfgerr++; |
| 5114 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5115 | #ifdef X509_V_FLAG_CRL_CHECK |
| 5116 | if (srv->ssl_ctx.crl_file) { |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5117 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5118 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5119 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5120 | ha_alert("unable to configure CRL file '%s'.\n", |
| 5121 | srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5122 | cfgerr++; |
| 5123 | } |
| 5124 | else { |
| 5125 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5126 | } |
| 5127 | } |
| 5128 | #endif |
| 5129 | } |
| 5130 | |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5131 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 5132 | SSL_CTX_sess_set_new_cb(ctx, ssl_sess_new_srv_cb); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5133 | if (srv->ssl_ctx.ciphers && |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5134 | !SSL_CTX_set_cipher_list(ctx, srv->ssl_ctx.ciphers)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5135 | ha_alert("unable to set SSL cipher list to '%s'.\n", |
| 5136 | srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5137 | cfgerr++; |
| 5138 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5139 | |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 5140 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5141 | if (srv->ssl_ctx.ciphersuites && |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5142 | !SSL_CTX_set_ciphersuites(ctx, srv->ssl_ctx.ciphersuites)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5143 | ha_alert("unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 5144 | srv->ssl_ctx.ciphersuites); |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5145 | cfgerr++; |
| 5146 | } |
| 5147 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5148 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 5149 | if (srv->ssl_ctx.npn_str) |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5150 | 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] | 5151 | #endif |
| 5152 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5153 | if (srv->ssl_ctx.alpn_str) |
| 5154 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 5155 | #endif |
| 5156 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5157 | |
| 5158 | return cfgerr; |
| 5159 | } |
| 5160 | |
| 5161 | /* |
| 5162 | * Prepare the frontend's SSL_CTX based on the server line configuration. |
| 5163 | * Since the CA file loading is made depending on the verify option of the |
| 5164 | * server line, the link between the SSL_CTX and the CA file tree entry is |
| 5165 | * made here. |
| 5166 | * If we want to create a link between the CA file entry and the corresponding |
| 5167 | * ckch instance (for CA file hot update), it needs to be done after |
| 5168 | * ssl_sock_prepare_srv_ssl_ctx. |
| 5169 | * Returns an error count. |
| 5170 | */ |
| 5171 | int ssl_sock_prep_srv_ctx_and_inst(const struct server *srv, SSL_CTX *ctx, |
| 5172 | struct ckch_inst *ckch_inst) |
| 5173 | { |
| 5174 | int cfgerr = 0; |
| 5175 | |
| 5176 | cfgerr += ssl_sock_prepare_srv_ssl_ctx(srv, ctx); |
| 5177 | if (!cfgerr && ckch_inst) |
| 5178 | ckch_inst_add_cafile_link(ckch_inst, NULL, NULL, srv); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5179 | |
| 5180 | return cfgerr; |
| 5181 | } |
| 5182 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5183 | |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5184 | /* |
| 5185 | * Create an initial CTX used to start the SSL connections. |
| 5186 | * May be used by QUIC xprt which makes usage of SSL sessions initialized from SSL_CTXs. |
| 5187 | * Returns 0 if succeeded, or something >0 if not. |
| 5188 | */ |
| 5189 | #ifdef USE_QUIC |
| 5190 | static int ssl_initial_ctx(struct bind_conf *bind_conf) |
| 5191 | { |
| 5192 | if (bind_conf->xprt == xprt_get(XPRT_QUIC)) |
| 5193 | return ssl_quic_initial_ctx(bind_conf); |
| 5194 | else |
| 5195 | return ssl_sock_initial_ctx(bind_conf); |
| 5196 | } |
| 5197 | #else |
| 5198 | static int ssl_initial_ctx(struct bind_conf *bind_conf) |
| 5199 | { |
| 5200 | return ssl_sock_initial_ctx(bind_conf); |
| 5201 | } |
| 5202 | #endif |
| 5203 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5204 | /* 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] | 5205 | * be NULL, in which case nothing is done. Returns the number of errors |
| 5206 | * encountered. |
| 5207 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5208 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5209 | { |
| 5210 | struct ebmb_node *node; |
| 5211 | struct sni_ctx *sni; |
| 5212 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5213 | int errcode = 0; |
| 5214 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5215 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5216 | /* Automatic memory computations need to know we use SSL there */ |
| 5217 | global.ssl_used_frontend = 1; |
| 5218 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5219 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 5220 | if (!bind_conf->initial_ctx) { |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5221 | err += ssl_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5222 | /* It should not be necessary to call this function, but it's |
| 5223 | necessary first to check and move all initialisation related |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5224 | to initial_ctx in ssl_initial_ctx. */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5225 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, NULL, bind_conf->initial_ctx, NULL, &errmsg); |
| 5226 | } |
| 5227 | if (bind_conf->default_ctx) { |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 5228 | 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] | 5229 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5230 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5231 | node = ebmb_first(&bind_conf->sni_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); |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +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); |
| 5238 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5239 | node = ebmb_next(node); |
| 5240 | } |
| 5241 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5242 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5243 | while (node) { |
| 5244 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5245 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5246 | /* only initialize the CTX on its first occurrence and |
| 5247 | if it is not the default_ctx */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5248 | 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] | 5249 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5250 | node = ebmb_next(node); |
| 5251 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5252 | |
| 5253 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5254 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5255 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5256 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5257 | err++; |
| 5258 | } |
| 5259 | |
| 5260 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5261 | return err; |
| 5262 | } |
| 5263 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5264 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 5265 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 5266 | * alerts are directly emitted since the rest of the stack does it below. |
| 5267 | */ |
| 5268 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 5269 | { |
| 5270 | struct proxy *px = bind_conf->frontend; |
| 5271 | int alloc_ctx; |
| 5272 | int err; |
| 5273 | |
Willy Tarreau | 11ba404 | 2022-05-20 15:56:32 +0200 | [diff] [blame] | 5274 | if (!(bind_conf->options & BC_O_USE_SSL)) { |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5275 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5276 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 5277 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5278 | } |
| 5279 | return 0; |
| 5280 | } |
| 5281 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5282 | if (bind_conf->strict_sni && !(bind_conf->options & BC_O_GENERATE_CERTS)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5283 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 5284 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5285 | } |
| 5286 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5287 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 5288 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5289 | return -1; |
| 5290 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5291 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 5292 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5293 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 5294 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
Willy Tarreau | 9135859 | 2021-06-15 08:08:04 +0200 | [diff] [blame] | 5295 | sizeof(*sh_ssl_sess_tree), (global.nbthread > 1)); |
Frédéric Lécaille | 4c8aa11 | 2018-10-25 20:22:46 +0200 | [diff] [blame] | 5296 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5297 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 5298 | 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"); |
| 5299 | else |
| 5300 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 5301 | return -1; |
| 5302 | } |
| 5303 | /* free block callback */ |
| 5304 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 5305 | /* init the root tree within the extra space */ |
| 5306 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 5307 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5308 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5309 | err = 0; |
| 5310 | /* initialize all certificate contexts */ |
| 5311 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 5312 | |
| 5313 | /* initialize CA variables if the certificates generation is enabled */ |
| 5314 | err += ssl_sock_load_ca(bind_conf); |
| 5315 | |
| 5316 | return -err; |
| 5317 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5318 | |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5319 | /* release ssl context allocated for servers. Most of the field free here |
| 5320 | * must also be allocated in srv_ssl_settings_cpy() */ |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5321 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 5322 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5323 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5324 | ha_free(&srv->ssl_ctx.alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5325 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 5326 | #ifdef OPENSSL_NPN_NEGOTIATED |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5327 | ha_free(&srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 5328 | #endif |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 5329 | if (srv->ssl_ctx.reused_sess) { |
| 5330 | int i; |
| 5331 | |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 5332 | for (i = 0; i < global.nbthread; i++) { |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5333 | ha_free(&srv->ssl_ctx.reused_sess[i].ptr); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 5334 | ha_free(&srv->ssl_ctx.reused_sess[i].sni); |
| 5335 | } |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5336 | ha_free(&srv->ssl_ctx.reused_sess); |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 5337 | } |
| 5338 | |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5339 | if (srv->ssl_ctx.ctx) { |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5340 | SSL_CTX_free(srv->ssl_ctx.ctx); |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5341 | srv->ssl_ctx.ctx = NULL; |
| 5342 | } |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5343 | |
| 5344 | ha_free(&srv->ssl_ctx.ca_file); |
| 5345 | ha_free(&srv->ssl_ctx.crl_file); |
| 5346 | ha_free(&srv->ssl_ctx.client_crt); |
| 5347 | ha_free(&srv->ssl_ctx.verify_host); |
| 5348 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5349 | ha_free(&srv->sni_expr); |
William Lallemand | 43c2ce4 | 2022-03-16 17:48:19 +0100 | [diff] [blame] | 5350 | release_sample_expr(srv->ssl_ctx.sni); |
| 5351 | srv->ssl_ctx.sni = NULL; |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5352 | #endif |
| 5353 | ha_free(&srv->ssl_ctx.ciphers); |
| 5354 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
| 5355 | ha_free(&srv->ssl_ctx.ciphersuites); |
| 5356 | #endif |
William Lallemand | e69563f | 2021-12-30 14:45:19 +0100 | [diff] [blame] | 5357 | /* If there is a certificate we must unlink the ckch instance */ |
| 5358 | ckch_inst_free(srv->ssl_ctx.inst); |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5359 | } |
| 5360 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5361 | /* 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] | 5362 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 5363 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5364 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5365 | { |
| 5366 | struct ebmb_node *node, *back; |
| 5367 | struct sni_ctx *sni; |
| 5368 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5369 | node = ebmb_first(&bind_conf->sni_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 | } |
| 5379 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5380 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5381 | while (node) { |
| 5382 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5383 | back = ebmb_next(node); |
| 5384 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5385 | SSL_CTX_free(sni->ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5386 | LIST_DELETE(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5387 | free(sni); |
| 5388 | node = back; |
| 5389 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 5390 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5391 | SSL_CTX_free(bind_conf->initial_ctx); |
| 5392 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5393 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5394 | bind_conf->default_ctx = NULL; |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 5395 | bind_conf->default_inst = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5396 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5397 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 5398 | |
| 5399 | |
| 5400 | void ssl_sock_deinit() |
| 5401 | { |
| 5402 | crtlist_deinit(); /* must be free'd before the ckchs */ |
| 5403 | ckch_deinit(); |
| 5404 | } |
| 5405 | REGISTER_POST_DEINIT(ssl_sock_deinit); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5406 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5407 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5408 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5409 | { |
| 5410 | ssl_sock_free_ca(bind_conf); |
| 5411 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5412 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5413 | free(bind_conf->ca_sign_file); |
| 5414 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5415 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5416 | free(bind_conf->keys_ref->filename); |
| 5417 | free(bind_conf->keys_ref->tlskeys); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5418 | LIST_DELETE(&bind_conf->keys_ref->list); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5419 | free(bind_conf->keys_ref); |
| 5420 | } |
| 5421 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5422 | bind_conf->ca_sign_pass = NULL; |
| 5423 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5424 | } |
| 5425 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5426 | /* Load CA cert file and private key used to generate certificates */ |
| 5427 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5428 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5429 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5430 | struct proxy *px = bind_conf->frontend; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5431 | struct ckch_data *data = NULL; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5432 | int ret = 0; |
| 5433 | char *err = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5434 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5435 | if (!(bind_conf->options & BC_O_GENERATE_CERTS)) |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5436 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5437 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5438 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5439 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5440 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5441 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5442 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5443 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5444 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5445 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5446 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5447 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5448 | "no CA certificate File configured at [%s:%d].\n", |
| 5449 | px->id, bind_conf->file, bind_conf->line); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5450 | goto failed; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5451 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5452 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5453 | /* Allocate cert structure */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5454 | data = calloc(1, sizeof(*data)); |
| 5455 | if (!data) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5456 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain allocation failure\n", |
| 5457 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 5458 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5459 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5460 | |
| 5461 | /* Try to parse file */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5462 | 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] | 5463 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain loading failed: %s\n", |
| 5464 | 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] | 5465 | free(err); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5466 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5467 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5468 | |
| 5469 | /* Fail if missing cert or pkey */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5470 | if ((!data->cert) || (!data->key)) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5471 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain missing certificate or private key\n", |
| 5472 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 5473 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5474 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5475 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5476 | /* Final assignment to bind */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5477 | bind_conf->ca_sign_ckch = data; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5478 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5479 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5480 | failed: |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5481 | if (data) { |
| 5482 | ssl_sock_free_cert_key_and_chain_contents(data); |
| 5483 | free(data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5484 | } |
| 5485 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5486 | bind_conf->options &= ~BC_O_GENERATE_CERTS; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5487 | ret++; |
| 5488 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5489 | } |
| 5490 | |
| 5491 | /* Release CA cert and private key used to generate certificated */ |
| 5492 | void |
| 5493 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5494 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5495 | if (bind_conf->ca_sign_ckch) { |
| 5496 | 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] | 5497 | ha_free(&bind_conf->ca_sign_ckch); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5498 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5499 | } |
| 5500 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5501 | /* |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5502 | * Try to allocate the BIO and SSL session objects of <conn> connection with <bio> and |
| 5503 | * <ssl> as addresses, <bio_meth> as BIO method and <ssl_ctx> as SSL context inherited settings. |
| 5504 | * Connect the allocated BIO to the allocated SSL session. Also set <ctx> as address of custom |
| 5505 | * 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] | 5506 | * 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] | 5507 | * as parameters to this function. |
| 5508 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <conn> to |
| 5509 | * CO_ER_SSL_NO_MEM. |
| 5510 | */ |
| 5511 | int ssl_bio_and_sess_init(struct connection *conn, SSL_CTX *ssl_ctx, |
| 5512 | SSL **ssl, BIO **bio, BIO_METHOD *bio_meth, void *ctx) |
| 5513 | { |
| 5514 | int retry = 1; |
| 5515 | |
| 5516 | retry: |
| 5517 | /* Alloc a new SSL session. */ |
| 5518 | *ssl = SSL_new(ssl_ctx); |
| 5519 | if (!*ssl) { |
| 5520 | if (!retry--) |
| 5521 | goto err; |
| 5522 | |
| 5523 | pool_gc(NULL); |
| 5524 | goto retry; |
| 5525 | } |
| 5526 | |
| 5527 | *bio = BIO_new(bio_meth); |
| 5528 | if (!*bio) { |
| 5529 | SSL_free(*ssl); |
| 5530 | *ssl = NULL; |
| 5531 | if (!retry--) |
| 5532 | goto err; |
| 5533 | |
| 5534 | pool_gc(NULL); |
| 5535 | goto retry; |
| 5536 | } |
| 5537 | |
| 5538 | BIO_set_data(*bio, ctx); |
| 5539 | SSL_set_bio(*ssl, *bio, *bio); |
| 5540 | |
| 5541 | /* set connection pointer. */ |
| 5542 | if (!SSL_set_ex_data(*ssl, ssl_app_data_index, conn)) { |
| 5543 | SSL_free(*ssl); |
| 5544 | *ssl = NULL; |
| 5545 | if (!retry--) |
| 5546 | goto err; |
| 5547 | |
| 5548 | pool_gc(NULL); |
| 5549 | goto retry; |
| 5550 | } |
| 5551 | |
| 5552 | return 0; |
| 5553 | |
| 5554 | err: |
| 5555 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5556 | return -1; |
| 5557 | } |
| 5558 | |
Olivier Houchard | bc5ce92 | 2021-03-05 23:47:00 +0100 | [diff] [blame] | 5559 | /* This function is called when all the XPRT have been initialized. We can |
| 5560 | * now attempt to start the SSL handshake. |
| 5561 | */ |
| 5562 | static int ssl_sock_start(struct connection *conn, void *xprt_ctx) |
| 5563 | { |
| 5564 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5565 | |
| 5566 | if (ctx->xprt->start) { |
| 5567 | int ret; |
| 5568 | |
| 5569 | ret = ctx->xprt->start(conn, ctx->xprt_ctx); |
| 5570 | if (ret < 0) |
| 5571 | return ret; |
| 5572 | } |
| 5573 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 5574 | |
| 5575 | return 0; |
| 5576 | } |
| 5577 | |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5578 | /* |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5579 | * This function is called if SSL * context is not yet allocated. The function |
| 5580 | * is designed to be called before any other data-layer operation and sets the |
| 5581 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5582 | * It returns 0 on success and -1 in error case. |
| 5583 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5584 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5585 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5586 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5587 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5588 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5589 | return 0; |
| 5590 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5591 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 5592 | if (!ctx) { |
| 5593 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5594 | return -1; |
| 5595 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5596 | ctx->wait_event.tasklet = tasklet_new(); |
| 5597 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5598 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5599 | pool_free(ssl_sock_ctx_pool, ctx); |
| 5600 | return -1; |
| 5601 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5602 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 5603 | ctx->wait_event.tasklet->context = ctx; |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5604 | 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] | 5605 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5606 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5607 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5608 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5609 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 5610 | ctx->xprt_st = 0; |
| 5611 | ctx->xprt_ctx = NULL; |
Remi Tricot-Le Breton | 1fe0fad | 2021-09-29 18:56:52 +0200 | [diff] [blame] | 5612 | ctx->error_code = 0; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5613 | |
| 5614 | /* Only work with sockets for now, this should be adapted when we'll |
| 5615 | * add QUIC support. |
| 5616 | */ |
| 5617 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5618 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5619 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 5620 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5621 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5622 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5623 | if (global.maxsslconn && global.sslconns >= global.maxsslconn) { |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5624 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5625 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5626 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5627 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5628 | /* If it is in client mode initiate SSL session |
| 5629 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5630 | if (objt_server(conn->target)) { |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5631 | if (ssl_bio_and_sess_init(conn, __objt_server(conn->target)->ssl_ctx.ctx, |
| 5632 | &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5633 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5634 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5635 | SSL_set_connect_state(ctx->ssl); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 5636 | 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] | 5637 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5638 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 5639 | 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] | 5640 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5641 | SSL_SESSION_free(sess); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 5642 | ha_free(&__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5643 | } else if (sess) { |
| 5644 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5645 | } |
| 5646 | } |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 5647 | 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] | 5648 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5649 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5650 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5651 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5652 | _HA_ATOMIC_INC(&global.sslconns); |
| 5653 | _HA_ATOMIC_INC(&global.totalsslconns); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5654 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5655 | return 0; |
| 5656 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5657 | else if (objt_listener(conn->target)) { |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5658 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5659 | |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5660 | if (ssl_bio_and_sess_init(conn, bc->initial_ctx, |
| 5661 | &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5662 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5663 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5664 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5665 | if (bc->ssl_conf.early_data) { |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 5666 | b_alloc(&ctx->early_buf); |
| 5667 | SSL_set_max_early_data(ctx->ssl, |
| 5668 | /* Only allow early data if we managed to allocate |
| 5669 | * a buffer. |
| 5670 | */ |
| 5671 | (!b_is_null(&ctx->early_buf)) ? |
| 5672 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 5673 | } |
| 5674 | #endif |
| 5675 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5676 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5677 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5678 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5679 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5680 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Willy Tarreau | a84986a | 2021-02-03 11:21:38 +0100 | [diff] [blame] | 5681 | if (bc->ssl_conf.early_data) |
| 5682 | conn->flags |= CO_FL_EARLY_SSL_HS; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5683 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5684 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5685 | _HA_ATOMIC_INC(&global.sslconns); |
| 5686 | _HA_ATOMIC_INC(&global.totalsslconns); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5687 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5688 | return 0; |
| 5689 | } |
| 5690 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5691 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5692 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5693 | if (ctx && ctx->wait_event.tasklet) |
| 5694 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5695 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5696 | return -1; |
| 5697 | } |
| 5698 | |
| 5699 | |
| 5700 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5701 | * updates the FD status if it wants some polling before being called again. |
| 5702 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5703 | * otherwise it returns non-zero and removes itself from the connection's |
| 5704 | * flags (the bit is provided in <flag> by the caller). |
| 5705 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5706 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5707 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 5708 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5709 | int ret; |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 5710 | struct ssl_counters *counters = NULL; |
| 5711 | struct ssl_counters *counters_px = NULL; |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 5712 | struct listener *li; |
| 5713 | struct server *srv; |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5714 | socklen_t lskerr; |
| 5715 | int skerr; |
| 5716 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5717 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5718 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5719 | return 0; |
| 5720 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5721 | /* get counters */ |
| 5722 | switch (obj_type(conn->target)) { |
| 5723 | case OBJ_TYPE_LISTENER: |
Amaury Denoyelle | 2bf5d41 | 2021-07-26 09:59:06 +0200 | [diff] [blame] | 5724 | li = __objt_listener(conn->target); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5725 | counters = EXTRA_COUNTERS_GET(li->extra_counters, &ssl_stats_module); |
| 5726 | counters_px = EXTRA_COUNTERS_GET(li->bind_conf->frontend->extra_counters_fe, |
| 5727 | &ssl_stats_module); |
| 5728 | break; |
| 5729 | |
| 5730 | case OBJ_TYPE_SERVER: |
Amaury Denoyelle | 2bf5d41 | 2021-07-26 09:59:06 +0200 | [diff] [blame] | 5731 | srv = __objt_server(conn->target); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5732 | counters = EXTRA_COUNTERS_GET(srv->extra_counters, &ssl_stats_module); |
| 5733 | counters_px = EXTRA_COUNTERS_GET(srv->proxy->extra_counters_be, |
| 5734 | &ssl_stats_module); |
| 5735 | break; |
| 5736 | |
| 5737 | default: |
| 5738 | break; |
| 5739 | } |
| 5740 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 5741 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5742 | goto out_error; |
| 5743 | |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5744 | /* don't start calculating a handshake on a dead connection */ |
| 5745 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) |
| 5746 | goto out_error; |
| 5747 | |
| 5748 | /* FIXME/WT: for now we don't have a clear way to inspect the connection |
| 5749 | * status from the lower layers, so let's check the FD directly. Ideally |
| 5750 | * the xprt layers should provide some status indicating their knowledge |
| 5751 | * of shutdowns or error. |
| 5752 | */ |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 5753 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 5754 | |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5755 | skerr = 0; |
| 5756 | lskerr = sizeof(skerr); |
| 5757 | if ((getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) < 0) || |
| 5758 | skerr != 0) |
| 5759 | goto out_error; |
| 5760 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5761 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5762 | /* |
| 5763 | * Check if we have early data. If we do, we have to read them |
| 5764 | * before SSL_do_handshake() is called, And there's no way to |
| 5765 | * detect early data, except to try to read them |
| 5766 | */ |
| 5767 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5768 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5769 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5770 | while (1) { |
| 5771 | ret = SSL_read_early_data(ctx->ssl, |
| 5772 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 5773 | &read_data); |
| 5774 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5775 | goto check_error; |
| 5776 | if (read_data > 0) { |
| 5777 | conn->flags |= CO_FL_EARLY_DATA; |
| 5778 | b_add(&ctx->early_buf, read_data); |
| 5779 | } |
| 5780 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5781 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5782 | if (!b_data(&ctx->early_buf)) |
| 5783 | b_free(&ctx->early_buf); |
| 5784 | break; |
| 5785 | } |
| 5786 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5787 | } |
| 5788 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5789 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5790 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5791 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5792 | * the reneg handshake. |
| 5793 | * Here we use SSL_peek as a workaround for reneg. |
| 5794 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5795 | 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] | 5796 | char c; |
| 5797 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5798 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5799 | if (ret <= 0) { |
| 5800 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5801 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5802 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5803 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5804 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5805 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5806 | 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] | 5807 | return 0; |
| 5808 | } |
| 5809 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5810 | /* handshake may have been completed but we have |
| 5811 | * no more data to read. |
| 5812 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5813 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5814 | ret = 1; |
| 5815 | goto reneg_ok; |
| 5816 | } |
| 5817 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5818 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5819 | 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] | 5820 | return 0; |
| 5821 | } |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5822 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5823 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5824 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5825 | return 0; |
| 5826 | } |
| 5827 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5828 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5829 | /* if errno is null, then connection was successfully established */ |
| 5830 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5831 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5832 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5833 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5834 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5835 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5836 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5837 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5838 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5839 | /* 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] | 5840 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5841 | empty_handshake = state == TLS_ST_BEFORE; |
| 5842 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5843 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5844 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5845 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5846 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5847 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5848 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5849 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5850 | else |
| 5851 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5852 | } |
| 5853 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5854 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5855 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5856 | else |
| 5857 | conn->err_code = CO_ER_SSL_ABORT; |
| 5858 | } |
| 5859 | } |
| 5860 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5861 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5862 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5863 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5864 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5865 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5866 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5867 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5868 | goto out_error; |
| 5869 | } |
| 5870 | else { |
| 5871 | /* Fail on all other handshake errors */ |
| 5872 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5873 | * buffer, causing an RST to be emitted upon close() on |
| 5874 | * TCP sockets. We first try to drain possibly pending |
| 5875 | * data to avoid this as much as possible. |
| 5876 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 5877 | conn_ctrl_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5878 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5879 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5880 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5881 | goto out_error; |
| 5882 | } |
| 5883 | } |
| 5884 | /* read some data: consider handshake completed */ |
| 5885 | goto reneg_ok; |
| 5886 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5887 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5888 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5889 | if (ret != 1) { |
| 5890 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5891 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5892 | |
Remi Tricot-Le Breton | 1fe0fad | 2021-09-29 18:56:52 +0200 | [diff] [blame] | 5893 | if (!ctx->error_code) |
| 5894 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 7c6898e | 2021-07-29 09:45:51 +0200 | [diff] [blame] | 5895 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5896 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5897 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5898 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5899 | 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] | 5900 | return 0; |
| 5901 | } |
| 5902 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5903 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5904 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5905 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5906 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5907 | return 0; |
| 5908 | } |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5909 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5910 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5911 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5912 | return 0; |
| 5913 | } |
| 5914 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5915 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5916 | /* if errno is null, then connection was successfully established */ |
| 5917 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5918 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5919 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5920 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5921 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5922 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5923 | #else |
| 5924 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5925 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5926 | /* 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] | 5927 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5928 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5929 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5930 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5931 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5932 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5933 | if (empty_handshake) { |
| 5934 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5935 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5936 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5937 | else |
| 5938 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5939 | } |
| 5940 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5941 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5942 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5943 | else |
| 5944 | conn->err_code = CO_ER_SSL_ABORT; |
| 5945 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5946 | } |
| 5947 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5948 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5949 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5950 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5951 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5952 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5953 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5954 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5955 | goto out_error; |
| 5956 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5957 | else { |
| 5958 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5959 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5960 | * buffer, causing an RST to be emitted upon close() on |
| 5961 | * TCP sockets. We first try to drain possibly pending |
| 5962 | * data to avoid this as much as possible. |
| 5963 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 5964 | conn_ctrl_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5965 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5966 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5967 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5968 | goto out_error; |
| 5969 | } |
| 5970 | } |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5971 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5972 | else { |
| 5973 | /* |
| 5974 | * If the server refused the early data, we have to send a |
| 5975 | * 425 to the client, as we no longer have the data to sent |
| 5976 | * them again. |
| 5977 | */ |
| 5978 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5979 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5980 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5981 | goto out_error; |
| 5982 | } |
| 5983 | } |
| 5984 | } |
| 5985 | #endif |
| 5986 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5987 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5988 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5989 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5990 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5991 | /* ASYNC engine API doesn't support moving read/write |
| 5992 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5993 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5994 | */ |
| 5995 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5996 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5997 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5998 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5999 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6000 | if (objt_server(conn->target)) { |
| 6001 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 6002 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 6003 | 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] | 6004 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6005 | else { |
| 6006 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 6007 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 6008 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 6009 | } |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 6010 | |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6011 | if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6012 | HA_ATOMIC_INC(&counters->sess); |
| 6013 | HA_ATOMIC_INC(&counters_px->sess); |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6014 | } |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 6015 | } |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6016 | else if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6017 | HA_ATOMIC_INC(&counters->reused_sess); |
| 6018 | HA_ATOMIC_INC(&counters_px->reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6019 | } |
| 6020 | |
| 6021 | /* The connection is now established at both layers, it's time to leave */ |
| 6022 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6023 | return 1; |
| 6024 | |
| 6025 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6026 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6027 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6028 | ERR_clear_error(); |
| 6029 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6030 | /* free resumed session if exists */ |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6031 | if (objt_server(conn->target)) { |
| 6032 | struct server *s = __objt_server(conn->target); |
| 6033 | /* RWLOCK: only rdlock the SSL cache even when writing in it because there is |
| 6034 | * one cache per thread, it only prevents to flush it from the CLI in |
| 6035 | * another thread */ |
| 6036 | |
| 6037 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 6038 | if (s->ssl_ctx.reused_sess[tid].ptr) |
| 6039 | ha_free(&s->ssl_ctx.reused_sess[tid].ptr); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6040 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6041 | } |
| 6042 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6043 | if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6044 | HA_ATOMIC_INC(&counters->failed_handshake); |
| 6045 | HA_ATOMIC_INC(&counters_px->failed_handshake); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6046 | } |
| 6047 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6048 | /* Fail on all other handshake errors */ |
| 6049 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6050 | if (!conn->err_code) |
| 6051 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6052 | return 0; |
| 6053 | } |
| 6054 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6055 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6056 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6057 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6058 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6059 | * unless the transport layer was already released. |
| 6060 | */ |
| 6061 | 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] | 6062 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6063 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6064 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6065 | if (!ctx) |
| 6066 | return -1; |
| 6067 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6068 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6069 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6070 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6071 | ctx->subs = es; |
| 6072 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6073 | |
| 6074 | /* we may have to subscribe to lower layers for new events */ |
| 6075 | event_type &= ~ctx->wait_event.events; |
| 6076 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6077 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6078 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6079 | } |
| 6080 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6081 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6082 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6083 | * subscribe() call. It always returns zero. |
| 6084 | */ |
| 6085 | 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] | 6086 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6087 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6088 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6089 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6090 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6091 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6092 | es->events &= ~event_type; |
| 6093 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6094 | ctx->subs = NULL; |
| 6095 | |
| 6096 | /* If we subscribed, and we're not doing the handshake, |
| 6097 | * then we subscribed because the upper layer asked for it, |
| 6098 | * as the upper layer is no longer interested, we can |
| 6099 | * unsubscribe too. |
| 6100 | */ |
| 6101 | event_type &= ctx->wait_event.events; |
| 6102 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6103 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6104 | |
| 6105 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6106 | } |
| 6107 | |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6108 | /* The connection has been taken over, so destroy the old tasklet and create |
| 6109 | * a new one. The original thread ID must be passed into orig_tid |
| 6110 | * It should be called with the takeover lock for the old thread held. |
| 6111 | * Returns 0 on success, and -1 on failure |
| 6112 | */ |
| 6113 | static int ssl_takeover(struct connection *conn, void *xprt_ctx, int orig_tid) |
| 6114 | { |
| 6115 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6116 | struct tasklet *tl = tasklet_new(); |
| 6117 | |
| 6118 | if (!tl) |
| 6119 | return -1; |
| 6120 | |
| 6121 | ctx->wait_event.tasklet->context = NULL; |
| 6122 | tasklet_wakeup_on(ctx->wait_event.tasklet, orig_tid); |
| 6123 | ctx->wait_event.tasklet = tl; |
| 6124 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6125 | ctx->wait_event.tasklet->context = ctx; |
| 6126 | return 0; |
| 6127 | } |
| 6128 | |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6129 | /* notify the next xprt that the connection is about to become idle and that it |
| 6130 | * may be stolen at any time after the function returns and that any tasklet in |
| 6131 | * the chain must be careful before dereferencing its context. |
| 6132 | */ |
| 6133 | static void ssl_set_idle(struct connection *conn, void *xprt_ctx) |
| 6134 | { |
| 6135 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6136 | |
| 6137 | if (!ctx || !ctx->wait_event.tasklet) |
| 6138 | return; |
| 6139 | |
| 6140 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 6141 | if (ctx->xprt) |
| 6142 | xprt_set_idle(conn, ctx->xprt, ctx->xprt_ctx); |
| 6143 | } |
| 6144 | |
| 6145 | /* notify the next xprt that the connection is not idle anymore and that it may |
| 6146 | * not be stolen before the next xprt_set_idle(). |
| 6147 | */ |
| 6148 | static void ssl_set_used(struct connection *conn, void *xprt_ctx) |
| 6149 | { |
| 6150 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6151 | |
| 6152 | if (!ctx || !ctx->wait_event.tasklet) |
| 6153 | return; |
| 6154 | |
| 6155 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 6156 | if (ctx->xprt) |
| 6157 | xprt_set_used(conn, ctx->xprt, ctx->xprt_ctx); |
| 6158 | } |
| 6159 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6160 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6161 | * Returns 0 on success, and non-zero on failure. |
| 6162 | */ |
| 6163 | 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) |
| 6164 | { |
| 6165 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6166 | |
| 6167 | if (oldxprt_ops != NULL) |
| 6168 | *oldxprt_ops = ctx->xprt; |
| 6169 | if (oldxprt_ctx != NULL) |
| 6170 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6171 | ctx->xprt = toadd_ops; |
| 6172 | ctx->xprt_ctx = toadd_ctx; |
| 6173 | return 0; |
| 6174 | } |
| 6175 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6176 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6177 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6178 | * XPRT. |
| 6179 | */ |
| 6180 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6181 | { |
| 6182 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6183 | |
| 6184 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6185 | ctx->xprt_ctx = newctx; |
| 6186 | ctx->xprt = newops; |
| 6187 | return 0; |
| 6188 | } |
| 6189 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6190 | } |
| 6191 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6192 | 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] | 6193 | { |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6194 | struct tasklet *tl = (struct tasklet *)t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6195 | struct ssl_sock_ctx *ctx = context; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6196 | struct connection *conn; |
| 6197 | int conn_in_list; |
| 6198 | int ret = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6199 | |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6200 | if (state & TASK_F_USR1) { |
| 6201 | /* the tasklet was idling on an idle connection, it might have |
| 6202 | * been stolen, let's be careful! |
| 6203 | */ |
| 6204 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6205 | if (tl->context == NULL) { |
| 6206 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6207 | tasklet_free(tl); |
| 6208 | return NULL; |
| 6209 | } |
| 6210 | conn = ctx->conn; |
Christopher Faulet | 3a7b539 | 2023-03-16 11:43:05 +0100 | [diff] [blame] | 6211 | conn_in_list = conn_get_idle_flag(conn); |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6212 | if (conn_in_list) |
| 6213 | conn_delete_from_tree(&conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6214 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6215 | } else { |
| 6216 | conn = ctx->conn; |
| 6217 | conn_in_list = 0; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6218 | } |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6219 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6220 | /* First if we're doing an handshake, try that */ |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6221 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6222 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6223 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6224 | /* handshake completed, leave the bulk queue */ |
Willy Tarreau | 4c48edb | 2021-03-09 17:58:02 +0100 | [diff] [blame] | 6225 | _HA_ATOMIC_AND(&tl->state, ~TASK_HEAVY); |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6226 | } |
| 6227 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6228 | /* If we had an error, or the handshake is done and I/O is available, |
| 6229 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6230 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6231 | * we can't be sure conn_fd_handler() will be called again. |
| 6232 | */ |
| 6233 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6234 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6235 | int woke = 0; |
| 6236 | |
| 6237 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6238 | if (ctx->subs) { |
| 6239 | tasklet_wakeup(ctx->subs->tasklet); |
| 6240 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6241 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6242 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6243 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6244 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6245 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6246 | * upper layers know. If we have no mux, create it, |
| 6247 | * and once we have a mux, call its wake method if we didn't |
| 6248 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6249 | */ |
| 6250 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6251 | if (!ctx->conn->mux) |
| 6252 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6253 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6254 | ret = ctx->conn->mux->wake(ctx->conn); |
| 6255 | goto leave; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6256 | } |
| 6257 | } |
Ilya Shipitsin | 761d64c | 2021-01-07 11:59:58 +0500 | [diff] [blame] | 6258 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6259 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6260 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6261 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6262 | tasklet_wakeup(ctx->subs->tasklet); |
| 6263 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6264 | if (!ctx->subs->events) |
| 6265 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6266 | } |
| 6267 | #endif |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6268 | leave: |
| 6269 | if (!ret && conn_in_list) { |
| 6270 | struct server *srv = objt_server(conn->target); |
| 6271 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6272 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6273 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6274 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6275 | else |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6276 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6277 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6278 | } |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 6279 | return t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6280 | } |
| 6281 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6282 | /* 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] | 6283 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6284 | * buffer wraps, in which case a second call may be performed. The connection's |
| 6285 | * flags are updated with whatever special event is detected (error, read0, |
| 6286 | * empty). The caller is responsible for taking care of those events and |
| 6287 | * avoiding the call if inappropriate. The function does not call the |
| 6288 | * connection's polling update function, so the caller is responsible for this. |
| 6289 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6290 | 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] | 6291 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6292 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 6293 | ssize_t ret; |
| 6294 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6295 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6296 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6297 | goto out_error; |
| 6298 | |
Ilya Shipitsin | 761d64c | 2021-01-07 11:59:58 +0500 | [diff] [blame] | 6299 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6300 | if (b_data(&ctx->early_buf)) { |
| 6301 | try = b_contig_space(buf); |
| 6302 | if (try > b_data(&ctx->early_buf)) |
| 6303 | try = b_data(&ctx->early_buf); |
| 6304 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 6305 | b_add(buf, try); |
| 6306 | b_del(&ctx->early_buf, try); |
| 6307 | if (b_data(&ctx->early_buf) == 0) |
| 6308 | b_free(&ctx->early_buf); |
| 6309 | return try; |
| 6310 | } |
| 6311 | #endif |
| 6312 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6313 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6314 | /* a handshake was requested */ |
| 6315 | return 0; |
| 6316 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6317 | /* read the largest possible block. For this, we perform only one call |
| 6318 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 6319 | * in which case we accept to do it once again. A new attempt is made on |
| 6320 | * EINTR too. |
| 6321 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 6322 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6323 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6324 | try = b_contig_space(buf); |
| 6325 | if (!try) |
| 6326 | break; |
| 6327 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6328 | if (try > count) |
| 6329 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6330 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6331 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6332 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6333 | if (conn->flags & CO_FL_ERROR) { |
| 6334 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6335 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6336 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6337 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6338 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6339 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6340 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6341 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6342 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6343 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6344 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6345 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6346 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6347 | 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] | 6348 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6349 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6350 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6351 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6352 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6353 | break; |
| 6354 | } |
| 6355 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6356 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6357 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6358 | SUB_RETRY_RECV, |
| 6359 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6360 | /* handshake is running, and it may need to re-enable read */ |
| 6361 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6362 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6363 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6364 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6365 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6366 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6367 | break; |
| 6368 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6369 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6370 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 6371 | goto read0; |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6372 | else if (ret == SSL_ERROR_SSL) { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6373 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
| 6374 | if (ctx && !ctx->error_code) |
Remi Tricot-Le Breton | 9543d5a | 2021-09-29 18:56:53 +0200 | [diff] [blame] | 6375 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6376 | conn->err_code = CO_ERR_SSL_FATAL; |
| 6377 | } |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6378 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 6379 | * stack before shutting down the connection for |
| 6380 | * reading. */ |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 6381 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN || errno == EWOULDBLOCK)) |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6382 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6383 | /* otherwise it's a real error */ |
| 6384 | goto out_error; |
| 6385 | } |
| 6386 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6387 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6388 | return done; |
| 6389 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6390 | clear_ssl_error: |
| 6391 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6392 | ssl_sock_dump_errors(conn, NULL); |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6393 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6394 | read0: |
| 6395 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6396 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6397 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6398 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6399 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6400 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6401 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6402 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6403 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6404 | } |
| 6405 | |
| 6406 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6407 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 6408 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 6409 | * 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] | 6410 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 6411 | * a second call may be performed. The connection's flags are updated with |
| 6412 | * whatever special event is detected (error, empty). The caller is responsible |
| 6413 | * for taking care of those events and avoiding the call if inappropriate. The |
| 6414 | * 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] | 6415 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 6416 | * caller to take care of this. It's up to the caller to update the buffer's |
| 6417 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6418 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6419 | 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] | 6420 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6421 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6422 | ssize_t ret; |
| 6423 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6424 | |
| 6425 | done = 0; |
| 6426 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6427 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6428 | goto out_error; |
| 6429 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6430 | 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] | 6431 | /* a handshake was requested */ |
| 6432 | return 0; |
| 6433 | |
| 6434 | /* send the largest possible block. For this we perform only one call |
| 6435 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 6436 | * in which case we accept to do it once again. |
| 6437 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6438 | while (count) { |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6439 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6440 | size_t written_data; |
| 6441 | #endif |
| 6442 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6443 | try = b_contig_data(buf, done); |
| 6444 | if (try > count) |
| 6445 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6446 | |
Thomas Prückl | 1024393 | 2022-04-27 13:04:54 +0200 | [diff] [blame] | 6447 | if (global_ssl.hard_max_record && try > global_ssl.hard_max_record) |
| 6448 | try = global_ssl.hard_max_record; |
| 6449 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 6450 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6451 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6452 | global_ssl.max_record && try > global_ssl.max_record) { |
| 6453 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6454 | } |
| 6455 | else { |
| 6456 | /* we need to keep the information about the fact that |
| 6457 | * we're not limiting the upcoming send(), because if it |
| 6458 | * fails, we'll have to retry with at least as many data. |
| 6459 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6460 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6461 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6462 | |
Willy Tarreau | ac78c4f | 2023-03-17 16:13:05 +0100 | [diff] [blame] | 6463 | if (try < count || flags & CO_SFL_MSG_MORE) |
| 6464 | ctx->xprt_st |= SSL_SOCK_SEND_MORE; |
| 6465 | else |
| 6466 | ctx->xprt_st &= ~SSL_SOCK_SEND_MORE; |
| 6467 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6468 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6469 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6470 | unsigned int max_early; |
| 6471 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6472 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6473 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6474 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6475 | if (SSL_get0_session(ctx->ssl)) |
| 6476 | 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] | 6477 | else |
| 6478 | max_early = 0; |
| 6479 | } |
| 6480 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6481 | if (try + ctx->sent_early_data > max_early) { |
| 6482 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6483 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6484 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6485 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6486 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6487 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6488 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6489 | 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] | 6490 | if (ret == 1) { |
| 6491 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6492 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6493 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6494 | 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] | 6495 | /* Initiate the handshake, now */ |
| 6496 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 6497 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6498 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6499 | } |
| 6500 | |
| 6501 | } else |
| 6502 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6503 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6504 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6505 | if (conn->flags & CO_FL_ERROR) { |
| 6506 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6507 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6508 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6509 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6510 | /* A send succeeded, so we can consider ourself connected */ |
| 6511 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6512 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6513 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6514 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6515 | } |
| 6516 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6517 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6518 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6519 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6520 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6521 | /* handshake is running, and it may need to re-enable write */ |
| 6522 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6523 | 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] | 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 |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6529 | break; |
| 6530 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6531 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6532 | break; |
| 6533 | } |
| 6534 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6535 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6536 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6537 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6538 | SUB_RETRY_RECV, |
| 6539 | &ctx->wait_event); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6540 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6541 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6542 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6543 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6544 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6545 | break; |
| 6546 | } |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6547 | else if (ret == SSL_ERROR_SSL || ret == SSL_ERROR_SYSCALL) { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6548 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
| 6549 | |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 6550 | if (ctx && !ctx->error_code) |
Remi Tricot-Le Breton | 9543d5a | 2021-09-29 18:56:53 +0200 | [diff] [blame] | 6551 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6552 | conn->err_code = CO_ERR_SSL_FATAL; |
| 6553 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6554 | goto out_error; |
| 6555 | } |
| 6556 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6557 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6558 | return done; |
| 6559 | |
| 6560 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6561 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6562 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6563 | ERR_clear_error(); |
| 6564 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6565 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6566 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6567 | } |
| 6568 | |
Willy Tarreau | 832e242 | 2021-05-13 10:11:03 +0200 | [diff] [blame] | 6569 | void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6570 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6571 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6572 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6573 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6574 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6575 | if (ctx->wait_event.events != 0) |
| 6576 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 6577 | ctx->wait_event.events, |
| 6578 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6579 | if (ctx->subs) { |
| 6580 | ctx->subs->events = 0; |
| 6581 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6582 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6583 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 6584 | if (ctx->xprt->close) |
| 6585 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6586 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6587 | if (global_ssl.async) { |
| 6588 | OSSL_ASYNC_FD all_fd[32], afd; |
| 6589 | size_t num_all_fds = 0; |
| 6590 | int i; |
| 6591 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6592 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6593 | if (num_all_fds > 32) { |
| 6594 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 6595 | return; |
| 6596 | } |
| 6597 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6598 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6599 | |
| 6600 | /* If an async job is pending, we must try to |
| 6601 | to catch the end using polling before calling |
| 6602 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6603 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6604 | for (i=0 ; i < num_all_fds ; i++) { |
| 6605 | /* switch on an handler designed to |
| 6606 | * handle the SSL_free |
| 6607 | */ |
| 6608 | afd = all_fd[i]; |
| 6609 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6610 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6611 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 6612 | /* To ensure that the fd cache won't be used |
| 6613 | * and we'll catch a real RD event. |
| 6614 | */ |
| 6615 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6616 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6617 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6618 | pool_free(ssl_sock_ctx_pool, ctx); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 6619 | _HA_ATOMIC_INC(&jobs); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6620 | return; |
| 6621 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6622 | /* Else we can remove the fds from the fdtab |
| 6623 | * and call SSL_free. |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 6624 | * note: we do a fd_stop_both and not a delete |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6625 | * because the fd is owned by the engine. |
| 6626 | * the engine is responsible to close |
| 6627 | */ |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 6628 | for (i=0 ; i < num_all_fds ; i++) { |
| 6629 | /* We want to remove the fd from the fdtab |
| 6630 | * but we flag it to disown because the |
| 6631 | * close is performed by the engine itself |
| 6632 | */ |
| 6633 | fdtab[all_fd[i]].state |= FD_DISOWN; |
| 6634 | fd_delete(all_fd[i]); |
| 6635 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6636 | } |
| 6637 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6638 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6639 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6640 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6641 | pool_free(ssl_sock_ctx_pool, ctx); |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 6642 | _HA_ATOMIC_DEC(&global.sslconns); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6643 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6644 | } |
| 6645 | |
| 6646 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 6647 | * any case, flags the connection as reusable if no handshake was in progress. |
| 6648 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6649 | 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] | 6650 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6651 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6652 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6653 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6654 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 6655 | if (!clean) |
| 6656 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6657 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6658 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6659 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6660 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6661 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6662 | ERR_clear_error(); |
| 6663 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6664 | } |
| 6665 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6666 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 6667 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6668 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 6669 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6670 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6671 | X509 *crt; |
| 6672 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6673 | if (!ctx) |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6674 | return 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6675 | crt = SSL_get_certificate(ctx->ssl); |
| 6676 | if (!crt) |
| 6677 | return 0; |
| 6678 | |
| 6679 | return cert_get_pkey_algo(crt, out); |
| 6680 | } |
| 6681 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6682 | /* used for ppv2 cert signature (can be used for logging) */ |
| 6683 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 6684 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6685 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6686 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6687 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 6688 | X509 *crt; |
| 6689 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6690 | if (!ctx) |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6691 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6692 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6693 | if (!crt) |
| 6694 | return NULL; |
| 6695 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6696 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6697 | } |
| 6698 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6699 | /* used for ppv2 authority */ |
| 6700 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6701 | { |
| 6702 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6703 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6704 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6705 | if (!ctx) |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6706 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6707 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6708 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6709 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6710 | #endif |
| 6711 | } |
| 6712 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6713 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6714 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6715 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6716 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6717 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6718 | if (!ctx) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6719 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6720 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6721 | } |
| 6722 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6723 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6724 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 6725 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6726 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6727 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6728 | if (!ctx) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6729 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6730 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6731 | } |
| 6732 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6733 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6734 | { |
| 6735 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6736 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6737 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6738 | if (!ctx) |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 6739 | return; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6740 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6741 | #endif |
| 6742 | } |
| 6743 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6744 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6745 | * to disable SNI. |
| 6746 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6747 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6748 | { |
| 6749 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6750 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6751 | struct server *s; |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6752 | char *prev_name; |
| 6753 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6754 | if (!ctx) |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6755 | return; |
Willy Tarreau | 77bfa66 | 2021-12-23 11:12:13 +0100 | [diff] [blame] | 6756 | |
| 6757 | BUG_ON(!(conn->flags & CO_FL_WAIT_L6_CONN)); |
| 6758 | BUG_ON(!(conn->flags & CO_FL_SSL_WAIT_HS)); |
| 6759 | |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6760 | s = __objt_server(conn->target); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6761 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6762 | /* 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] | 6763 | * new connection will present a new SNI. compare with the SNI |
| 6764 | * previously stored in the reused_sess */ |
| 6765 | /* the RWLOCK is used to ensure that we are not trying to flush the |
| 6766 | * cache from the CLI */ |
| 6767 | |
| 6768 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
| 6769 | prev_name = s->ssl_ctx.reused_sess[tid].sni; |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6770 | if ((!prev_name && hostname) || |
| 6771 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6772 | SSL_set_session(ctx->ssl, NULL); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6773 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6774 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6775 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6776 | #endif |
| 6777 | } |
| 6778 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6779 | /* Extract peer certificate's common name into the chunk dest |
| 6780 | * Returns |
| 6781 | * the len of the extracted common name |
| 6782 | * or 0 if no CN found in DN |
| 6783 | * or -1 on error case (i.e. no peer certificate) |
| 6784 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6785 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6786 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6787 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6788 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6789 | X509 *crt = NULL; |
| 6790 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6791 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6792 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6793 | .area = (char *)&find_cn, |
| 6794 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6795 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6796 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6797 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6798 | if (!ctx) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6799 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6800 | |
| 6801 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6802 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6803 | if (!crt) |
| 6804 | goto out; |
| 6805 | |
| 6806 | name = X509_get_subject_name(crt); |
| 6807 | if (!name) |
| 6808 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6809 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6810 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6811 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6812 | if (crt) |
| 6813 | X509_free(crt); |
| 6814 | |
| 6815 | return result; |
| 6816 | } |
| 6817 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6818 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6819 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6820 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6821 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6822 | X509 *crt = NULL; |
| 6823 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6824 | if (!ctx) |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6825 | return 0; |
| 6826 | |
| 6827 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6828 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6829 | if (!crt) |
| 6830 | return 0; |
| 6831 | |
| 6832 | X509_free(crt); |
| 6833 | return 1; |
| 6834 | } |
| 6835 | |
| 6836 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6837 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6838 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6839 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6840 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6841 | if (!ctx) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6842 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6843 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6844 | } |
| 6845 | |
| 6846 | /* returns result from SSL verify */ |
| 6847 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6848 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6849 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6850 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6851 | if (!ctx) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6852 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6853 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6854 | } |
| 6855 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6856 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6857 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6858 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6859 | * freed by the caller. NPN is also checked if available since older versions |
| 6860 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6861 | */ |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 6862 | 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] | 6863 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6864 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6865 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6866 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6867 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6868 | return 0; |
| 6869 | |
| 6870 | *str = NULL; |
| 6871 | |
| 6872 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6873 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6874 | if (*str) |
| 6875 | return 1; |
| 6876 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6877 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6878 | 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] | 6879 | if (*str) |
| 6880 | return 1; |
| 6881 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6882 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6883 | return 0; |
| 6884 | } |
| 6885 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6886 | /* "issuers-chain-path" load chain certificate in global */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6887 | 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] | 6888 | { |
| 6889 | X509 *ca; |
| 6890 | X509_NAME *name = NULL; |
| 6891 | ASN1_OCTET_STRING *skid = NULL; |
| 6892 | STACK_OF(X509) *chain = NULL; |
| 6893 | struct issuer_chain *issuer; |
| 6894 | struct eb64_node *node; |
| 6895 | char *path; |
| 6896 | u64 key; |
| 6897 | int ret = 0; |
| 6898 | |
| 6899 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 6900 | if (chain == NULL) { |
| 6901 | chain = sk_X509_new_null(); |
| 6902 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 6903 | name = X509_get_subject_name(ca); |
| 6904 | } |
| 6905 | if (!sk_X509_push(chain, ca)) { |
| 6906 | X509_free(ca); |
| 6907 | goto end; |
| 6908 | } |
| 6909 | } |
| 6910 | if (!chain) { |
| 6911 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 6912 | goto end; |
| 6913 | } |
| 6914 | if (!skid) { |
| 6915 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 6916 | goto end; |
| 6917 | } |
| 6918 | if (!name) { |
| 6919 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 6920 | goto end; |
| 6921 | } |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 6922 | key = XXH3(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6923 | 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] | 6924 | issuer = container_of(node, typeof(*issuer), node); |
| 6925 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 6926 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 6927 | goto end; |
| 6928 | } |
| 6929 | } |
| 6930 | issuer = calloc(1, sizeof *issuer); |
| 6931 | path = strdup(fp); |
| 6932 | if (!issuer || !path) { |
| 6933 | free(issuer); |
| 6934 | free(path); |
| 6935 | goto end; |
| 6936 | } |
| 6937 | issuer->node.key = key; |
| 6938 | issuer->path = path; |
| 6939 | issuer->chain = chain; |
| 6940 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6941 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6942 | ret = 1; |
| 6943 | end: |
| 6944 | if (skid) |
| 6945 | ASN1_OCTET_STRING_free(skid); |
| 6946 | if (chain) |
| 6947 | sk_X509_pop_free(chain, X509_free); |
| 6948 | return ret; |
| 6949 | } |
| 6950 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 6951 | struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6952 | { |
| 6953 | AUTHORITY_KEYID *akid; |
| 6954 | struct issuer_chain *issuer = NULL; |
| 6955 | |
| 6956 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
William Lallemand | f69cd68 | 2020-11-19 16:24:13 +0100 | [diff] [blame] | 6957 | if (akid && akid->keyid) { |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6958 | struct eb64_node *node; |
| 6959 | u64 hk; |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 6960 | 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] | 6961 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 6962 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 6963 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 6964 | issuer = ti; |
| 6965 | break; |
| 6966 | } |
| 6967 | } |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6968 | } |
Remi Tricot-Le Breton | 1621dc1 | 2022-11-03 15:16:48 +0100 | [diff] [blame] | 6969 | AUTHORITY_KEYID_free(akid); |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6970 | return issuer; |
| 6971 | } |
| 6972 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6973 | void ssl_free_global_issuers(void) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6974 | { |
| 6975 | struct eb64_node *node, *back; |
| 6976 | struct issuer_chain *issuer; |
| 6977 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6978 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6979 | while (node) { |
| 6980 | issuer = container_of(node, typeof(*issuer), node); |
| 6981 | back = eb64_next(node); |
| 6982 | eb64_delete(node); |
| 6983 | free(issuer->path); |
| 6984 | sk_X509_pop_free(issuer->chain, X509_free); |
| 6985 | free(issuer); |
| 6986 | node = back; |
| 6987 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 6990 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6991 | static int ssl_check_async_engine_count(void) { |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 6992 | int err_code = ERR_NONE; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6993 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6994 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6995 | 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] | 6996 | err_code = ERR_ABORT; |
| 6997 | } |
| 6998 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6999 | } |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7000 | #endif |
| 7001 | |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7002 | /* "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] | 7003 | * 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] | 7004 | */ |
Willy Tarreau | 8050efe | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 7005 | 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] | 7006 | { |
| 7007 | const struct ssl_sock_ctx *sctx = ctx; |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7008 | int ret = 0; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7009 | |
| 7010 | if (!sctx) |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7011 | return ret; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7012 | |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7013 | if (sctx->conn != conn) { |
| 7014 | chunk_appendf(&trash, " xctx.conn=%p(BOGUS)", sctx->conn); |
| 7015 | ret = 1; |
| 7016 | } |
Christopher Faulet | f19c639 | 2023-03-14 15:51:33 +0100 | [diff] [blame] | 7017 | chunk_appendf(&trash, " xctx.st=%d .err=%ld", sctx->xprt_st, sctx->error_code); |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7018 | |
| 7019 | if (sctx->xprt) { |
| 7020 | chunk_appendf(&trash, " .xprt=%s", sctx->xprt->name); |
| 7021 | if (sctx->xprt_ctx) |
| 7022 | chunk_appendf(&trash, " .xctx=%p", sctx->xprt_ctx); |
| 7023 | } |
| 7024 | |
| 7025 | chunk_appendf(&trash, " .wait.ev=%d", sctx->wait_event.events); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7026 | |
| 7027 | /* as soon as a shutdown is reported the lower layer unregisters its |
| 7028 | * subscriber, so the situations below are transient and rare enough to |
| 7029 | * be reported as suspicious. In any case they shouldn't last. |
| 7030 | */ |
| 7031 | if ((sctx->wait_event.events & 1) && (conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_ERROR))) |
| 7032 | ret = 1; |
| 7033 | if ((sctx->wait_event.events & 2) && (conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR))) |
| 7034 | ret = 1; |
| 7035 | |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7036 | chunk_appendf(&trash, " .subs=%p", sctx->subs); |
| 7037 | if (sctx->subs) { |
| 7038 | 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] | 7039 | if (sctx->subs->tasklet->calls >= 1000000) |
| 7040 | ret = 1; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7041 | chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 7042 | sctx->subs->tasklet->calls, |
| 7043 | sctx->subs->tasklet->context); |
| 7044 | resolve_sym_name(&trash, NULL, sctx->subs->tasklet->process); |
| 7045 | chunk_appendf(&trash, ")"); |
| 7046 | } |
| 7047 | chunk_appendf(&trash, " .sent_early=%d", sctx->sent_early_data); |
| 7048 | chunk_appendf(&trash, " .early_in=%d", (int)sctx->early_buf.data); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7049 | return ret; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7050 | } |
| 7051 | |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7052 | #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] | 7053 | /* 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] | 7054 | * each reference. The variable <ref> must point to the current node's list |
| 7055 | * 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] | 7056 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7057 | static inline |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7058 | 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] | 7059 | { |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7060 | /* Get next list entry. */ |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7061 | ref = ref->n; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7062 | |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7063 | /* If the entry is the last of the list, return NULL. */ |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7064 | if (ref == end) |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7065 | return NULL; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7066 | |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7067 | return LIST_ELEM(ref, struct tls_keys_ref *, list); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7068 | } |
| 7069 | |
| 7070 | static inline |
| 7071 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 7072 | { |
| 7073 | int id; |
| 7074 | char *error; |
| 7075 | |
| 7076 | /* If the reference starts by a '#', this is numeric id. */ |
| 7077 | if (reference[0] == '#') { |
| 7078 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 7079 | id = strtol(reference + 1, &error, 10); |
| 7080 | if (*error != '\0') |
| 7081 | return NULL; |
| 7082 | |
| 7083 | /* Perform the unique id lookup. */ |
| 7084 | return tlskeys_ref_lookupid(id); |
| 7085 | } |
| 7086 | |
| 7087 | /* Perform the string lookup. */ |
| 7088 | return tlskeys_ref_lookup(reference); |
| 7089 | } |
| 7090 | #endif |
| 7091 | |
| 7092 | |
| 7093 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7094 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7095 | /* 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] | 7096 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) |
| 7097 | { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7098 | struct show_keys_ctx *ctx = appctx->svcctx; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7099 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7100 | switch (ctx->state) { |
| 7101 | case SHOW_KEYS_INIT: |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7102 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7103 | * quit the function with returning 0. The function is called |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7104 | * later and restart at the state "SHOW_KEYS_INIT". |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7105 | */ |
| 7106 | chunk_reset(&trash); |
| 7107 | |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7108 | if (ctx->dump_entries) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7109 | chunk_appendf(&trash, "# id secret\n"); |
| 7110 | else |
| 7111 | chunk_appendf(&trash, "# id (file)\n"); |
| 7112 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7113 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7114 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7115 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7116 | /* Now, we start the browsing of the references lists. |
| 7117 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 7118 | * available field of this pointer is <list>. It is used with the function |
| 7119 | * tlskeys_list_get_next() for retruning the first available entry |
| 7120 | */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7121 | if (ctx->next_ref == NULL) |
| 7122 | ctx->next_ref = tlskeys_list_get_next(&tlskeys_reference, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7123 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7124 | ctx->state = SHOW_KEYS_LIST; |
Willy Tarreau | a551f4f | 2022-11-14 07:34:43 +0100 | [diff] [blame] | 7125 | __fallthrough; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7126 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7127 | case SHOW_KEYS_LIST: |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7128 | while (ctx->next_ref) { |
| 7129 | struct tls_keys_ref *ref = ctx->next_ref; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7130 | |
| 7131 | chunk_reset(&trash); |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7132 | if (ctx->dump_entries && ctx->next_index == 0) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7133 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7134 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7135 | if (ctx->next_index == 0) |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7136 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 7137 | |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7138 | if (ctx->dump_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7139 | int head; |
| 7140 | |
| 7141 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 7142 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7143 | while (ctx->next_index < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7144 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7145 | |
| 7146 | chunk_reset(t2); |
| 7147 | /* 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] | 7148 | if (ref->key_size_bits == 128) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7149 | 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] | 7150 | sizeof(struct tls_sess_key_128), |
| 7151 | t2->area, t2->size); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7152 | 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] | 7153 | t2->area); |
| 7154 | } |
| 7155 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7156 | 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] | 7157 | sizeof(struct tls_sess_key_256), |
| 7158 | t2->area, t2->size); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7159 | 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] | 7160 | t2->area); |
| 7161 | } |
| 7162 | else { |
| 7163 | /* This case should never happen */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7164 | 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] | 7165 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7166 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7167 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7168 | /* let's try again later from this stream. We add ourselves into |
| 7169 | * this stream's users so that it can remove us upon termination. |
| 7170 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7171 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7172 | return 0; |
| 7173 | } |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7174 | ctx->next_index++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7175 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7176 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7177 | ctx->next_index = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7178 | } |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7179 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7180 | /* let's try again later from this stream. We add ourselves into |
| 7181 | * this stream's users so that it can remove us upon termination. |
| 7182 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7183 | return 0; |
| 7184 | } |
| 7185 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7186 | if (ctx->names_only == 0) /* don't display everything if not necessary */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7187 | break; |
| 7188 | |
| 7189 | /* get next list entry and check the end of the list */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7190 | ctx->next_ref = tlskeys_list_get_next(&ref->list, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7191 | } |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7192 | ctx->state = SHOW_KEYS_DONE; |
Willy Tarreau | a551f4f | 2022-11-14 07:34:43 +0100 | [diff] [blame] | 7193 | __fallthrough; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7194 | |
| 7195 | default: |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7196 | return 1; |
| 7197 | } |
| 7198 | return 0; |
| 7199 | } |
| 7200 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7201 | /* 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] | 7202 | 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] | 7203 | { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7204 | struct show_keys_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 7205 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7206 | /* no parameter, shows only file list */ |
| 7207 | if (!*args[2]) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7208 | ctx->names_only = 1; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7209 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7210 | } |
| 7211 | |
| 7212 | if (args[2][0] == '*') { |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7213 | /* list every TLS ticket keys */ |
| 7214 | ctx->names_only = 1; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7215 | } else { |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7216 | ctx->next_ref = tlskeys_ref_lookup_ref(args[2]); |
| 7217 | if (!ctx->next_ref) |
| 7218 | 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] | 7219 | } |
| 7220 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7221 | ctx->dump_entries = 1; |
| 7222 | return 0; |
| 7223 | } |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7224 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7225 | static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
| 7226 | { |
| 7227 | struct tls_keys_ref *ref; |
| 7228 | int ret; |
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 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 7231 | if (!*args[3] || !*args[4]) |
| 7232 | 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] | 7233 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7234 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 7235 | if (!ref) |
| 7236 | 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] | 7237 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7238 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
| 7239 | if (ret < 0) |
| 7240 | 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] | 7241 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7242 | trash.data = ret; |
| 7243 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 7244 | 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] | 7245 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7246 | 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] | 7247 | } |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7248 | #endif |
| 7249 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7250 | |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7251 | #ifdef HAVE_SSL_PROVIDERS |
| 7252 | struct provider_name { |
| 7253 | const char *name; |
| 7254 | struct list list; |
| 7255 | }; |
| 7256 | |
| 7257 | |
| 7258 | static int ssl_provider_get_name_cb(OSSL_PROVIDER *provider, void *cbdata) |
| 7259 | { |
| 7260 | struct list *provider_names = cbdata; |
| 7261 | struct provider_name *item = NULL; |
| 7262 | const char *name = OSSL_PROVIDER_get0_name(provider); |
| 7263 | |
| 7264 | if (!provider_names) |
| 7265 | return 0; |
| 7266 | |
| 7267 | item = calloc(1, sizeof(*item)); |
| 7268 | |
| 7269 | if (!item) |
| 7270 | return 0; |
| 7271 | |
| 7272 | item->name = name; |
| 7273 | LIST_APPEND(provider_names, &item->list); |
| 7274 | |
| 7275 | return 1; |
| 7276 | } |
| 7277 | |
| 7278 | static void ssl_provider_get_name_list(struct list *provider_names) |
| 7279 | { |
| 7280 | if (!provider_names) |
| 7281 | return; |
| 7282 | |
| 7283 | OSSL_PROVIDER_do_all(NULL, ssl_provider_get_name_cb, provider_names); |
| 7284 | } |
| 7285 | |
| 7286 | static void ssl_provider_clear_name_list(struct list *provider_names) |
| 7287 | { |
| 7288 | struct provider_name *item = NULL, *item_s = NULL; |
| 7289 | |
| 7290 | if (provider_names) { |
| 7291 | list_for_each_entry_safe(item, item_s, provider_names, list) { |
| 7292 | LIST_DELETE(&item->list); |
| 7293 | free(item); |
| 7294 | } |
| 7295 | } |
| 7296 | } |
| 7297 | |
| 7298 | static int cli_io_handler_show_providers(struct appctx *appctx) |
| 7299 | { |
| 7300 | struct buffer *trash = get_trash_chunk(); |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7301 | struct list provider_names; |
| 7302 | struct provider_name *name; |
| 7303 | |
| 7304 | LIST_INIT(&provider_names); |
| 7305 | |
| 7306 | chunk_appendf(trash, "Loaded providers : \n"); |
| 7307 | |
| 7308 | ssl_provider_get_name_list(&provider_names); |
| 7309 | |
| 7310 | list_for_each_entry(name, &provider_names, list) { |
| 7311 | chunk_appendf(trash, "\t- %s\n", name->name); |
| 7312 | } |
| 7313 | |
| 7314 | ssl_provider_clear_name_list(&provider_names); |
| 7315 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7316 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7317 | goto yield; |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7318 | |
| 7319 | return 1; |
| 7320 | |
| 7321 | yield: |
| 7322 | return 0; |
| 7323 | } |
| 7324 | #endif |
Remi Tricot-Le Breton | eeaa29b | 2022-12-20 11:11:07 +0100 | [diff] [blame] | 7325 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7326 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7327 | /* register cli keywords */ |
| 7328 | static struct cli_kw_list cli_kws = {{ },{ |
| 7329 | #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] | 7330 | { { "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 }, |
| 7331 | { { "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] | 7332 | #endif |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7333 | #ifdef HAVE_SSL_PROVIDERS |
| 7334 | { { "show", "ssl", "providers", NULL }, "show ssl providers : show loaded SSL providers", NULL, cli_io_handler_show_providers }, |
| 7335 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7336 | { { NULL }, NULL, NULL, NULL } |
| 7337 | }}; |
| 7338 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7339 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7340 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 7341 | /* transport-layer operations for SSL sockets */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 7342 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7343 | .snd_buf = ssl_sock_from_buf, |
| 7344 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 7345 | .subscribe = ssl_subscribe, |
| 7346 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 7347 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 7348 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7349 | .rcv_pipe = NULL, |
| 7350 | .snd_pipe = NULL, |
| 7351 | .shutr = NULL, |
| 7352 | .shutw = ssl_sock_shutw, |
| 7353 | .close = ssl_sock_close, |
| 7354 | .init = ssl_sock_init, |
Olivier Houchard | bc5ce92 | 2021-03-05 23:47:00 +0100 | [diff] [blame] | 7355 | .start = ssl_sock_start, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 7356 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 7357 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7358 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 7359 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7360 | .get_alpn = ssl_sock_get_alpn, |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 7361 | .takeover = ssl_takeover, |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 7362 | .set_idle = ssl_set_idle, |
| 7363 | .set_used = ssl_set_used, |
Willy Tarreau | de82795 | 2022-04-11 10:43:28 +0200 | [diff] [blame] | 7364 | .get_ssl_sock_ctx = ssl_sock_get_ctx, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 7365 | .name = "SSL", |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7366 | .show_fd = ssl_sock_show_fd, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7367 | }; |
| 7368 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7369 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 7370 | struct session *sess, struct stream *s, int flags) |
| 7371 | { |
| 7372 | struct connection *conn; |
| 7373 | |
| 7374 | conn = objt_conn(sess->origin); |
| 7375 | |
Willy Tarreau | b52d4d2 | 2022-05-27 10:44:39 +0200 | [diff] [blame] | 7376 | if (conn) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7377 | 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] | 7378 | sc_ep_set(s->scf, SE_FL_WAIT_FOR_HS); |
Christopher Faulet | 6e1bbc4 | 2022-12-12 08:08:15 +0100 | [diff] [blame] | 7379 | s->req.flags |= CF_READ_EVENT; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7380 | return ACT_RET_YIELD; |
| 7381 | } |
| 7382 | } |
| 7383 | return (ACT_RET_CONT); |
| 7384 | } |
| 7385 | |
| 7386 | 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) |
| 7387 | { |
| 7388 | rule->action_ptr = ssl_action_wait_for_hs; |
| 7389 | |
| 7390 | return ACT_RET_PRS_OK; |
| 7391 | } |
| 7392 | |
| 7393 | static struct action_kw_list http_req_actions = {ILH, { |
| 7394 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 7395 | { /* END */ } |
| 7396 | }}; |
| 7397 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7398 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 7399 | |
Ilya Shipitsin | f00cdb1 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 7400 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7401 | |
| 7402 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7403 | { |
| 7404 | if (ptr) { |
| 7405 | chunk_destroy(ptr); |
| 7406 | free(ptr); |
| 7407 | } |
| 7408 | } |
| 7409 | |
| 7410 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7411 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7412 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7413 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7414 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7415 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7416 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7417 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 7418 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7419 | static void ssl_sock_keylog_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7420 | { |
| 7421 | struct ssl_keylog *keylog; |
| 7422 | |
| 7423 | if (!ptr) |
| 7424 | return; |
| 7425 | |
| 7426 | keylog = ptr; |
| 7427 | |
| 7428 | pool_free(pool_head_ssl_keylog_str, keylog->client_random); |
| 7429 | pool_free(pool_head_ssl_keylog_str, keylog->client_early_traffic_secret); |
| 7430 | pool_free(pool_head_ssl_keylog_str, keylog->client_handshake_traffic_secret); |
| 7431 | pool_free(pool_head_ssl_keylog_str, keylog->server_handshake_traffic_secret); |
| 7432 | pool_free(pool_head_ssl_keylog_str, keylog->client_traffic_secret_0); |
| 7433 | pool_free(pool_head_ssl_keylog_str, keylog->server_traffic_secret_0); |
| 7434 | pool_free(pool_head_ssl_keylog_str, keylog->exporter_secret); |
| 7435 | pool_free(pool_head_ssl_keylog_str, keylog->early_exporter_secret); |
| 7436 | |
| 7437 | pool_free(pool_head_ssl_keylog, ptr); |
| 7438 | } |
| 7439 | #endif |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7440 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 7441 | static void ssl_sock_clt_crt_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7442 | { |
| 7443 | if (!ptr) |
| 7444 | return; |
| 7445 | |
| 7446 | X509_free((X509*)ptr); |
| 7447 | } |
| 7448 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 7449 | static void ssl_sock_clt_sni_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7450 | { |
| 7451 | pool_free(ssl_sock_client_sni_pool, ptr); |
| 7452 | } |
| 7453 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7454 | static void __ssl_sock_init(void) |
| 7455 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7456 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7457 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7458 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7459 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7460 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7461 | if (global_ssl.listen_default_ciphers) |
| 7462 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 7463 | if (global_ssl.connect_default_ciphers) |
| 7464 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 7465 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7466 | if (global_ssl.listen_default_ciphersuites) |
| 7467 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 7468 | if (global_ssl.connect_default_ciphersuites) |
| 7469 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 7470 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 7471 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 7472 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7473 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7474 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7475 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7476 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7477 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7478 | n = sk_SSL_COMP_num(cm); |
| 7479 | while (n--) { |
| 7480 | (void) sk_SSL_COMP_pop(cm); |
| 7481 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7482 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7483 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7484 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7485 | ssl_locking_init(); |
| 7486 | #endif |
Ilya Shipitsin | f00cdb1 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 7487 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7488 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 7489 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7490 | |
| 7491 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 7492 | ocsp_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_ocsp_free_func); |
| 7493 | #endif |
| 7494 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 7495 | 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] | 7496 | 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] | 7497 | #ifdef USE_QUIC |
| 7498 | ssl_qc_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7499 | #endif /* USE_QUIC */ |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 7500 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7501 | ssl_keylog_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_keylog_free_func); |
| 7502 | #endif |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 7503 | 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] | 7504 | 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] | 7505 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7506 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7507 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7508 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 7509 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7510 | hap_register_post_check(tlskeys_finalize_config); |
| 7511 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7512 | |
| 7513 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 7514 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 7515 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 7516 | hap_register_post_deinit(ssl_free_global_issuers); |
| 7517 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7518 | #ifndef OPENSSL_NO_DH |
| 7519 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7520 | hap_register_post_deinit(ssl_free_dh); |
| 7521 | #endif |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 7522 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7523 | hap_register_post_deinit(ssl_free_engines); |
| 7524 | #endif |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 7525 | #ifdef HAVE_SSL_PROVIDERS |
| 7526 | hap_register_post_deinit(ssl_unload_providers); |
| 7527 | #endif |
Remi Tricot-Le Breton | 78a36e3 | 2022-02-11 12:04:45 +0100 | [diff] [blame] | 7528 | #if HA_OPENSSL_VERSION_NUMBER < 0x3000000fL |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7529 | /* Load SSL string for the verbose & debug mode. */ |
| 7530 | ERR_load_SSL_strings(); |
Remi Tricot-Le Breton | 78a36e3 | 2022-02-11 12:04:45 +0100 | [diff] [blame] | 7531 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 7532 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 7533 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 7534 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 7535 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 7536 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 7537 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 7538 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 7539 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 7540 | |
| 7541 | HA_SPIN_INIT(&ckch_lock); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 7542 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7543 | HA_SPIN_INIT(&ocsp_tree_lock); |
| 7544 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 7545 | /* Try to register dedicated SSL/TLS protocol message callbacks for |
| 7546 | * heartbleed attack (CVE-2014-0160) and clienthello. |
| 7547 | */ |
| 7548 | hap_register_post_check(ssl_sock_register_msg_callbacks); |
| 7549 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 7550 | /* Try to free all callbacks that were registered by using |
| 7551 | * ssl_sock_register_msg_callback(). |
| 7552 | */ |
| 7553 | hap_register_post_deinit(ssl_sock_unregister_msg_callbacks); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7554 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7555 | INITCALL0(STG_REGISTER, __ssl_sock_init); |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7556 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7557 | /* Compute and register the version string */ |
| 7558 | static void ssl_register_build_options() |
| 7559 | { |
| 7560 | char *ptr = NULL; |
| 7561 | int i; |
| 7562 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7563 | memprintf(&ptr, "Built with OpenSSL version : " |
| 7564 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 7565 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7566 | #else /* OPENSSL_IS_BORINGSSL */ |
| 7567 | OPENSSL_VERSION_TEXT |
| 7568 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7569 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 7570 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7571 | #endif |
| 7572 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7573 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7574 | "no (library version too old)" |
| 7575 | #elif defined(OPENSSL_NO_TLSEXT) |
| 7576 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 7577 | #else |
| 7578 | "yes" |
| 7579 | #endif |
| 7580 | "", ptr); |
| 7581 | |
| 7582 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 7583 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7584 | "yes" |
| 7585 | #else |
| 7586 | #ifdef OPENSSL_NO_TLSEXT |
| 7587 | "no (because of OPENSSL_NO_TLSEXT)" |
| 7588 | #else |
| 7589 | "no (version might be too old, 0.9.8f min needed)" |
| 7590 | #endif |
| 7591 | #endif |
| 7592 | "", ptr); |
| 7593 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 7594 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 7595 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7596 | if (methodVersions[i].option) |
| 7597 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 7598 | |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7599 | #ifdef HAVE_SSL_PROVIDERS |
| 7600 | { |
| 7601 | struct list provider_names; |
| 7602 | struct provider_name *name; |
| 7603 | LIST_INIT(&provider_names); |
| 7604 | ssl_provider_get_name_list(&provider_names); |
| 7605 | |
| 7606 | memprintf(&ptr, "%s\nOpenSSL providers loaded :", ptr); |
| 7607 | |
| 7608 | list_for_each_entry(name, &provider_names, list) { |
| 7609 | memprintf(&ptr, "%s %s", ptr, name->name); |
| 7610 | } |
| 7611 | |
| 7612 | ssl_provider_clear_name_list(&provider_names); |
| 7613 | } |
| 7614 | #endif |
| 7615 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7616 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7617 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7618 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7619 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 7620 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 7621 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7622 | void ssl_free_engines(void) { |
| 7623 | struct ssl_engine_list *wl, *wlb; |
| 7624 | /* free up engine list */ |
| 7625 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 7626 | ENGINE_finish(wl->e); |
| 7627 | ENGINE_free(wl->e); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 7628 | LIST_DELETE(&wl->list); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7629 | free(wl); |
| 7630 | } |
| 7631 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7632 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7633 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 7634 | #ifdef HAVE_SSL_PROVIDERS |
| 7635 | void ssl_unload_providers(void) { |
| 7636 | struct ssl_provider_list *prov, *provb; |
| 7637 | list_for_each_entry_safe(prov, provb, &openssl_providers, list) { |
| 7638 | OSSL_PROVIDER_unload(prov->provider); |
| 7639 | LIST_DELETE(&prov->list); |
| 7640 | free(prov); |
| 7641 | } |
| 7642 | } |
| 7643 | #endif |
| 7644 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7645 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7646 | void ssl_free_dh(void) { |
| 7647 | if (local_dh_1024) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7648 | HASSL_DH_free(local_dh_1024); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7649 | local_dh_1024 = NULL; |
| 7650 | } |
| 7651 | if (local_dh_2048) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7652 | HASSL_DH_free(local_dh_2048); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7653 | local_dh_2048 = NULL; |
| 7654 | } |
| 7655 | if (local_dh_4096) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7656 | HASSL_DH_free(local_dh_4096); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7657 | local_dh_4096 = NULL; |
| 7658 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7659 | if (global_dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7660 | HASSL_DH_free(global_dh); |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7661 | global_dh = NULL; |
| 7662 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7663 | } |
| 7664 | #endif |
| 7665 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7666 | static void __ssl_sock_deinit(void) |
| 7667 | { |
| 7668 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7669 | if (ssl_ctx_lru_tree) { |
| 7670 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7671 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7672 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7673 | #endif |
| 7674 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7675 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7676 | ERR_remove_state(0); |
| 7677 | ERR_free_strings(); |
| 7678 | |
| 7679 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7680 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7681 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7682 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7683 | CRYPTO_cleanup_all_ex_data(); |
| 7684 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 7685 | BIO_meth_free(ha_meth); |
Remi Tricot-Le Breton | 6477bbd | 2022-12-20 11:11:13 +0100 | [diff] [blame] | 7686 | |
| 7687 | #if !defined OPENSSL_NO_OCSP |
| 7688 | ssl_destroy_ocsp_update_task(); |
| 7689 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7690 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7691 | REGISTER_POST_DEINIT(__ssl_sock_deinit); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7692 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 7693 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7694 | /* |
| 7695 | * Local variables: |
| 7696 | * c-indent-level: 8 |
| 7697 | * c-basic-offset: 8 |
| 7698 | * End: |
| 7699 | */ |