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 |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 135 | .keylog = 0 |
| 136 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 137 | }; |
| 138 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 139 | static BIO_METHOD *ha_meth; |
| 140 | |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 141 | 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] | 142 | |
Willy Tarreau | b8dec4a | 2022-06-23 11:02:08 +0200 | [diff] [blame] | 143 | 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] | 144 | |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 145 | /* ssl stats module */ |
| 146 | enum { |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 147 | SSL_ST_SESS, |
| 148 | SSL_ST_REUSED_SESS, |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 149 | SSL_ST_FAILED_HANDSHAKE, |
Amaury Denoyelle | fbc3377 | 2020-11-03 17:10:01 +0100 | [diff] [blame] | 150 | |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 151 | SSL_ST_STATS_COUNT /* must be the last member of the enum */ |
| 152 | }; |
| 153 | |
| 154 | static struct name_desc ssl_stats[] = { |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 155 | [SSL_ST_SESS] = { .name = "ssl_sess", |
| 156 | .desc = "Total number of ssl sessions established" }, |
| 157 | [SSL_ST_REUSED_SESS] = { .name = "ssl_reused_sess", |
| 158 | .desc = "Total number of ssl sessions reused" }, |
| 159 | [SSL_ST_FAILED_HANDSHAKE] = { .name = "ssl_failed_handshake", |
| 160 | .desc = "Total number of failed handshake" }, |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 161 | }; |
| 162 | |
| 163 | static struct ssl_counters { |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 164 | long long sess; |
| 165 | long long reused_sess; |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 166 | long long failed_handshake; |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 167 | } ssl_counters; |
| 168 | |
| 169 | static void ssl_fill_stats(void *data, struct field *stats) |
| 170 | { |
Amaury Denoyelle | fbc3377 | 2020-11-03 17:10:01 +0100 | [diff] [blame] | 171 | struct ssl_counters *counters = data; |
| 172 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 173 | stats[SSL_ST_SESS] = mkf_u64(FN_COUNTER, counters->sess); |
| 174 | stats[SSL_ST_REUSED_SESS] = mkf_u64(FN_COUNTER, counters->reused_sess); |
| 175 | stats[SSL_ST_FAILED_HANDSHAKE] = mkf_u64(FN_COUNTER, counters->failed_handshake); |
Amaury Denoyelle | 9963fa7 | 2020-11-03 17:10:00 +0100 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | static struct stats_module ssl_stats_module = { |
| 179 | .name = "ssl", |
| 180 | .fill_stats = ssl_fill_stats, |
| 181 | .stats = ssl_stats, |
| 182 | .stats_count = SSL_ST_STATS_COUNT, |
| 183 | .counters = &ssl_counters, |
| 184 | .counters_size = sizeof(ssl_counters), |
| 185 | .domain_flags = MK_STATS_PROXY_DOMAIN(STATS_PX_CAP_FE|STATS_PX_CAP_LI|STATS_PX_CAP_BE|STATS_PX_CAP_SRV), |
| 186 | .clearable = 1, |
| 187 | }; |
| 188 | |
| 189 | INITCALL1(STG_REGISTER, stats_register_module, &ssl_stats_module); |
| 190 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 191 | /* CLI context for "show tls-keys" */ |
| 192 | struct show_keys_ctx { |
| 193 | struct tls_keys_ref *next_ref; /* next reference to be dumped */ |
| 194 | int names_only; /* non-zero = only show file names */ |
| 195 | int next_index; /* next index to be dumped */ |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 196 | int dump_entries; /* dump entries also */ |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 197 | enum { |
| 198 | SHOW_KEYS_INIT = 0, |
| 199 | SHOW_KEYS_LIST, |
| 200 | SHOW_KEYS_DONE, |
| 201 | } state; /* phase of the current dump */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 202 | }; |
| 203 | |
Willy Tarreau | 691d503 | 2021-01-20 14:55:01 +0100 | [diff] [blame] | 204 | /* 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] | 205 | struct task *ssl_sock_io_cb(struct task *, void *, unsigned int); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 206 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 207 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 208 | /* Methods to implement OpenSSL BIO */ |
| 209 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 210 | { |
| 211 | struct buffer tmpbuf; |
| 212 | struct ssl_sock_ctx *ctx; |
| 213 | int ret; |
| 214 | |
| 215 | ctx = BIO_get_data(h); |
| 216 | tmpbuf.size = num; |
| 217 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 218 | tmpbuf.data = num; |
| 219 | tmpbuf.head = 0; |
| 220 | ret = ctx->xprt->snd_buf(ctx->conn, ctx->xprt_ctx, &tmpbuf, num, 0); |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 221 | 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] | 222 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 223 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 224 | } else if (ret == 0) |
| 225 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 226 | return ret; |
| 227 | } |
| 228 | |
| 229 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 230 | { |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int ha_ssl_puts(BIO *h, const char *str) |
| 236 | { |
| 237 | |
| 238 | return ha_ssl_write(h, str, strlen(str)); |
| 239 | } |
| 240 | |
| 241 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 242 | { |
| 243 | struct buffer tmpbuf; |
| 244 | struct ssl_sock_ctx *ctx; |
| 245 | int ret; |
| 246 | |
| 247 | ctx = BIO_get_data(h); |
| 248 | tmpbuf.size = size; |
| 249 | tmpbuf.area = buf; |
| 250 | tmpbuf.data = 0; |
| 251 | tmpbuf.head = 0; |
| 252 | 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] | 253 | 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] | 254 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 255 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 256 | } else if (ret == 0) |
| 257 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 258 | |
| 259 | return ret; |
| 260 | } |
| 261 | |
| 262 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 263 | { |
| 264 | int ret = 0; |
| 265 | switch (cmd) { |
| 266 | case BIO_CTRL_DUP: |
| 267 | case BIO_CTRL_FLUSH: |
| 268 | ret = 1; |
| 269 | break; |
| 270 | } |
| 271 | return ret; |
| 272 | } |
| 273 | |
| 274 | static int ha_ssl_new(BIO *h) |
| 275 | { |
| 276 | BIO_set_init(h, 1); |
| 277 | BIO_set_data(h, NULL); |
| 278 | BIO_clear_flags(h, ~0); |
| 279 | return 1; |
| 280 | } |
| 281 | |
| 282 | static int ha_ssl_free(BIO *data) |
| 283 | { |
| 284 | |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 289 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 290 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 291 | static HA_RWLOCK_T *ssl_rwlocks; |
| 292 | |
| 293 | |
| 294 | unsigned long ssl_id_function(void) |
| 295 | { |
| 296 | return (unsigned long)tid; |
| 297 | } |
| 298 | |
| 299 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 300 | { |
| 301 | if (mode & CRYPTO_LOCK) { |
| 302 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 303 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 304 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 305 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 306 | } |
| 307 | else { |
| 308 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 309 | HA_RWLOCK_RDUNLOCK(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_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | static int ssl_locking_init(void) |
| 316 | { |
| 317 | int i; |
| 318 | |
| 319 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 320 | if (!ssl_rwlocks) |
| 321 | return -1; |
| 322 | |
| 323 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 324 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 325 | |
| 326 | CRYPTO_set_id_callback(ssl_id_function); |
| 327 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 328 | |
| 329 | return 0; |
| 330 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 331 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 332 | #endif |
| 333 | |
Willy Tarreau | af613e8 | 2020-06-05 08:40:51 +0200 | [diff] [blame] | 334 | __decl_thread(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 335 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 336 | |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 337 | |
| 338 | /* mimic what X509_STORE_load_locations do with store_ctx */ |
| 339 | static int ssl_set_cert_crl_file(X509_STORE *store_ctx, char *path) |
| 340 | { |
Remi Tricot-Le Breton | d75b99e | 2021-05-17 11:45:55 +0200 | [diff] [blame] | 341 | X509_STORE *store = NULL; |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 342 | struct cafile_entry *ca_e = ssl_store_get_cafile_entry(path, 0); |
| 343 | if (ca_e) |
| 344 | store = ca_e->ca_store; |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 345 | if (store_ctx && store) { |
| 346 | int i; |
| 347 | X509_OBJECT *obj; |
| 348 | STACK_OF(X509_OBJECT) *objs = X509_STORE_get0_objects(store); |
| 349 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 350 | obj = sk_X509_OBJECT_value(objs, i); |
| 351 | switch (X509_OBJECT_get_type(obj)) { |
| 352 | case X509_LU_X509: |
| 353 | X509_STORE_add_cert(store_ctx, X509_OBJECT_get0_X509(obj)); |
| 354 | break; |
| 355 | case X509_LU_CRL: |
| 356 | X509_STORE_add_crl(store_ctx, X509_OBJECT_get0_X509_CRL(obj)); |
| 357 | break; |
| 358 | default: |
| 359 | break; |
| 360 | } |
| 361 | } |
| 362 | return 1; |
| 363 | } |
| 364 | return 0; |
| 365 | } |
| 366 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 367 | /* SSL_CTX_load_verify_locations substitute, internally call X509_STORE_load_locations */ |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 368 | static int ssl_set_verify_locations_file(SSL_CTX *ctx, char *path) |
| 369 | { |
| 370 | X509_STORE *store_ctx = SSL_CTX_get_cert_store(ctx); |
| 371 | return ssl_set_cert_crl_file(store_ctx, path); |
| 372 | } |
| 373 | |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 374 | /* |
| 375 | Extract CA_list from CA_file already in tree. |
| 376 | Duplicate ca_name is tracking with ebtree. It's simplify openssl compatibility. |
| 377 | Return a shared ca_list: SSL_dup_CA_list must be used before set it on SSL_CTX. |
| 378 | */ |
| 379 | static STACK_OF(X509_NAME)* ssl_get_client_ca_file(char *path) |
| 380 | { |
| 381 | struct ebmb_node *eb; |
| 382 | struct cafile_entry *ca_e; |
| 383 | |
| 384 | eb = ebst_lookup(&cafile_tree, path); |
| 385 | if (!eb) |
| 386 | return NULL; |
| 387 | ca_e = ebmb_entry(eb, struct cafile_entry, node); |
| 388 | |
| 389 | if (ca_e->ca_list == NULL) { |
| 390 | int i; |
| 391 | unsigned long key; |
| 392 | struct eb_root ca_name_tree = EB_ROOT; |
| 393 | struct eb64_node *node, *back; |
| 394 | struct { |
| 395 | struct eb64_node node; |
| 396 | X509_NAME *xname; |
| 397 | } *ca_name; |
| 398 | STACK_OF(X509_OBJECT) *objs; |
| 399 | STACK_OF(X509_NAME) *skn; |
| 400 | X509 *x; |
| 401 | X509_NAME *xn; |
| 402 | |
| 403 | skn = sk_X509_NAME_new_null(); |
| 404 | /* take x509 from cafile_tree */ |
| 405 | objs = X509_STORE_get0_objects(ca_e->ca_store); |
| 406 | for (i = 0; i < sk_X509_OBJECT_num(objs); i++) { |
| 407 | x = X509_OBJECT_get0_X509(sk_X509_OBJECT_value(objs, i)); |
| 408 | if (!x) |
| 409 | continue; |
| 410 | xn = X509_get_subject_name(x); |
| 411 | if (!xn) |
| 412 | continue; |
| 413 | /* Check for duplicates. */ |
| 414 | key = X509_NAME_hash(xn); |
| 415 | for (node = eb64_lookup(&ca_name_tree, key), ca_name = NULL; |
| 416 | node && ca_name == NULL; |
| 417 | node = eb64_next(node)) { |
| 418 | ca_name = container_of(node, typeof(*ca_name), node); |
| 419 | if (X509_NAME_cmp(xn, ca_name->xname) != 0) |
| 420 | ca_name = NULL; |
| 421 | } |
| 422 | /* find a duplicate */ |
| 423 | if (ca_name) |
| 424 | continue; |
| 425 | ca_name = calloc(1, sizeof *ca_name); |
| 426 | xn = X509_NAME_dup(xn); |
| 427 | if (!ca_name || |
| 428 | !xn || |
| 429 | !sk_X509_NAME_push(skn, xn)) { |
| 430 | free(ca_name); |
| 431 | X509_NAME_free(xn); |
| 432 | sk_X509_NAME_pop_free(skn, X509_NAME_free); |
| 433 | sk_X509_NAME_free(skn); |
| 434 | skn = NULL; |
| 435 | break; |
| 436 | } |
| 437 | ca_name->node.key = key; |
| 438 | ca_name->xname = xn; |
| 439 | eb64_insert(&ca_name_tree, &ca_name->node); |
| 440 | } |
| 441 | ca_e->ca_list = skn; |
| 442 | /* remove temporary ca_name tree */ |
| 443 | node = eb64_first(&ca_name_tree); |
| 444 | while (node) { |
| 445 | ca_name = container_of(node, typeof(*ca_name), node); |
| 446 | back = eb64_next(node); |
| 447 | eb64_delete(node); |
| 448 | free(ca_name); |
| 449 | node = back; |
| 450 | } |
| 451 | } |
| 452 | return ca_e->ca_list; |
| 453 | } |
| 454 | |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 455 | struct pool_head *pool_head_ssl_capture __read_mostly = NULL; |
William Lallemand | 15e1694 | 2020-05-15 00:25:08 +0200 | [diff] [blame] | 456 | int ssl_capture_ptr_index = -1; |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 457 | int ssl_app_data_index = -1; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 458 | #ifdef USE_QUIC |
| 459 | int ssl_qc_app_data_index = -1; |
| 460 | #endif /* USE_QUIC */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 461 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 462 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 463 | int ssl_keylog_index = -1; |
Willy Tarreau | ff88270 | 2021-04-10 17:23:00 +0200 | [diff] [blame] | 464 | struct pool_head *pool_head_ssl_keylog __read_mostly = NULL; |
| 465 | struct pool_head *pool_head_ssl_keylog_str __read_mostly = NULL; |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 466 | #endif |
| 467 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 468 | int ssl_client_crt_ref_index = -1; |
| 469 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 470 | /* Used to store the client's SNI in case of ClientHello callback error */ |
| 471 | int ssl_client_sni_index = -1; |
| 472 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 473 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 474 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 475 | #endif |
| 476 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 477 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 478 | unsigned int openssl_engines_initialized; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 479 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 480 | struct ssl_engine_list { |
| 481 | struct list list; |
| 482 | ENGINE *e; |
| 483 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 484 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 485 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 486 | #ifdef HAVE_SSL_PROVIDERS |
| 487 | struct list openssl_providers = LIST_HEAD_INIT(openssl_providers); |
| 488 | struct ssl_provider_list { |
| 489 | struct list list; |
| 490 | OSSL_PROVIDER *provider; |
| 491 | }; |
| 492 | #endif |
| 493 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 494 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 495 | static int ssl_dh_ptr_index = -1; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 496 | static HASSL_DH *global_dh = NULL; |
| 497 | static HASSL_DH *local_dh_1024 = NULL; |
| 498 | static HASSL_DH *local_dh_2048 = NULL; |
| 499 | static HASSL_DH *local_dh_4096 = NULL; |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 500 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 501 | 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] | 502 | #else |
| 503 | static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey); |
| 504 | #endif |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 505 | #endif /* OPENSSL_NO_DH */ |
| 506 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 507 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 508 | /* X509V3 Extensions that will be added on generated certificates */ |
| 509 | #define X509V3_EXT_SIZE 5 |
| 510 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 511 | "basicConstraints", |
| 512 | "nsComment", |
| 513 | "subjectKeyIdentifier", |
| 514 | "authorityKeyIdentifier", |
| 515 | "keyUsage", |
| 516 | }; |
| 517 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 518 | "CA:FALSE", |
| 519 | "\"OpenSSL Generated Certificate\"", |
| 520 | "hash", |
| 521 | "keyid,issuer:always", |
| 522 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 523 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 524 | /* LRU cache to store generated certificate */ |
| 525 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 526 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 527 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 528 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 529 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 530 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 531 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 532 | /* The order here matters for picking a default context, |
| 533 | * keep the most common keytype at the bottom of the list |
| 534 | */ |
| 535 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 536 | "dsa", |
| 537 | "ecdsa", |
| 538 | "rsa" |
| 539 | }; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 540 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 541 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 542 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 543 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 544 | /* Dedicated callback functions for heartbeat and clienthello. |
| 545 | */ |
| 546 | #ifdef TLS1_RT_HEARTBEAT |
| 547 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 548 | int content_type, const void *buf, size_t len, |
| 549 | SSL *ssl); |
| 550 | #endif |
| 551 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 552 | int content_type, const void *buf, size_t len, |
| 553 | SSL *ssl); |
| 554 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 555 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 556 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 557 | int content_type, const void *buf, size_t len, |
| 558 | SSL *ssl); |
| 559 | #endif |
| 560 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 561 | /* List head of all registered SSL/TLS protocol message callbacks. */ |
| 562 | struct list ssl_sock_msg_callbacks = LIST_HEAD_INIT(ssl_sock_msg_callbacks); |
| 563 | |
| 564 | /* Registers the function <func> in order to be called on SSL/TLS protocol |
| 565 | * message processing. It will return 0 if the function <func> is not set |
| 566 | * or if it fails to allocate memory. |
| 567 | */ |
| 568 | int ssl_sock_register_msg_callback(ssl_sock_msg_callback_func func) |
| 569 | { |
| 570 | struct ssl_sock_msg_callback *cbk; |
| 571 | |
| 572 | if (!func) |
| 573 | return 0; |
| 574 | |
| 575 | cbk = calloc(1, sizeof(*cbk)); |
| 576 | if (!cbk) { |
| 577 | ha_alert("out of memory in ssl_sock_register_msg_callback().\n"); |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | cbk->func = func; |
| 582 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 583 | LIST_APPEND(&ssl_sock_msg_callbacks, &cbk->list); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 584 | |
| 585 | return 1; |
| 586 | } |
| 587 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 588 | /* Used to register dedicated SSL/TLS protocol message callbacks. |
| 589 | */ |
| 590 | static int ssl_sock_register_msg_callbacks(void) |
| 591 | { |
| 592 | #ifdef TLS1_RT_HEARTBEAT |
| 593 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_heartbeat)) |
| 594 | return ERR_ABORT; |
| 595 | #endif |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 596 | if (global_ssl.capture_buffer_size > 0) { |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 597 | if (!ssl_sock_register_msg_callback(ssl_sock_parse_clienthello)) |
| 598 | return ERR_ABORT; |
| 599 | } |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 600 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 601 | if (global_ssl.keylog > 0) { |
| 602 | if (!ssl_sock_register_msg_callback(ssl_init_keylog)) |
| 603 | return ERR_ABORT; |
| 604 | } |
| 605 | #endif |
| 606 | |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 607 | return ERR_NONE; |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 608 | } |
| 609 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 610 | /* Used to free all SSL/TLS protocol message callbacks that were |
| 611 | * registered by using ssl_sock_register_msg_callback(). |
| 612 | */ |
| 613 | static void ssl_sock_unregister_msg_callbacks(void) |
| 614 | { |
| 615 | struct ssl_sock_msg_callback *cbk, *cbkback; |
| 616 | |
| 617 | list_for_each_entry_safe(cbk, cbkback, &ssl_sock_msg_callbacks, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 618 | LIST_DELETE(&cbk->list); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 619 | free(cbk); |
| 620 | } |
| 621 | } |
| 622 | |
Willy Tarreau | de82795 | 2022-04-11 10:43:28 +0200 | [diff] [blame] | 623 | static struct ssl_sock_ctx *ssl_sock_get_ctx(struct connection *conn) |
| 624 | { |
| 625 | if (!conn || conn->xprt != xprt_get(XPRT_SSL) || !conn->xprt_ctx) |
| 626 | return NULL; |
| 627 | |
| 628 | return (struct ssl_sock_ctx *)conn->xprt_ctx; |
| 629 | } |
| 630 | |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 631 | SSL *ssl_sock_get_ssl_object(struct connection *conn) |
| 632 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 633 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 634 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 635 | return ctx ? ctx->ssl : NULL; |
Dragan Dosen | eb607fe | 2020-05-11 17:17:06 +0200 | [diff] [blame] | 636 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 637 | /* |
| 638 | * This function gives the detail of the SSL error. It is used only |
| 639 | * if the debug mode and the verbose mode are activated. It dump all |
| 640 | * the SSL error until the stack was empty. |
| 641 | */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 642 | static forceinline void ssl_sock_dump_errors(struct connection *conn, |
| 643 | struct quic_conn *qc) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 644 | { |
| 645 | unsigned long ret; |
| 646 | |
| 647 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 648 | while(1) { |
Remi Tricot-Le Breton | 1effd9a | 2022-02-11 12:04:44 +0100 | [diff] [blame] | 649 | const char *func = NULL; |
| 650 | ERR_peek_error_func(&func); |
| 651 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 652 | ret = ERR_get_error(); |
| 653 | if (ret == 0) |
| 654 | return; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 655 | if (conn) { |
| 656 | fprintf(stderr, "fd[%#x] OpenSSL error[0x%lx] %s: %s\n", |
| 657 | conn_fd(conn), ret, |
| 658 | func, ERR_reason_error_string(ret)); |
| 659 | } |
| 660 | #ifdef USE_QUIC |
| 661 | else { |
| 662 | /* TODO: we are not sure <conn> is always initialized for QUIC connections */ |
| 663 | fprintf(stderr, "qc @%p OpenSSL error[0x%lx] %s: %s\n", qc, ret, |
| 664 | func, ERR_reason_error_string(ret)); |
| 665 | } |
| 666 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 667 | } |
| 668 | } |
| 669 | } |
| 670 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 671 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 672 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 673 | 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] | 674 | { |
| 675 | int err_code = ERR_ABORT; |
| 676 | ENGINE *engine; |
| 677 | struct ssl_engine_list *el; |
| 678 | |
| 679 | /* grab the structural reference to the engine */ |
| 680 | engine = ENGINE_by_id(engine_id); |
| 681 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 682 | 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] | 683 | goto fail_get; |
| 684 | } |
| 685 | |
| 686 | if (!ENGINE_init(engine)) { |
| 687 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 688 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 689 | goto fail_init; |
| 690 | } |
| 691 | |
| 692 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 693 | 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] | 694 | goto fail_set_method; |
| 695 | } |
| 696 | |
| 697 | el = calloc(1, sizeof(*el)); |
Remi Tricot-Le Breton | 612b2c3 | 2021-05-12 17:45:21 +0200 | [diff] [blame] | 698 | if (!el) |
| 699 | goto fail_alloc; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 700 | el->e = engine; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 701 | LIST_INSERT(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 702 | nb_engines++; |
| 703 | if (global_ssl.async) |
| 704 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 705 | return 0; |
| 706 | |
Remi Tricot-Le Breton | 612b2c3 | 2021-05-12 17:45:21 +0200 | [diff] [blame] | 707 | fail_alloc: |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 708 | fail_set_method: |
| 709 | /* release the functional reference from ENGINE_init() */ |
| 710 | ENGINE_finish(engine); |
| 711 | |
| 712 | fail_init: |
| 713 | /* release the structural reference from ENGINE_by_id() */ |
| 714 | ENGINE_free(engine); |
| 715 | |
| 716 | fail_get: |
| 717 | return err_code; |
| 718 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 719 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 720 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 721 | #ifdef HAVE_SSL_PROVIDERS |
| 722 | int ssl_init_provider(const char *provider_name) |
| 723 | { |
| 724 | int err_code = ERR_ABORT; |
| 725 | struct ssl_provider_list *prov = NULL; |
| 726 | |
| 727 | prov = calloc(1, sizeof(*prov)); |
| 728 | if (!prov) { |
| 729 | ha_alert("ssl-provider %s: memory allocation failure\n", provider_name); |
| 730 | goto error; |
| 731 | } |
| 732 | |
| 733 | if ((prov->provider = OSSL_PROVIDER_load(NULL, provider_name)) == NULL) { |
| 734 | ha_alert("ssl-provider %s: unknown provider\n", provider_name); |
| 735 | goto error; |
| 736 | } |
| 737 | |
| 738 | LIST_INSERT(&openssl_providers, &prov->list); |
| 739 | |
| 740 | return 0; |
| 741 | |
| 742 | error: |
| 743 | ha_free(&prov); |
| 744 | return err_code; |
| 745 | } |
| 746 | #endif /* HAVE_SSL_PROVIDERS */ |
| 747 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 748 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 749 | /* |
| 750 | * openssl async fd handler |
| 751 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 752 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 753 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 754 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 755 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 756 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 757 | * to poll this fd until it is requested |
| 758 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 759 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 760 | fd_cant_recv(fd); |
| 761 | |
| 762 | /* crypto engine is available, let's notify the associated |
| 763 | * connection that it can pursue its processing. |
| 764 | */ |
Olivier Houchard | a459826 | 2020-09-15 22:16:02 +0200 | [diff] [blame] | 765 | tasklet_wakeup(ctx->wait_event.tasklet); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 768 | /* |
| 769 | * openssl async delayed SSL_free handler |
| 770 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 771 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 772 | { |
| 773 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 774 | OSSL_ASYNC_FD all_fd[32]; |
| 775 | size_t num_all_fds = 0; |
| 776 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 777 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 778 | /* We suppose that the async job for a same SSL * |
| 779 | * are serialized. So if we are awake it is |
| 780 | * because the running job has just finished |
| 781 | * and we can remove all async fds safely |
| 782 | */ |
| 783 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 784 | if (num_all_fds > 32) { |
| 785 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 786 | return; |
| 787 | } |
| 788 | |
| 789 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 790 | for (i=0 ; i < num_all_fds ; i++) { |
| 791 | /* We want to remove the fd from the fdtab |
| 792 | * but we flag it to disown because the |
| 793 | * close is performed by the engine itself |
| 794 | */ |
| 795 | fdtab[all_fd[i]].state |= FD_DISOWN; |
| 796 | fd_delete(all_fd[i]); |
| 797 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 798 | |
| 799 | /* 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] | 800 | SSL_free(ssl); |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 801 | _HA_ATOMIC_DEC(&global.sslconns); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 802 | _HA_ATOMIC_DEC(&jobs); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 803 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 804 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 805 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 806 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 807 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 808 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 809 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 810 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 811 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 812 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 813 | size_t num_add_fds = 0; |
| 814 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 815 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 816 | |
| 817 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 818 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 819 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 820 | 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] | 821 | return; |
| 822 | } |
| 823 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 824 | 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] | 825 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 826 | /* We remove unused fds from the fdtab */ |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 827 | for (i=0 ; i < num_del_fds ; i++) { |
| 828 | /* We want to remove the fd from the fdtab |
| 829 | * but we flag it to disown because the |
| 830 | * close is performed by the engine itself |
| 831 | */ |
| 832 | fdtab[del_fd[i]].state |= FD_DISOWN; |
| 833 | fd_delete(del_fd[i]); |
| 834 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 835 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 836 | /* We add new fds to the fdtab */ |
| 837 | for (i=0 ; i < num_add_fds ; i++) { |
Willy Tarreau | 27a3245 | 2022-07-07 08:29:00 +0200 | [diff] [blame] | 838 | 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] | 839 | } |
| 840 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 841 | num_add_fds = 0; |
| 842 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 843 | if (num_add_fds > 32) { |
| 844 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 845 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 846 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 847 | |
| 848 | /* We activate the polling for all known async fds */ |
| 849 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 850 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 851 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 852 | /* To ensure that the fd cache won't be used |
| 853 | * We'll prefer to catch a real RD event |
| 854 | * because handling an EAGAIN on this fd will |
| 855 | * result in a context switch and also |
| 856 | * some engines uses a fd in blocking mode. |
| 857 | */ |
| 858 | fd_cant_recv(add_fd[i]); |
| 859 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 860 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 861 | } |
| 862 | #endif |
| 863 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 864 | |
| 865 | /* |
| 866 | * Initialize an HMAC context <hctx> using the <key> and <md> parameters. |
| 867 | * Returns -1 in case of error, 1 otherwise. |
| 868 | */ |
| 869 | static int ssl_hmac_init(MAC_CTX *hctx, unsigned char *key, int key_len, const EVP_MD *md) |
| 870 | { |
| 871 | #ifdef HAVE_OSSL_PARAM |
| 872 | OSSL_PARAM params[3]; |
| 873 | |
| 874 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, key, key_len); |
| 875 | params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, (char*)EVP_MD_name(md), 0); |
| 876 | params[2] = OSSL_PARAM_construct_end(); |
| 877 | if (EVP_MAC_CTX_set_params(hctx, params) == 0) |
| 878 | return -1; /* error in mac initialisation */ |
| 879 | |
| 880 | #else |
| 881 | HMAC_Init_ex(hctx, key, key_len, md, NULL); |
| 882 | #endif |
| 883 | return 1; |
| 884 | } |
| 885 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 886 | #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] | 887 | |
| 888 | 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] | 889 | { |
Frédéric Lécaille | 6aec1f3 | 2022-09-06 17:04:55 +0200 | [diff] [blame] | 890 | struct tls_keys_ref *ref = NULL; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 891 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 892 | int head; |
| 893 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 894 | int ret = -1; /* error by default */ |
Frédéric Lécaille | 6aec1f3 | 2022-09-06 17:04:55 +0200 | [diff] [blame] | 895 | struct connection *conn = SSL_get_ex_data(s, ssl_app_data_index); |
| 896 | #ifdef USE_QUIC |
| 897 | struct quic_conn *qc = SSL_get_ex_data(s, ssl_qc_app_data_index); |
| 898 | #endif |
| 899 | |
| 900 | if (conn) |
| 901 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
| 902 | #ifdef USE_QUIC |
| 903 | else if (qc) |
| 904 | ref = qc->li->bind_conf->keys_ref; |
| 905 | #endif |
| 906 | |
| 907 | if (!ref) { |
| 908 | /* must never happen */ |
| 909 | ABORT_NOW(); |
| 910 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 911 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 912 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 913 | |
| 914 | keys = ref->tlskeys; |
| 915 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 916 | |
| 917 | if (enc) { |
| 918 | memcpy(key_name, keys[head].name, 16); |
| 919 | |
| 920 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 921 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 922 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 923 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 924 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 925 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 926 | goto end; |
| 927 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 928 | if (ssl_hmac_init(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0) |
| 929 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 930 | ret = 1; |
| 931 | } |
| 932 | else if (ref->key_size_bits == 256 ) { |
| 933 | |
| 934 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 935 | goto end; |
| 936 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 937 | if (ssl_hmac_init(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0) |
| 938 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 939 | ret = 1; |
| 940 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 941 | } else { |
| 942 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 943 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 944 | goto found; |
| 945 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 946 | ret = 0; |
| 947 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 948 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 949 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 950 | if (ref->key_size_bits == 128) { |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 951 | if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0) |
| 952 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 953 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 954 | goto end; |
| 955 | /* 2 for key renewal, 1 if current key is still valid */ |
| 956 | ret = i ? 2 : 1; |
| 957 | } |
| 958 | else if (ref->key_size_bits == 256) { |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 959 | if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0) |
| 960 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 961 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 962 | goto end; |
| 963 | /* 2 for key renewal, 1 if current key is still valid */ |
| 964 | ret = i ? 2 : 1; |
| 965 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 966 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 967 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 968 | end: |
| 969 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 970 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 974 | { |
| 975 | struct tls_keys_ref *ref; |
| 976 | |
| 977 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 978 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 979 | return ref; |
| 980 | return NULL; |
| 981 | } |
| 982 | |
| 983 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 984 | { |
| 985 | struct tls_keys_ref *ref; |
| 986 | |
| 987 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 988 | if (ref->unique_id == unique_id) |
| 989 | return ref; |
| 990 | return NULL; |
| 991 | } |
| 992 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 993 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 994 | * match existing ones, this function returns -1 |
| 995 | * else it returns 0 on success. |
| 996 | */ |
| 997 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 998 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 999 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1000 | if (ref->key_size_bits == 128) { |
| 1001 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1002 | return -1; |
| 1003 | } |
| 1004 | else if (ref->key_size_bits == 256) { |
| 1005 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1006 | return -1; |
| 1007 | } |
| 1008 | else |
| 1009 | return -1; |
| 1010 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1011 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1012 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1013 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1014 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1015 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1016 | |
| 1017 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1018 | } |
| 1019 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1020 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1021 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1022 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1023 | |
| 1024 | if(!ref) { |
| 1025 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1026 | return 1; |
| 1027 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1028 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1029 | memprintf(err, "Invalid key size"); |
| 1030 | return 1; |
| 1031 | } |
| 1032 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1033 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1034 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1035 | |
| 1036 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1037 | * automatic ids. It's called just after the basic checks. It returns |
| 1038 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1039 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1040 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1041 | { |
| 1042 | int i = 0; |
| 1043 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1044 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1045 | |
| 1046 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1047 | if (ref->unique_id == -1) { |
| 1048 | /* Look for the first free id. */ |
| 1049 | while (1) { |
| 1050 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1051 | if (ref2->unique_id == i) { |
| 1052 | i++; |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | if (&ref2->list == &tlskeys_reference) |
| 1057 | break; |
| 1058 | } |
| 1059 | |
| 1060 | /* Uses the unique id and increment it for the next entry. */ |
| 1061 | ref->unique_id = i; |
| 1062 | i++; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | /* This sort the reference list by id. */ |
| 1067 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1068 | LIST_DELETE(&ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1069 | list_for_each_entry(ref3, &tkr, list) { |
| 1070 | if (ref->unique_id < ref3->unique_id) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1071 | LIST_APPEND(&ref3->list, &ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1072 | break; |
| 1073 | } |
| 1074 | } |
| 1075 | if (&ref3->list == &tkr) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1076 | LIST_APPEND(&tkr, &ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | /* swap root */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1080 | LIST_INSERT(&tkr, &tlskeys_reference); |
| 1081 | LIST_DELETE(&tkr); |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1082 | return ERR_NONE; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1083 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1084 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1085 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1086 | |
Ilya Shipitsin | b3201a3 | 2020-10-18 09:11:50 +0500 | [diff] [blame] | 1087 | #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] | 1088 | /* |
| 1089 | * 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] | 1090 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1091 | * status extension, the issuer's certificate is mandatory. It should be |
| 1092 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1093 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1094 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1095 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1096 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1097 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1098 | * |
| 1099 | * 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] | 1100 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1101 | */ |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1102 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, struct ckch_data *data, STACK_OF(X509) *chain) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1103 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1104 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1105 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1106 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1107 | char *warn = NULL; |
| 1108 | unsigned char *p; |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1109 | #ifndef USE_OPENSSL_WOLFSSL |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1110 | void (*callback) (void); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1111 | #else |
| 1112 | tlsextStatusCb callback; |
| 1113 | #endif |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1114 | struct buffer *ocsp_uri = get_trash_chunk(); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1115 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1116 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 1117 | x = data->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1118 | if (!x) |
| 1119 | goto out; |
| 1120 | |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1121 | if (data->ocsp_update_mode == SSL_SOCK_OCSP_UPDATE_ON) { |
| 1122 | ssl_ocsp_get_uri_from_cert(x, ocsp_uri, NULL); |
| 1123 | /* We should have an "OCSP URI" field in order for auto update to work. */ |
| 1124 | if (b_data(ocsp_uri) == 0) |
| 1125 | goto out; |
| 1126 | } |
| 1127 | |
| 1128 | /* In case of ocsp update mode set to 'on', this function might be |
| 1129 | * called with no known ocsp response. If no ocsp uri can be found in |
| 1130 | * the certificate, nothing needs to be done here. */ |
| 1131 | if (!data->ocsp_response) { |
| 1132 | if (data->ocsp_update_mode != SSL_SOCK_OCSP_UPDATE_ON || b_data(ocsp_uri) == 0) { |
| 1133 | ret = 0; |
| 1134 | goto out; |
| 1135 | } |
| 1136 | } |
| 1137 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 1138 | issuer = data->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1139 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1140 | if (chain) { |
| 1141 | /* check if one of the certificate of the chain is the issuer */ |
| 1142 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1143 | X509 *ti = sk_X509_value(chain, i); |
| 1144 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1145 | issuer = ti; |
| 1146 | break; |
| 1147 | } |
| 1148 | } |
| 1149 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1150 | if (!issuer) |
| 1151 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1152 | |
Remi Tricot-Le Breton | 2d1daa8 | 2023-01-09 12:02:47 +0100 | [diff] [blame] | 1153 | if (!data->ocsp_cid) |
| 1154 | data->ocsp_cid = OCSP_cert_to_id(0, x, issuer); |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1155 | if (!data->ocsp_cid) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1156 | goto out; |
| 1157 | |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1158 | i = i2d_OCSP_CERTID(data->ocsp_cid, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1159 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1160 | goto out; |
| 1161 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1162 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1163 | if (!ocsp) |
| 1164 | goto out; |
| 1165 | |
| 1166 | p = ocsp->key_data; |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1167 | ocsp->key_length = i2d_OCSP_CERTID(data->ocsp_cid, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1168 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1169 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1170 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1171 | if (iocsp == ocsp) |
| 1172 | ocsp = NULL; |
| 1173 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1174 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1175 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1176 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1177 | #endif |
| 1178 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1179 | |
| 1180 | if (!callback) { |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1181 | struct ocsp_cbk_arg *cb_arg; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1182 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1183 | |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1184 | cb_arg = calloc(1, sizeof(*cb_arg)); |
| 1185 | if (!cb_arg) |
| 1186 | goto out; |
| 1187 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1188 | cb_arg->is_single = 1; |
| 1189 | cb_arg->s_ocsp = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1190 | iocsp->refcount++; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1191 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1192 | pkey = X509_get_pubkey(x); |
| 1193 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1194 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1195 | |
| 1196 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1197 | 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 */ |
| 1198 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1199 | } else { |
| 1200 | /* |
| 1201 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1202 | * Update that cb_arg with the new cert's staple |
| 1203 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1204 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1205 | struct certificate_ocsp *tmp_ocsp; |
| 1206 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1207 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1208 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1209 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1210 | cb_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1211 | |
| 1212 | /* |
| 1213 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1214 | * the order of operations below matter, take care when changing it |
| 1215 | */ |
| 1216 | tmp_ocsp = cb_arg->s_ocsp; |
| 1217 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1218 | cb_arg->s_ocsp = NULL; |
| 1219 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1220 | cb_arg->is_single = 0; |
| 1221 | cb_arg->single_kt = 0; |
| 1222 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1223 | pkey = X509_get_pubkey(x); |
| 1224 | key_type = EVP_PKEY_base_id(pkey); |
| 1225 | EVP_PKEY_free(pkey); |
| 1226 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1227 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1228 | if (index >= 0 && !cb_arg->m_ocsp[index]) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1229 | cb_arg->m_ocsp[index] = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1230 | iocsp->refcount++; |
| 1231 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1232 | } |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1233 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1234 | |
| 1235 | ret = 0; |
| 1236 | |
| 1237 | warn = NULL; |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1238 | 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] | 1239 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1240 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1241 | } |
| 1242 | |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1243 | if (data->ocsp_update_mode == SSL_SOCK_OCSP_UPDATE_ON) { |
| 1244 | |
| 1245 | /* Do not insert the same certificate_ocsp structure in the |
| 1246 | * update tree more than once. */ |
| 1247 | if (!ocsp) { |
Remi Tricot-Le Breton | 112b16a | 2023-01-09 12:02:44 +0100 | [diff] [blame] | 1248 | /* Issuer certificate is not included in the certificate |
| 1249 | * chain, it will have to be treated separately during |
| 1250 | * ocsp response validation. */ |
| 1251 | if (issuer == data->ocsp_issuer) { |
| 1252 | iocsp->issuer = issuer; |
| 1253 | X509_up_ref(issuer); |
| 1254 | } |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1255 | if (data->chain) |
| 1256 | iocsp->chain = X509_chain_up_ref(data->chain); |
| 1257 | |
Remi Tricot-Le Breton | 648c83e | 2023-01-09 12:02:48 +0100 | [diff] [blame] | 1258 | iocsp->uri = calloc(1, sizeof(*iocsp->uri)); |
| 1259 | if (!chunk_dup(iocsp->uri, ocsp_uri)) { |
| 1260 | ha_free(&iocsp->uri); |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1261 | goto out; |
Remi Tricot-Le Breton | 648c83e | 2023-01-09 12:02:48 +0100 | [diff] [blame] | 1262 | } |
Remi Tricot-Le Breton | b55be8c | 2022-12-20 11:11:12 +0100 | [diff] [blame] | 1263 | |
| 1264 | ssl_ocsp_update_insert(iocsp); |
| 1265 | } |
| 1266 | } |
| 1267 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1268 | out: |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1269 | if (ret && data->ocsp_cid) |
| 1270 | OCSP_CERTID_free(data->ocsp_cid); |
| 1271 | |
| 1272 | if (!ret && data->ocsp_response) { |
| 1273 | ha_free(&data->ocsp_response->area); |
| 1274 | ha_free(&data->ocsp_response); |
| 1275 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1276 | |
| 1277 | if (ocsp) |
Remi Tricot-Le Breton | aa529f7 | 2022-11-03 15:16:49 +0100 | [diff] [blame] | 1278 | ssl_sock_free_ocsp(ocsp); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1279 | |
| 1280 | if (warn) |
| 1281 | free(warn); |
| 1282 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1283 | return ret; |
| 1284 | } |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 1285 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 1286 | #endif |
| 1287 | |
| 1288 | #ifdef OPENSSL_IS_BORINGSSL |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1289 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, struct ckch_data *data, STACK_OF(X509) *chain) |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1290 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1291 | 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] | 1292 | } |
| 1293 | #endif |
| 1294 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1295 | |
Ilya Shipitsin | 7bbf586 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 1296 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1297 | |
| 1298 | #define CT_EXTENSION_TYPE 18 |
| 1299 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1300 | int sctl_ex_index = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1301 | |
| 1302 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1303 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1304 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1305 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1306 | *out = (unsigned char *) sctl->area; |
| 1307 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1308 | |
| 1309 | return 1; |
| 1310 | } |
| 1311 | |
| 1312 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1313 | { |
| 1314 | return 1; |
| 1315 | } |
| 1316 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1317 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1318 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1319 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1320 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1321 | 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] | 1322 | goto out; |
| 1323 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1324 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1325 | |
| 1326 | ret = 0; |
| 1327 | |
| 1328 | out: |
| 1329 | return ret; |
| 1330 | } |
| 1331 | |
| 1332 | #endif |
| 1333 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1334 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1335 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1336 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1337 | #ifdef USE_QUIC |
| 1338 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1339 | #endif /* USE_QUIC */ |
| 1340 | struct ssl_sock_ctx *ctx = NULL; |
| 1341 | |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1342 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1343 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1344 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1345 | if (conn) |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 1346 | ctx = conn_get_ssl_sock_ctx(conn); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1347 | #ifdef USE_QUIC |
| 1348 | else if (qc) |
| 1349 | ctx = qc->xprt_ctx; |
| 1350 | #endif /* USE_QUIC */ |
Amaury Denoyelle | 7c564bf | 2022-01-24 11:04:05 +0100 | [diff] [blame] | 1351 | |
| 1352 | if (!ctx) { |
| 1353 | /* must never happen */ |
| 1354 | ABORT_NOW(); |
| 1355 | return; |
| 1356 | } |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1357 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1358 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1359 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1360 | * change this to #if and do not assign a default value to this macro! |
| 1361 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1362 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1363 | /* Disable renegotiation (CVE-2009-3555) */ |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1364 | 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] | 1365 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1366 | conn->err_code = CO_ER_SSL_RENEG; |
| 1367 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1368 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1369 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1370 | |
| 1371 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1372 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1373 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1374 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1375 | consider that the buffering was activated, |
| 1376 | so we rise the output buffer size from 4k |
| 1377 | to 16k */ |
| 1378 | write_bio = SSL_get_wbio(ssl); |
| 1379 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1380 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1381 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1382 | } |
| 1383 | } |
| 1384 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1385 | } |
| 1386 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1387 | /* Callback is called for each certificate of the chain during a verify |
| 1388 | ok is set to 1 if preverify detect no error on current certificate. |
| 1389 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1390 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1391 | { |
| 1392 | SSL *ssl; |
| 1393 | struct connection *conn; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1394 | struct ssl_sock_ctx *ctx = NULL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1395 | int err, depth; |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1396 | X509 *client_crt; |
| 1397 | STACK_OF(X509) *certs; |
Frédéric Lécaille | 48e46f9 | 2022-10-14 09:34:00 +0200 | [diff] [blame] | 1398 | struct bind_conf *bind_conf = NULL; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1399 | struct quic_conn *qc = NULL; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1400 | |
| 1401 | 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] | 1402 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1403 | client_crt = SSL_get_ex_data(ssl, ssl_client_crt_ref_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1404 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1405 | if (conn) { |
| 1406 | bind_conf = __objt_listener(conn->target)->bind_conf; |
| 1407 | ctx = __conn_get_ssl_sock_ctx(conn); |
| 1408 | } |
| 1409 | #ifdef USE_QUIC |
| 1410 | else { |
| 1411 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Amaury Denoyelle | ba303de | 2022-10-17 18:46:49 +0200 | [diff] [blame] | 1412 | BUG_ON(!qc); /* Must never happen */ |
| 1413 | bind_conf = qc->li->bind_conf; |
| 1414 | ctx = qc->xprt_ctx; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1415 | } |
| 1416 | #endif |
| 1417 | |
Frédéric Lécaille | 48e46f9 | 2022-10-14 09:34:00 +0200 | [diff] [blame] | 1418 | BUG_ON(!ctx || !bind_conf); |
Christopher Faulet | 881cce9 | 2022-11-23 09:27:13 +0100 | [diff] [blame] | 1419 | ALREADY_CHECKED(ctx); |
| 1420 | ALREADY_CHECKED(bind_conf); |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1421 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1422 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1423 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1424 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1425 | err = X509_STORE_CTX_get_error(x_store); |
| 1426 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1427 | if (ok) /* no errors */ |
| 1428 | return ok; |
| 1429 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1430 | /* Keep a reference to the client's certificate in order to be able to |
| 1431 | * dump some fetches values in a log even when the verification process |
| 1432 | * fails. */ |
| 1433 | if (depth == 0) { |
| 1434 | X509_free(client_crt); |
| 1435 | client_crt = X509_STORE_CTX_get0_cert(x_store); |
| 1436 | if (client_crt) { |
| 1437 | X509_up_ref(client_crt); |
| 1438 | SSL_set_ex_data(ssl, ssl_client_crt_ref_index, client_crt); |
| 1439 | } |
| 1440 | } |
| 1441 | else { |
| 1442 | /* An error occurred on a CA certificate of the certificate |
| 1443 | * chain, we might never call this verify callback on the client |
| 1444 | * certificate's depth (which is 0) so we try to store the |
| 1445 | * reference right now. */ |
Remi Tricot-Le Breton | f95c295 | 2021-08-20 09:51:23 +0200 | [diff] [blame] | 1446 | certs = X509_STORE_CTX_get1_chain(x_store); |
| 1447 | if (certs) { |
| 1448 | client_crt = sk_X509_value(certs, 0); |
| 1449 | if (client_crt) { |
| 1450 | X509_up_ref(client_crt); |
| 1451 | 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] | 1452 | } |
| 1453 | sk_X509_pop_free(certs, X509_free); |
| 1454 | } |
| 1455 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1456 | |
| 1457 | /* check if CA error needs to be ignored */ |
| 1458 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1459 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1460 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1461 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1462 | } |
| 1463 | |
Remi Tricot-Le Breton | 9b25982 | 2022-11-10 10:48:58 +0100 | [diff] [blame] | 1464 | if (err <= SSL_MAX_VFY_ERROR_CODE && |
William Lallemand | 4639689 | 2022-11-10 16:45:24 +0100 | [diff] [blame] | 1465 | 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] | 1466 | goto err_ignored; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1467 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1468 | /* TODO: for QUIC connection, this error code is lost */ |
| 1469 | if (conn) |
| 1470 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1471 | return 0; |
| 1472 | } |
| 1473 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1474 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1475 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1476 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1477 | /* check if certificate error needs to be ignored */ |
Remi Tricot-Le Breton | 9b25982 | 2022-11-10 10:48:58 +0100 | [diff] [blame] | 1478 | if (err <= SSL_MAX_VFY_ERROR_CODE && |
William Lallemand | 4639689 | 2022-11-10 16:45:24 +0100 | [diff] [blame] | 1479 | cert_ignerr_bitfield_get(bind_conf->crt_ignerr_bitfield, err)) |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1480 | goto err_ignored; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1481 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1482 | /* TODO: for QUIC connection, this error code is lost */ |
| 1483 | if (conn) |
| 1484 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1485 | return 0; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1486 | |
| 1487 | err_ignored: |
| 1488 | ssl_sock_dump_errors(conn, qc); |
| 1489 | ERR_clear_error(); |
| 1490 | return 1; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1491 | } |
| 1492 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1493 | #ifdef TLS1_RT_HEARTBEAT |
| 1494 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 1495 | int content_type, const void *buf, size_t len, |
| 1496 | SSL *ssl) |
| 1497 | { |
| 1498 | /* test heartbeat received (write_p is set to 0 |
| 1499 | for a received record) */ |
| 1500 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 1501 | struct ssl_sock_ctx *ctx = __conn_get_ssl_sock_ctx(conn); |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 1502 | const unsigned char *p = buf; |
| 1503 | unsigned int payload; |
| 1504 | |
| 1505 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
| 1506 | |
| 1507 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1508 | if (*p != TLS1_HB_REQUEST) |
| 1509 | return; |
| 1510 | |
| 1511 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
| 1512 | goto kill_it; |
| 1513 | |
| 1514 | payload = (p[1] * 256) + p[2]; |
| 1515 | if (3 + payload + 16 <= len) |
| 1516 | return; /* OK no problem */ |
| 1517 | kill_it: |
| 1518 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1519 | * advertised payload is larger than the advertised packet |
| 1520 | * length, so we have garbage in the buffer between the |
| 1521 | * payload and the end of the buffer (p+len). We can't know |
| 1522 | * if the SSL stack is patched, and we don't know if we can |
| 1523 | * safely wipe out the area between p+3+len and payload. |
| 1524 | * So instead, we prevent the response from being sent by |
| 1525 | * setting the max_send_fragment to 0 and we report an SSL |
| 1526 | * error, which will kill this connection. It will be reported |
| 1527 | * above as SSL_ERROR_SSL while an other handshake failure with |
| 1528 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1529 | */ |
| 1530 | ssl->max_send_fragment = 0; |
| 1531 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1532 | } |
| 1533 | } |
| 1534 | #endif |
| 1535 | |
| 1536 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 1537 | int content_type, const void *buf, size_t len, |
| 1538 | SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1539 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1540 | struct ssl_capture *capture; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1541 | uchar *msg; |
| 1542 | uchar *end; |
| 1543 | uchar *extensions_end; |
| 1544 | uchar *ec_start = NULL; |
| 1545 | uchar *ec_formats_start = NULL; |
| 1546 | uchar *list_end; |
| 1547 | ushort protocol_version; |
| 1548 | ushort extension_id; |
| 1549 | ushort ec_len = 0; |
| 1550 | uchar ec_formats_len = 0; |
| 1551 | int offset = 0; |
| 1552 | int rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1553 | |
| 1554 | /* This function is called for "from client" and "to server" |
| 1555 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1556 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1557 | */ |
| 1558 | |
| 1559 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1560 | * otherwise it is set to 1. |
| 1561 | */ |
| 1562 | if (write_p != 0) |
| 1563 | return; |
| 1564 | |
| 1565 | /* content_type contains the type of message received or sent |
| 1566 | * according with the SSL/TLS protocol spec. This message is |
| 1567 | * encoded with one byte. The value 256 (two bytes) is used |
| 1568 | * for designing the SSL/TLS record layer. According with the |
| 1569 | * rfc6101, the expected message (other than 256) are: |
| 1570 | * - change_cipher_spec(20) |
| 1571 | * - alert(21) |
| 1572 | * - handshake(22) |
| 1573 | * - application_data(23) |
| 1574 | * - (255) |
| 1575 | * We are interessed by the handshake and specially the client |
| 1576 | * hello. |
| 1577 | */ |
| 1578 | if (content_type != 22) |
| 1579 | return; |
| 1580 | |
| 1581 | /* The message length is at least 4 bytes, containing the |
| 1582 | * message type and the message length. |
| 1583 | */ |
| 1584 | if (len < 4) |
| 1585 | return; |
| 1586 | |
| 1587 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1588 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1589 | * - hello_request(0) |
| 1590 | * - client_hello(1) |
| 1591 | * - server_hello(2) |
| 1592 | * - certificate(11) |
| 1593 | * - server_key_exchange (12) |
| 1594 | * - certificate_request(13) |
| 1595 | * - server_hello_done(14) |
| 1596 | * We are interested by the client hello. |
| 1597 | */ |
| 1598 | msg = (unsigned char *)buf; |
| 1599 | if (msg[0] != 1) |
| 1600 | return; |
| 1601 | |
| 1602 | /* Next three bytes are the length of the message. The total length |
| 1603 | * must be this decoded length + 4. If the length given as argument |
| 1604 | * is not the same, we abort the protocol dissector. |
| 1605 | */ |
| 1606 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1607 | if (len < rec_len + 4) |
| 1608 | return; |
| 1609 | msg += 4; |
| 1610 | end = msg + rec_len; |
| 1611 | if (end < msg) |
| 1612 | return; |
| 1613 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1614 | /* Expect 2 bytes for protocol version |
| 1615 | * (1 byte for major and 1 byte for minor) |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1616 | */ |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1617 | if (msg + 2 > end) |
| 1618 | return; |
| 1619 | protocol_version = (msg[0] << 8) + msg[1]; |
| 1620 | msg += 2; |
| 1621 | |
| 1622 | /* Expect the random, composed by 4 bytes for the unix time and |
| 1623 | * 28 bytes for unix payload. So we jump 4 + 28. |
| 1624 | */ |
| 1625 | msg += 4 + 28; |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1626 | if (msg > end) |
| 1627 | return; |
| 1628 | |
| 1629 | /* Next, is session id: |
| 1630 | * if present, we have to jump by length + 1 for the size information |
| 1631 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1632 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1633 | if (msg[0] > 0) |
| 1634 | msg += msg[0]; |
| 1635 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1636 | if (msg > end) |
| 1637 | return; |
| 1638 | |
| 1639 | /* Next two bytes are the ciphersuite length. */ |
| 1640 | if (msg + 2 > end) |
| 1641 | return; |
| 1642 | rec_len = (msg[0] << 8) + msg[1]; |
| 1643 | msg += 2; |
| 1644 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1645 | return; |
| 1646 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1647 | capture = pool_zalloc(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1648 | if (!capture) |
| 1649 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1650 | /* Compute the xxh64 of the ciphersuite. */ |
| 1651 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1652 | |
| 1653 | /* Capture the ciphersuite. */ |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1654 | capture->ciphersuite_len = MIN(global_ssl.capture_buffer_size, rec_len); |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1655 | capture->ciphersuite_offset = 0; |
| 1656 | memcpy(capture->data, msg, capture->ciphersuite_len); |
| 1657 | msg += rec_len; |
| 1658 | offset += capture->ciphersuite_len; |
| 1659 | |
| 1660 | /* Initialize other data */ |
| 1661 | capture->protocol_version = protocol_version; |
| 1662 | |
| 1663 | /* Next, compression methods: |
| 1664 | * if present, we have to jump by length + 1 for the size information |
| 1665 | * if not present, we have to jump by 1 only |
| 1666 | */ |
| 1667 | if (msg[0] > 0) |
| 1668 | msg += msg[0]; |
| 1669 | msg += 1; |
| 1670 | if (msg > end) |
| 1671 | goto store_capture; |
| 1672 | |
| 1673 | /* We reached extensions */ |
| 1674 | if (msg + 2 > end) |
| 1675 | goto store_capture; |
| 1676 | rec_len = (msg[0] << 8) + msg[1]; |
| 1677 | msg += 2; |
| 1678 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1679 | goto store_capture; |
| 1680 | extensions_end = msg + rec_len; |
| 1681 | capture->extensions_offset = offset; |
| 1682 | |
| 1683 | /* Parse each extension */ |
| 1684 | while (msg + 4 < extensions_end) { |
| 1685 | /* Add 2 bytes of extension_id */ |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1686 | if (global_ssl.capture_buffer_size >= offset + 2) { |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1687 | capture->data[offset++] = msg[0]; |
| 1688 | capture->data[offset++] = msg[1]; |
| 1689 | capture->extensions_len += 2; |
| 1690 | } |
| 1691 | else |
| 1692 | break; |
| 1693 | extension_id = (msg[0] << 8) + msg[1]; |
| 1694 | /* Length of the extension */ |
| 1695 | rec_len = (msg[2] << 8) + msg[3]; |
| 1696 | |
| 1697 | /* Expect 2 bytes extension id + 2 bytes extension size */ |
| 1698 | msg += 2 + 2; |
| 1699 | if (msg + rec_len > extensions_end || msg + rec_len < msg) |
| 1700 | goto store_capture; |
| 1701 | /* TLS Extensions |
| 1702 | * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */ |
| 1703 | if (extension_id == 0x000a) { |
| 1704 | /* Elliptic Curves: |
| 1705 | * https://www.rfc-editor.org/rfc/rfc8422.html |
| 1706 | * https://www.rfc-editor.org/rfc/rfc7919.html */ |
| 1707 | list_end = msg + rec_len; |
| 1708 | if (msg + 2 > list_end) |
| 1709 | goto store_capture; |
| 1710 | rec_len = (msg[0] << 8) + msg[1]; |
| 1711 | msg += 2; |
| 1712 | |
| 1713 | if (msg + rec_len > list_end || msg + rec_len < msg) |
| 1714 | goto store_capture; |
| 1715 | /* Store location/size of the list */ |
| 1716 | ec_start = msg; |
| 1717 | ec_len = rec_len; |
| 1718 | } |
| 1719 | else if (extension_id == 0x000b) { |
| 1720 | /* Elliptic Curves Point Formats: |
| 1721 | * https://www.rfc-editor.org/rfc/rfc8422.html */ |
| 1722 | list_end = msg + rec_len; |
| 1723 | if (msg + 1 > list_end) |
| 1724 | goto store_capture; |
| 1725 | rec_len = msg[0]; |
| 1726 | msg += 1; |
| 1727 | |
| 1728 | if (msg + rec_len > list_end || msg + rec_len < msg) |
| 1729 | goto store_capture; |
| 1730 | /* Store location/size of the list */ |
| 1731 | ec_formats_start = msg; |
| 1732 | ec_formats_len = rec_len; |
| 1733 | } |
| 1734 | msg += rec_len; |
| 1735 | } |
| 1736 | |
| 1737 | if (ec_start) { |
| 1738 | rec_len = ec_len; |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1739 | if (offset + rec_len > global_ssl.capture_buffer_size) |
| 1740 | rec_len = global_ssl.capture_buffer_size - offset; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1741 | memcpy(capture->data + offset, ec_start, rec_len); |
| 1742 | capture->ec_offset = offset; |
| 1743 | capture->ec_len = rec_len; |
| 1744 | offset += rec_len; |
| 1745 | } |
| 1746 | if (ec_formats_start) { |
| 1747 | rec_len = ec_formats_len; |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 1748 | if (offset + rec_len > global_ssl.capture_buffer_size) |
| 1749 | rec_len = global_ssl.capture_buffer_size - offset; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1750 | memcpy(capture->data + offset, ec_formats_start, rec_len); |
| 1751 | capture->ec_formats_offset = offset; |
| 1752 | capture->ec_formats_len = rec_len; |
| 1753 | offset += rec_len; |
| 1754 | } |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1755 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 1756 | store_capture: |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1757 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1758 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1759 | |
| 1760 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 1761 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1762 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 1763 | int content_type, const void *buf, size_t len, |
| 1764 | SSL *ssl) |
| 1765 | { |
| 1766 | struct ssl_keylog *keylog; |
| 1767 | |
| 1768 | if (SSL_get_ex_data(ssl, ssl_keylog_index)) |
| 1769 | return; |
| 1770 | |
Willy Tarreau | f208ac0 | 2021-03-22 21:10:12 +0100 | [diff] [blame] | 1771 | keylog = pool_zalloc(pool_head_ssl_keylog); |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1772 | if (!keylog) |
| 1773 | return; |
| 1774 | |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 1775 | if (!SSL_set_ex_data(ssl, ssl_keylog_index, keylog)) { |
| 1776 | pool_free(pool_head_ssl_keylog, keylog); |
| 1777 | return; |
| 1778 | } |
| 1779 | } |
| 1780 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1781 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1782 | /* Callback is called for ssl protocol analyse */ |
| 1783 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1784 | { |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1785 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 1786 | struct ssl_sock_msg_callback *cbk; |
| 1787 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 1788 | /* Try to call all callback functions that were registered by using |
| 1789 | * ssl_sock_register_msg_callback(). |
| 1790 | */ |
| 1791 | list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) { |
| 1792 | cbk->func(conn, write_p, version, content_type, buf, len, ssl); |
| 1793 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1794 | } |
| 1795 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1796 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1797 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1798 | const unsigned char *in, unsigned int inlen, |
| 1799 | void *arg) |
| 1800 | { |
| 1801 | struct server *srv = arg; |
| 1802 | |
| 1803 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1804 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1805 | return SSL_TLSEXT_ERR_OK; |
| 1806 | return SSL_TLSEXT_ERR_NOACK; |
| 1807 | } |
| 1808 | #endif |
| 1809 | |
| 1810 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1811 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1812 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1813 | */ |
| 1814 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1815 | unsigned int *len, void *arg) |
| 1816 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1817 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1818 | |
| 1819 | *data = (const unsigned char *)conf->npn_str; |
| 1820 | *len = conf->npn_len; |
| 1821 | return SSL_TLSEXT_ERR_OK; |
| 1822 | } |
| 1823 | #endif |
| 1824 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1825 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1826 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1827 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1828 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1829 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1830 | unsigned char *outlen, |
| 1831 | const unsigned char *server, |
| 1832 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1833 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1834 | struct ssl_bind_conf *conf = arg; |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 1835 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1836 | 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] | 1837 | #endif |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1838 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1839 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1840 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 1841 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1842 | if (qc) |
| 1843 | 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] | 1844 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1845 | return SSL_TLSEXT_ERR_NOACK; |
| 1846 | } |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 1847 | |
| 1848 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1849 | if (qc && !quic_set_app_ops(qc, *out, *outlen)) { |
| 1850 | 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] | 1851 | return SSL_TLSEXT_ERR_NOACK; |
| 1852 | } |
| 1853 | #endif |
| 1854 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1855 | return SSL_TLSEXT_ERR_OK; |
| 1856 | } |
| 1857 | #endif |
| 1858 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1859 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1860 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1861 | |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 1862 | /* Configure a DNS SAN extension on a certificate. */ |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1863 | int ssl_sock_add_san_ext(X509V3_CTX* ctx, X509* cert, const char *servername) { |
| 1864 | int failure = 0; |
| 1865 | X509_EXTENSION *san_ext = NULL; |
| 1866 | CONF *conf = NULL; |
| 1867 | struct buffer *san_name = get_trash_chunk(); |
| 1868 | |
| 1869 | conf = NCONF_new(NULL); |
| 1870 | if (!conf) { |
| 1871 | failure = 1; |
| 1872 | goto cleanup; |
| 1873 | } |
| 1874 | |
| 1875 | /* Build an extension based on the DNS entry above */ |
| 1876 | chunk_appendf(san_name, "DNS:%s", servername); |
| 1877 | san_ext = X509V3_EXT_nconf_nid(conf, ctx, NID_subject_alt_name, san_name->area); |
| 1878 | if (!san_ext) { |
| 1879 | failure = 1; |
| 1880 | goto cleanup; |
| 1881 | } |
| 1882 | |
| 1883 | /* Add the extension */ |
| 1884 | if (!X509_add_ext(cert, san_ext, -1 /* Add to end */)) { |
| 1885 | failure = 1; |
| 1886 | goto cleanup; |
| 1887 | } |
| 1888 | |
| 1889 | /* Success */ |
| 1890 | failure = 0; |
| 1891 | |
| 1892 | cleanup: |
| 1893 | if (NULL != san_ext) X509_EXTENSION_free(san_ext); |
| 1894 | if (NULL != conf) NCONF_free(conf); |
| 1895 | |
| 1896 | return failure; |
| 1897 | } |
| 1898 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1899 | /* Create a X509 certificate with the specified servername and serial. This |
| 1900 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1901 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1902 | 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] | 1903 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 1904 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
| 1905 | EVP_PKEY *capkey = bind_conf->ca_sign_ckch->key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1906 | SSL_CTX *ssl_ctx = NULL; |
| 1907 | X509 *newcrt = NULL; |
| 1908 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1909 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1910 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1911 | X509_NAME *name; |
| 1912 | const EVP_MD *digest; |
| 1913 | X509V3_CTX ctx; |
| 1914 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1915 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1916 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1917 | /* Get the private key of the default certificate and use it */ |
Ilya Shipitsin | af20488 | 2020-12-19 03:12:12 +0500 | [diff] [blame] | 1918 | #ifdef HAVE_SSL_CTX_get0_privatekey |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1919 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1920 | #else |
| 1921 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1922 | if (tmp_ssl) |
| 1923 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1924 | #endif |
| 1925 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1926 | goto mkcert_error; |
| 1927 | |
| 1928 | /* Create the certificate */ |
| 1929 | if (!(newcrt = X509_new())) |
| 1930 | goto mkcert_error; |
| 1931 | |
| 1932 | /* Set version number for the certificate (X509v3) and the serial |
| 1933 | * number */ |
| 1934 | if (X509_set_version(newcrt, 2L) != 1) |
| 1935 | goto mkcert_error; |
Willy Tarreau | 1db4273 | 2021-04-06 11:44:07 +0200 | [diff] [blame] | 1936 | 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] | 1937 | |
| 1938 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1939 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1940 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1941 | goto mkcert_error; |
| 1942 | |
| 1943 | /* set public key in the certificate */ |
| 1944 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1945 | goto mkcert_error; |
| 1946 | |
| 1947 | /* Set issuer name from the CA */ |
| 1948 | if (!(name = X509_get_subject_name(cacert))) |
| 1949 | goto mkcert_error; |
| 1950 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1951 | goto mkcert_error; |
| 1952 | |
| 1953 | /* Set the subject name using the same, but the CN */ |
| 1954 | name = X509_NAME_dup(name); |
| 1955 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1956 | (const unsigned char *)servername, |
| 1957 | -1, -1, 0) != 1) { |
| 1958 | X509_NAME_free(name); |
| 1959 | goto mkcert_error; |
| 1960 | } |
| 1961 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1962 | X509_NAME_free(name); |
| 1963 | goto mkcert_error; |
| 1964 | } |
| 1965 | X509_NAME_free(name); |
| 1966 | |
| 1967 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1968 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1969 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1970 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1971 | X509_EXTENSION *ext; |
| 1972 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1973 | 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] | 1974 | goto mkcert_error; |
| 1975 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1976 | X509_EXTENSION_free(ext); |
| 1977 | goto mkcert_error; |
| 1978 | } |
| 1979 | X509_EXTENSION_free(ext); |
| 1980 | } |
| 1981 | |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 1982 | /* Add SAN extension */ |
| 1983 | if (ssl_sock_add_san_ext(&ctx, newcrt, servername)) { |
| 1984 | goto mkcert_error; |
| 1985 | } |
| 1986 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1987 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1988 | |
| 1989 | key_type = EVP_PKEY_base_id(capkey); |
| 1990 | |
| 1991 | if (key_type == EVP_PKEY_DSA) |
| 1992 | digest = EVP_sha1(); |
| 1993 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1994 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1995 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1996 | digest = EVP_sha256(); |
| 1997 | else { |
Ilya Shipitsin | ec36c91 | 2021-01-07 11:57:42 +0500 | [diff] [blame] | 1998 | #ifdef ASN1_PKEY_CTRL_DEFAULT_MD_NID |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1999 | int nid; |
| 2000 | |
| 2001 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 2002 | goto mkcert_error; |
| 2003 | if (!(digest = EVP_get_digestbynid(nid))) |
| 2004 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 2005 | #else |
| 2006 | goto mkcert_error; |
| 2007 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2008 | } |
| 2009 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2010 | if (!(X509_sign(newcrt, capkey, digest))) |
| 2011 | goto mkcert_error; |
| 2012 | |
| 2013 | /* Create and set the new SSL_CTX */ |
| 2014 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 2015 | goto mkcert_error; |
| 2016 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 2017 | goto mkcert_error; |
| 2018 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 2019 | goto mkcert_error; |
| 2020 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 2021 | goto mkcert_error; |
| 2022 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2023 | /* Build chaining the CA cert and the rest of the chain, keep these order */ |
| 2024 | #if defined(SSL_CTX_add1_chain_cert) |
| 2025 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, bind_conf->ca_sign_ckch->cert)) { |
| 2026 | goto mkcert_error; |
| 2027 | } |
| 2028 | |
| 2029 | if (bind_conf->ca_sign_ckch->chain) { |
| 2030 | for (i = 0; i < sk_X509_num(bind_conf->ca_sign_ckch->chain); i++) { |
| 2031 | X509 *chain_cert = sk_X509_value(bind_conf->ca_sign_ckch->chain, i); |
| 2032 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, chain_cert)) { |
| 2033 | goto mkcert_error; |
| 2034 | } |
| 2035 | } |
| 2036 | } |
| 2037 | #endif |
| 2038 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2039 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2040 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2041 | #ifndef OPENSSL_NO_DH |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 2042 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 2043 | 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] | 2044 | #else |
| 2045 | ssl_sock_set_tmp_dh_from_pkey(ssl_ctx, pkey); |
| 2046 | #endif |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2047 | #endif |
Remi Tricot-Le Breton | c11e7e1 | 2022-02-08 17:45:56 +0100 | [diff] [blame] | 2048 | |
| 2049 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 2050 | #if defined(SSL_CTX_set1_curves_list) |
| 2051 | { |
| 2052 | const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
| 2053 | if (!SSL_CTX_set1_curves_list(ssl_ctx, ecdhe)) |
| 2054 | goto end; |
| 2055 | } |
| 2056 | #endif |
| 2057 | #else |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2058 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2059 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2060 | 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] | 2061 | EC_KEY *ecc; |
| 2062 | int nid; |
| 2063 | |
| 2064 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2065 | goto end; |
| 2066 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2067 | goto end; |
| 2068 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2069 | EC_KEY_free(ecc); |
| 2070 | } |
Remi Tricot-Le Breton | c11e7e1 | 2022-02-08 17:45:56 +0100 | [diff] [blame] | 2071 | #endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */ |
| 2072 | #endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */ |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2073 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2074 | return ssl_ctx; |
| 2075 | |
| 2076 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2077 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2078 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2079 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2080 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2081 | return NULL; |
| 2082 | } |
| 2083 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2084 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2085 | /* 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] | 2086 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2087 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2088 | 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] | 2089 | { |
| 2090 | struct lru64 *lru = NULL; |
| 2091 | |
| 2092 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2093 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2094 | 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] | 2095 | if (lru && lru->domain) { |
| 2096 | if (ssl) |
| 2097 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2098 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2099 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2100 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2101 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2102 | } |
| 2103 | return NULL; |
| 2104 | } |
| 2105 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2106 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2107 | * function is not thread-safe, it should only be used to check if a certificate |
| 2108 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2109 | * thread). It is kept for backward compatibility. */ |
| 2110 | SSL_CTX * |
| 2111 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2112 | { |
| 2113 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2114 | } |
| 2115 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2116 | /* Set a certificate int the LRU cache used to store generated |
| 2117 | * certificate. Return 0 on success, otherwise -1 */ |
| 2118 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2119 | 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] | 2120 | { |
| 2121 | struct lru64 *lru = NULL; |
| 2122 | |
| 2123 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2124 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2125 | 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] | 2126 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2127 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2128 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2129 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2130 | if (lru->domain && lru->data) |
| 2131 | lru->free((SSL_CTX *)lru->data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2132 | 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] | 2133 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2134 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2135 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2136 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2137 | } |
| 2138 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2139 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2140 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2141 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2142 | { |
| 2143 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2144 | } |
| 2145 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2146 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2147 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2148 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2149 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2150 | 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] | 2151 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2152 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2153 | SSL_CTX *ssl_ctx = NULL; |
| 2154 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2155 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2156 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2157 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2158 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2159 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2160 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2161 | if (lru && lru->domain) |
| 2162 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2163 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2164 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2165 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2166 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2167 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2168 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2169 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2170 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2171 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2172 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2173 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2174 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2175 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2176 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2177 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2178 | return 0; |
| 2179 | } |
| 2180 | static int |
| 2181 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2182 | { |
| 2183 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2184 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2185 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2186 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2187 | 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] | 2188 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2189 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2190 | } |
| 2191 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2192 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2193 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2194 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2195 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2196 | |
| 2197 | 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] | 2198 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2199 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2200 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2201 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2202 | #endif |
| 2203 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2204 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2205 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2206 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2207 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2208 | 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] | 2209 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2210 | 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] | 2211 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2212 | #endif |
| 2213 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2214 | 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] | 2215 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2216 | 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] | 2217 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2218 | #endif |
| 2219 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2220 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2221 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2222 | /* Unusable in this context. */ |
| 2223 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2224 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2225 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2226 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2227 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2228 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2229 | |
| 2230 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2231 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2232 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2233 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2234 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2235 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2236 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2237 | } |
| 2238 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2239 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2240 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2241 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2242 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2243 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2244 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2245 | } |
| 2246 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2247 | 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] | 2248 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2249 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2250 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2251 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2252 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2253 | } |
| 2254 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2255 | 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] | 2256 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2257 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2258 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2259 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2260 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2261 | } |
| 2262 | 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] | 2263 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2264 | 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] | 2265 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2266 | #endif |
| 2267 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2268 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
William Lallemand | f22b032 | 2021-06-02 16:09:11 +0200 | [diff] [blame] | 2269 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2270 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2271 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2272 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2273 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2274 | #endif |
| 2275 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2276 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2277 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 2278 | struct methodVersions methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2279 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2280 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2281 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2282 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2283 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2284 | {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] | 2285 | }; |
| 2286 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2287 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2288 | { |
| 2289 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2290 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2291 | SSL_set_SSL_CTX(ssl, ctx); |
| 2292 | } |
| 2293 | |
Ilya Shipitsin | 1fc44d4 | 2021-01-23 00:09:14 +0500 | [diff] [blame] | 2294 | #ifdef HAVE_SSL_CLIENT_HELLO_CB |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2295 | |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2296 | int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2297 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2298 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2299 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2300 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2301 | 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] | 2302 | return SSL_TLSEXT_ERR_OK; |
| 2303 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2304 | } |
| 2305 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2306 | #ifdef OPENSSL_IS_BORINGSSL |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2307 | int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2308 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2309 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2310 | #else |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2311 | int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2312 | { |
| 2313 | #endif |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2314 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 2315 | #ifdef USE_QUIC |
| 2316 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 2317 | #endif /* USE_QUIC */ |
| 2318 | struct bind_conf *s = NULL; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2319 | const uint8_t *extension_data; |
| 2320 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2321 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2322 | |
| 2323 | char *wildp = NULL; |
| 2324 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2325 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2326 | 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] | 2327 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2328 | int i; |
| 2329 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2330 | if (conn) |
| 2331 | s = __objt_listener(conn->target)->bind_conf; |
| 2332 | #ifdef USE_QUIC |
| 2333 | else if (qc) |
| 2334 | s = qc->li->bind_conf; |
| 2335 | #endif /* USE_QUIC */ |
Amaury Denoyelle | 7c564bf | 2022-01-24 11:04:05 +0100 | [diff] [blame] | 2336 | |
| 2337 | if (!s) { |
| 2338 | /* must never happen */ |
| 2339 | ABORT_NOW(); |
| 2340 | return 0; |
| 2341 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2342 | |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2343 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2344 | if (qc) { |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2345 | /* Look for the QUIC transport parameters. */ |
| 2346 | #ifdef OPENSSL_IS_BORINGSSL |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2347 | 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] | 2348 | &extension_data, &extension_len)) |
| 2349 | #else |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2350 | 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] | 2351 | &extension_data, &extension_len)) |
| 2352 | #endif |
Frédéric Lécaille | b5b5247 | 2021-11-22 15:55:16 +0100 | [diff] [blame] | 2353 | { |
| 2354 | /* This is not redundant. It we only return 0 without setting |
| 2355 | * <*al>, this has as side effect to generate another TLS alert |
| 2356 | * which would be set after calling quic_set_tls_alert(). |
| 2357 | */ |
| 2358 | *al = SSL_AD_MISSING_EXTENSION; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2359 | quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION); |
Frédéric Lécaille | b5b5247 | 2021-11-22 15:55:16 +0100 | [diff] [blame] | 2360 | return 0; |
| 2361 | } |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2362 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2363 | if (!quic_transport_params_store(qc, 0, extension_data, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 2364 | extension_data + extension_len) || |
| 2365 | !qc_conn_finalize(qc, 0)) |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2366 | goto abort; |
| 2367 | } |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2368 | #endif /* USE_QUIC */ |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2369 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2370 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2371 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2372 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2373 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2374 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2375 | #else |
| 2376 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2377 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2378 | /* |
| 2379 | * The server_name extension was given too much extensibility when it |
| 2380 | * was written, so parsing the normal case is a bit complex. |
| 2381 | */ |
| 2382 | size_t len; |
| 2383 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2384 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2385 | /* Extract the length of the supplied list of names. */ |
| 2386 | len = (*extension_data++) << 8; |
| 2387 | len |= *extension_data++; |
| 2388 | if (len + 2 != extension_len) |
| 2389 | goto abort; |
| 2390 | /* |
| 2391 | * The list in practice only has a single element, so we only consider |
| 2392 | * the first one. |
| 2393 | */ |
| 2394 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2395 | goto abort; |
| 2396 | extension_len = len - 1; |
| 2397 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2398 | if (extension_len <= 2) |
| 2399 | goto abort; |
| 2400 | len = (*extension_data++) << 8; |
| 2401 | len |= *extension_data++; |
| 2402 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2403 | || memchr(extension_data, 0, len) != NULL) |
| 2404 | goto abort; |
| 2405 | servername = extension_data; |
| 2406 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2407 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2408 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2409 | 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] | 2410 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2411 | } |
| 2412 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2413 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2414 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2415 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2416 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2417 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2418 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2419 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2420 | goto abort; |
| 2421 | } |
| 2422 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2423 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2424 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2425 | 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] | 2426 | #else |
| 2427 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2428 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2429 | uint8_t sign; |
| 2430 | size_t len; |
| 2431 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2432 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2433 | len = (*extension_data++) << 8; |
| 2434 | len |= *extension_data++; |
| 2435 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2436 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2437 | if (len % 2 != 0) |
| 2438 | goto abort; |
| 2439 | for (; len > 0; len -= 2) { |
| 2440 | extension_data++; /* hash */ |
| 2441 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2442 | switch (sign) { |
| 2443 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2444 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2445 | break; |
| 2446 | case TLSEXT_signature_ecdsa: |
| 2447 | has_ecdsa_sig = 1; |
| 2448 | break; |
| 2449 | default: |
| 2450 | continue; |
| 2451 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2452 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2453 | break; |
| 2454 | } |
| 2455 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2456 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2457 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2458 | } |
| 2459 | 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] | 2460 | const SSL_CIPHER *cipher; |
| 2461 | size_t len; |
| 2462 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2463 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2464 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2465 | len = ctx->cipher_suites_len; |
| 2466 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2467 | #else |
| 2468 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2469 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2470 | if (len % 2 != 0) |
| 2471 | goto abort; |
| 2472 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2473 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2474 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2475 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2476 | #else |
| 2477 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2478 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2479 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2480 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2481 | break; |
| 2482 | } |
| 2483 | } |
| 2484 | } |
| 2485 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2486 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2487 | trash.area[i] = tolower(servername[i]); |
| 2488 | if (!wildp && (trash.area[i] == '.')) |
| 2489 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2490 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2491 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2492 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2493 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2494 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2495 | /* Look for an ECDSA, RSA and DSA certificate, first in the single |
| 2496 | * name and if not found in the wildcard */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2497 | for (i = 0; i < 2; i++) { |
| 2498 | if (i == 0) /* lookup in full qualified names */ |
| 2499 | node = ebst_lookup(&s->sni_ctx, trash.area); |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2500 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2501 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2502 | else |
| 2503 | break; |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2504 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2505 | for (n = node; n; n = ebmb_next_dup(n)) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2506 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2507 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2508 | if (!container_of(n, struct sni_ctx, name)->neg) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 2509 | struct sni_ctx *sni, *sni_tmp; |
| 2510 | int skip = 0; |
| 2511 | |
| 2512 | if (i == 1 && wildp) { /* wildcard */ |
| 2513 | /* If this is a wildcard, look for an exclusion on the same crt-list line */ |
| 2514 | sni = container_of(n, struct sni_ctx, name); |
| 2515 | 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] | 2516 | 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] | 2517 | skip = 1; |
| 2518 | break; |
| 2519 | } |
| 2520 | } |
| 2521 | if (skip) |
| 2522 | continue; |
| 2523 | } |
| 2524 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2525 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2526 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2527 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2528 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2529 | break; |
| 2530 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2531 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2532 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2533 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2534 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2535 | if (!node_anonymous) |
| 2536 | node_anonymous = n; |
| 2537 | break; |
| 2538 | } |
| 2539 | } |
| 2540 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2541 | } |
| 2542 | /* Once the certificates are found, select them depending on what is |
| 2543 | * supported in the client and by key_signature priority order: EDSA > |
| 2544 | * RSA > DSA */ |
William Lallemand | 5b1d1f6 | 2020-08-14 15:30:13 +0200 | [diff] [blame] | 2545 | if (has_ecdsa_sig && node_ecdsa) |
| 2546 | node = node_ecdsa; |
| 2547 | else if (has_rsa_sig && node_rsa) |
| 2548 | node = node_rsa; |
| 2549 | else if (node_anonymous) |
| 2550 | node = node_anonymous; |
| 2551 | else if (node_ecdsa) |
| 2552 | node = node_ecdsa; /* no ecdsa signature case (< TLSv1.2) */ |
| 2553 | else |
| 2554 | node = node_rsa; /* no rsa signature case (far far away) */ |
| 2555 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2556 | if (node) { |
| 2557 | /* switch ctx */ |
| 2558 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 2559 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 2560 | if (conf) { |
| 2561 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2562 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2563 | if (conf->early_data) |
| 2564 | allow_early = 1; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 2565 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 2566 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
| 2567 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2568 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2569 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2570 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2571 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2572 | 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] | 2573 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2574 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2575 | } |
| 2576 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2577 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2578 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2579 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2580 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2581 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2582 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2583 | } |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2584 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 2585 | /* We are about to raise an handshake error so the servername extension |
| 2586 | * callback will never be called and the SNI will never be stored in the |
| 2587 | * SSL context. In order for the ssl_fc_sni sample fetch to still work |
| 2588 | * in such a case, we store the SNI ourselves as an ex_data information |
| 2589 | * in the SSL context. |
| 2590 | */ |
| 2591 | { |
| 2592 | char *client_sni = pool_alloc(ssl_sock_client_sni_pool); |
| 2593 | if (client_sni) { |
| 2594 | strncpy(client_sni, trash.area, TLSEXT_MAXLEN_host_name); |
| 2595 | client_sni[TLSEXT_MAXLEN_host_name] = '\0'; |
| 2596 | SSL_set_ex_data(ssl, ssl_client_sni_index, client_sni); |
| 2597 | } |
| 2598 | } |
| 2599 | |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2600 | /* other cases fallback on abort, if strict-sni is set but no node was found */ |
| 2601 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2602 | abort: |
| 2603 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2604 | if (conn) |
| 2605 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2606 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2607 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2608 | #else |
| 2609 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2610 | return 0; |
| 2611 | #endif |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 2612 | |
| 2613 | allow_early: |
| 2614 | #ifdef OPENSSL_IS_BORINGSSL |
| 2615 | if (allow_early) |
| 2616 | SSL_set_early_data_enabled(ssl, 1); |
| 2617 | #else |
| 2618 | if (!allow_early) |
| 2619 | SSL_set_max_early_data(ssl, 0); |
| 2620 | #endif |
| 2621 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2622 | } |
| 2623 | |
William Lallemand | 002e206 | 2021-11-18 15:25:16 +0100 | [diff] [blame] | 2624 | #else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2625 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2626 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2627 | * warning when no match is found, which implies the default (first) cert |
| 2628 | * will keep being used. |
| 2629 | */ |
William Lallemand | 844009d | 2022-09-02 15:27:32 +0200 | [diff] [blame] | 2630 | int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2631 | { |
| 2632 | const char *servername; |
| 2633 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2634 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2635 | struct bind_conf *s = priv; |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2636 | #ifdef USE_QUIC |
| 2637 | const uint8_t *extension_data; |
| 2638 | size_t extension_len; |
| 2639 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 2640 | #endif /* USE_QUIC */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2641 | int i; |
| 2642 | (void)al; /* shut gcc stupid warning */ |
| 2643 | |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 2644 | #ifdef USE_QUIC |
| 2645 | if (qc) { |
| 2646 | |
| 2647 | /* Look for the QUIC transport parameters. */ |
| 2648 | SSL_get_peer_quic_transport_params(ssl, &extension_data, &extension_len); |
| 2649 | if (extension_len == 0) { |
| 2650 | /* This is not redundant. It we only return 0 without setting |
| 2651 | * <*al>, this has as side effect to generate another TLS alert |
| 2652 | * which would be set after calling quic_set_tls_alert(). |
| 2653 | */ |
| 2654 | *al = SSL_AD_MISSING_EXTENSION; |
| 2655 | quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION); |
| 2656 | return SSL_TLSEXT_ERR_NOACK; |
| 2657 | } |
| 2658 | |
| 2659 | if (!quic_transport_params_store(qc, 0, extension_data, |
| 2660 | extension_data + extension_len) || |
| 2661 | !qc_conn_finalize(qc, 0)) { |
| 2662 | return SSL_TLSEXT_ERR_NOACK; |
| 2663 | } |
| 2664 | } |
| 2665 | #endif /* USE_QUIC */ |
| 2666 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2667 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2668 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2669 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2670 | 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] | 2671 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2672 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2673 | if (s->strict_sni) |
| 2674 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2675 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2676 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2677 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2678 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2679 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2680 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2681 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2682 | if (!servername[i]) |
| 2683 | break; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 2684 | trash.area[i] = tolower((unsigned char)servername[i]); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2685 | if (!wildp && (trash.area[i] == '.')) |
| 2686 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2687 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2688 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2689 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2690 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2691 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2692 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2693 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 2694 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2695 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2696 | node = n; |
| 2697 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2698 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2699 | } |
| 2700 | if (!node && wildp) { |
| 2701 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2702 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 2703 | /* lookup a not neg filter */ |
| 2704 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2705 | node = n; |
| 2706 | break; |
| 2707 | } |
| 2708 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2709 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 2710 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2711 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2712 | 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] | 2713 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2714 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2715 | return SSL_TLSEXT_ERR_OK; |
| 2716 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2717 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2718 | if (s->strict_sni) { |
| 2719 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2720 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2721 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2722 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2723 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2724 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2728 | 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] | 2729 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2730 | return SSL_TLSEXT_ERR_OK; |
| 2731 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2732 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2733 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2734 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2735 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2736 | |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2737 | static inline HASSL_DH *ssl_new_dh_fromdata(BIGNUM *p, BIGNUM *g) |
| 2738 | { |
| 2739 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 2740 | OSSL_PARAM_BLD *tmpl = NULL; |
| 2741 | OSSL_PARAM *params = NULL; |
| 2742 | EVP_PKEY_CTX *ctx = NULL; |
| 2743 | EVP_PKEY *pkey = NULL; |
| 2744 | |
| 2745 | if ((tmpl = OSSL_PARAM_BLD_new()) == NULL |
| 2746 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
| 2747 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g) |
| 2748 | || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
| 2749 | goto end; |
| 2750 | } |
| 2751 | ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 2752 | if (ctx == NULL |
| 2753 | || !EVP_PKEY_fromdata_init(ctx) |
| 2754 | || !EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params)) { |
| 2755 | goto end; |
| 2756 | } |
| 2757 | |
| 2758 | end: |
| 2759 | EVP_PKEY_CTX_free(ctx); |
| 2760 | OSSL_PARAM_free(params); |
| 2761 | OSSL_PARAM_BLD_free(tmpl); |
Remi Tricot-Le Breton | a2c21db | 2022-11-03 15:16:47 +0100 | [diff] [blame] | 2762 | BN_free(p); |
| 2763 | BN_free(g); |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2764 | return pkey; |
| 2765 | #else |
| 2766 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2767 | HASSL_DH *dh = DH_new(); |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2768 | |
| 2769 | if (!dh) |
| 2770 | return NULL; |
| 2771 | |
| 2772 | DH_set0_pqg(dh, p, NULL, g); |
| 2773 | |
| 2774 | return dh; |
| 2775 | #endif |
| 2776 | } |
| 2777 | |
Remi Tricot-Le Breton | c69be7c | 2022-04-20 18:30:17 +0200 | [diff] [blame] | 2778 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2779 | static inline HASSL_DH *ssl_get_dh_by_nid(int nid) |
| 2780 | { |
| 2781 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 2782 | OSSL_PARAM params[2]; |
| 2783 | EVP_PKEY *pkey = NULL; |
| 2784 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 2785 | const char *named_group = NULL; |
| 2786 | |
| 2787 | if (!pctx) |
| 2788 | goto end; |
| 2789 | |
| 2790 | named_group = OBJ_nid2ln(nid); |
| 2791 | |
| 2792 | if (!named_group) |
| 2793 | goto end; |
| 2794 | |
| 2795 | params[0] = OSSL_PARAM_construct_utf8_string("group", (char*)named_group, 0); |
| 2796 | params[1] = OSSL_PARAM_construct_end(); |
| 2797 | |
| 2798 | if (EVP_PKEY_keygen_init(pctx) && EVP_PKEY_CTX_set_params(pctx, params)) |
| 2799 | EVP_PKEY_generate(pctx, &pkey); |
| 2800 | |
| 2801 | end: |
| 2802 | EVP_PKEY_CTX_free(pctx); |
| 2803 | return pkey; |
| 2804 | #else |
| 2805 | |
| 2806 | HASSL_DH *dh = NULL; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2807 | dh = DH_new_by_nid(nid); |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2808 | return dh; |
| 2809 | #endif |
| 2810 | } |
Remi Tricot-Le Breton | c69be7c | 2022-04-20 18:30:17 +0200 | [diff] [blame] | 2811 | #endif |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2812 | |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 2813 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2814 | static HASSL_DH * ssl_get_dh_1024(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2815 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2816 | static unsigned char dh1024_p[]={ |
| 2817 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2818 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2819 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2820 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2821 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2822 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2823 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2824 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2825 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2826 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2827 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2828 | }; |
| 2829 | static unsigned char dh1024_g[]={ |
| 2830 | 0x02, |
| 2831 | }; |
| 2832 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2833 | BIGNUM *p; |
| 2834 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2835 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2836 | HASSL_DH *dh = NULL; |
| 2837 | |
| 2838 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2839 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
| 2840 | |
| 2841 | if (p && g) |
| 2842 | dh = ssl_new_dh_fromdata(p, g); |
| 2843 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2844 | return dh; |
| 2845 | } |
| 2846 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2847 | static HASSL_DH *ssl_get_dh_2048(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2848 | { |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2849 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2850 | static unsigned char dh2048_p[]={ |
| 2851 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2852 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2853 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2854 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2855 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2856 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2857 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2858 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2859 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2860 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2861 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2862 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2863 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2864 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2865 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2866 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2867 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2868 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2869 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2870 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2871 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2872 | 0xB7,0x1F,0x77,0xF3, |
| 2873 | }; |
| 2874 | static unsigned char dh2048_g[]={ |
| 2875 | 0x02, |
| 2876 | }; |
| 2877 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2878 | BIGNUM *p; |
| 2879 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2880 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2881 | HASSL_DH *dh = NULL; |
| 2882 | |
| 2883 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2884 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
| 2885 | |
| 2886 | if (p && g) |
| 2887 | dh = ssl_new_dh_fromdata(p, g); |
| 2888 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2889 | return dh; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2890 | #else |
| 2891 | return ssl_get_dh_by_nid(NID_ffdhe2048); |
| 2892 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2893 | } |
| 2894 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2895 | static HASSL_DH *ssl_get_dh_4096(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2896 | { |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2897 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2898 | static unsigned char dh4096_p[]={ |
| 2899 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2900 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2901 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2902 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2903 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2904 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2905 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2906 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2907 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2908 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2909 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2910 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2911 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2912 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2913 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2914 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2915 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2916 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2917 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2918 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2919 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2920 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2921 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2922 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2923 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2924 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2925 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2926 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2927 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2928 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2929 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2930 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2931 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2932 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2933 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2934 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2935 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2936 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2937 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2938 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2939 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2940 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2941 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2942 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2943 | static unsigned char dh4096_g[]={ |
| 2944 | 0x02, |
| 2945 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2946 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2947 | BIGNUM *p; |
| 2948 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2949 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2950 | HASSL_DH *dh = NULL; |
| 2951 | |
| 2952 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2953 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
| 2954 | |
| 2955 | if (p && g) |
| 2956 | dh = ssl_new_dh_fromdata(p, g); |
| 2957 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2958 | return dh; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 2959 | #else |
| 2960 | return ssl_get_dh_by_nid(NID_ffdhe4096); |
| 2961 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2962 | } |
| 2963 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2964 | static HASSL_DH *ssl_get_tmp_dh(EVP_PKEY *pkey) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2965 | { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 2966 | HASSL_DH *dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2967 | int type; |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 2968 | int keylen = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2969 | |
| 2970 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2971 | |
Ilya Shipitsin | 52f2ff5 | 2022-07-23 23:55:19 +0500 | [diff] [blame] | 2972 | if (type == EVP_PKEY_EC) { |
| 2973 | keylen = global_ssl.default_dh_param; |
| 2974 | } |
| 2975 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2976 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2977 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2978 | */ |
| 2979 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2980 | keylen = EVP_PKEY_bits(pkey); |
| 2981 | } |
| 2982 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2983 | if (keylen > global_ssl.default_dh_param) { |
| 2984 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2985 | } |
| 2986 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2987 | if (keylen >= 4096) { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 2988 | if (!local_dh_4096) |
| 2989 | local_dh_4096 = ssl_get_dh_4096(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2990 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2991 | } |
| 2992 | else if (keylen >= 2048) { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 2993 | if (!local_dh_2048) |
| 2994 | local_dh_2048 = ssl_get_dh_2048(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2995 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2996 | } |
| 2997 | else { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 2998 | if (!local_dh_1024) |
| 2999 | local_dh_1024 = ssl_get_dh_1024(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3000 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | return dh; |
| 3004 | } |
| 3005 | |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3006 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3007 | /* Returns Diffie-Hellman parameters matching the private key length |
| 3008 | but not exceeding global_ssl.default_dh_param */ |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3009 | 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] | 3010 | { |
| 3011 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
| 3012 | |
| 3013 | return ssl_get_tmp_dh(pkey); |
| 3014 | } |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3015 | #endif |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3016 | |
Remi Tricot-Le Breton | 846eda9 | 2022-02-11 12:04:50 +0100 | [diff] [blame] | 3017 | static int ssl_sock_set_tmp_dh(SSL_CTX *ctx, HASSL_DH *dh) |
| 3018 | { |
| 3019 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
| 3020 | return SSL_CTX_set_tmp_dh(ctx, dh); |
| 3021 | #else |
| 3022 | int retval = 0; |
| 3023 | HASSL_DH_up_ref(dh); |
| 3024 | |
| 3025 | retval = SSL_CTX_set0_tmp_dh_pkey(ctx, dh); |
| 3026 | |
| 3027 | if (!retval) |
| 3028 | HASSL_DH_free(dh); |
| 3029 | |
| 3030 | return retval; |
| 3031 | #endif |
| 3032 | } |
| 3033 | |
Remi Tricot-Le Breton | 5f17930 | 2022-02-11 12:04:51 +0100 | [diff] [blame] | 3034 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3035 | static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey) |
| 3036 | { |
| 3037 | HASSL_DH *dh = NULL; |
| 3038 | if (pkey && (dh = ssl_get_tmp_dh(pkey))) { |
| 3039 | HASSL_DH_up_ref(dh); |
| 3040 | if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dh)) |
| 3041 | HASSL_DH_free(dh); |
| 3042 | } |
Remi Tricot-Le Breton | 5f17930 | 2022-02-11 12:04:51 +0100 | [diff] [blame] | 3043 | } |
| 3044 | #endif |
| 3045 | |
Remi Tricot-Le Breton | 09ebb33 | 2022-02-11 12:04:48 +0100 | [diff] [blame] | 3046 | HASSL_DH *ssl_sock_get_dh_from_bio(BIO *bio) |
| 3047 | { |
| 3048 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3049 | HASSL_DH *dh = NULL; |
| 3050 | OSSL_DECODER_CTX *dctx = NULL; |
| 3051 | const char *format = "PEM"; |
| 3052 | const char *keytype = "DH"; |
| 3053 | |
| 3054 | dctx = OSSL_DECODER_CTX_new_for_pkey(&dh, format, NULL, keytype, |
| 3055 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
| 3056 | NULL, NULL); |
| 3057 | |
| 3058 | if (dctx == NULL || OSSL_DECODER_CTX_get_num_decoders(dctx) == 0) |
| 3059 | goto end; |
| 3060 | |
| 3061 | /* The DH parameters might not be the first section found in the PEM |
| 3062 | * file so we need to iterate over all of them until we find the right |
| 3063 | * one. |
| 3064 | */ |
| 3065 | while (!BIO_eof(bio) && !dh) |
| 3066 | OSSL_DECODER_from_bio(dctx, bio); |
| 3067 | |
| 3068 | end: |
| 3069 | OSSL_DECODER_CTX_free(dctx); |
| 3070 | return dh; |
| 3071 | #else |
| 3072 | HASSL_DH *dh = NULL; |
| 3073 | |
| 3074 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 3075 | |
| 3076 | return dh; |
| 3077 | #endif |
| 3078 | } |
| 3079 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3080 | static HASSL_DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3081 | { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3082 | HASSL_DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3083 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3084 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3085 | if (in == NULL) |
| 3086 | goto end; |
| 3087 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3088 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3089 | goto end; |
| 3090 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3091 | dh = ssl_sock_get_dh_from_bio(in); |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3092 | |
| 3093 | end: |
| 3094 | if (in) |
| 3095 | BIO_free(in); |
| 3096 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 3097 | ERR_clear_error(); |
| 3098 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3099 | return dh; |
| 3100 | } |
| 3101 | |
| 3102 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 3103 | { |
| 3104 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 3105 | |
| 3106 | if (global_dh) { |
| 3107 | return 0; |
| 3108 | } |
| 3109 | |
| 3110 | return -1; |
| 3111 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3112 | #endif |
| 3113 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3114 | /* 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] | 3115 | 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] | 3116 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 3117 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3118 | { |
| 3119 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3120 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3121 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3122 | if (*name == '!') { |
| 3123 | neg = 1; |
| 3124 | name++; |
| 3125 | } |
| 3126 | if (*name == '*') { |
| 3127 | wild = 1; |
| 3128 | name++; |
| 3129 | } |
| 3130 | /* !* filter is a nop */ |
| 3131 | if (neg && wild) |
| 3132 | return order; |
| 3133 | if (*name) { |
| 3134 | int j, len; |
| 3135 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3136 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 3137 | trash.area[j] = tolower((unsigned char)name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3138 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3139 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3140 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3141 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3142 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 3143 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3144 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3145 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3146 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3147 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3148 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3149 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3150 | sc->order = order++; |
| 3151 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3152 | sc->wild = wild; |
| 3153 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3154 | sc->ckch_inst = ckch_inst; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3155 | LIST_APPEND(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3156 | } |
| 3157 | return order; |
| 3158 | } |
| 3159 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3160 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3161 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 3162 | * This function can't return an error. |
| 3163 | * |
| 3164 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 3165 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3166 | 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] | 3167 | { |
| 3168 | |
| 3169 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 3170 | struct ebmb_node *node; |
| 3171 | |
| 3172 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 3173 | |
| 3174 | /* ignore if sc0 was already inserted in a tree */ |
| 3175 | if (sc0->name.node.leaf_p) |
| 3176 | continue; |
| 3177 | |
| 3178 | /* Check for duplicates. */ |
| 3179 | if (sc0->wild) |
| 3180 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 3181 | else |
| 3182 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 3183 | |
| 3184 | for (; node; node = ebmb_next_dup(node)) { |
| 3185 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 3186 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 3187 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 3188 | /* it's a duplicate, we should remove and free it */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3189 | LIST_DELETE(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3190 | SSL_CTX_free(sc0->ctx); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3191 | ha_free(&sc0); |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 3192 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | /* if duplicate, ignore the insertion */ |
| 3197 | if (!sc0) |
| 3198 | continue; |
| 3199 | |
| 3200 | if (sc0->wild) |
| 3201 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 3202 | else |
| 3203 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3204 | } |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3205 | |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3206 | /* replace the default_ctx if required with the instance's ctx. */ |
| 3207 | if (ckch_inst->is_default) { |
| 3208 | SSL_CTX_free(bind_conf->default_ctx); |
| 3209 | SSL_CTX_up_ref(ckch_inst->ctx); |
| 3210 | bind_conf->default_ctx = ckch_inst->ctx; |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 3211 | bind_conf->default_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3212 | } |
| 3213 | } |
| 3214 | |
| 3215 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3216 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3217 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3218 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3219 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3220 | /* tree of crtlist (crt-list/directory) */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3221 | struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3222 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3223 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 3224 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3225 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 3226 | * DH parameter available in ckchs nor in global, the default |
| 3227 | * DH parameters are applied on the SSL_CTX. |
| 3228 | * Returns a bitfield containing the flags: |
| 3229 | * ERR_FATAL in any fatal error case |
| 3230 | * ERR_ALERT if a reason of the error is availabine in err |
| 3231 | * ERR_WARN if a warning is available into err |
| 3232 | * The value 0 means there is no error nor warning and |
| 3233 | * the operation succeed. |
| 3234 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3235 | #ifndef OPENSSL_NO_DH |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3236 | 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] | 3237 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3238 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3239 | int ret = 0; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3240 | HASSL_DH *dh = NULL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3241 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3242 | if (data && data->dh) { |
| 3243 | dh = data->dh; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3244 | if (!ssl_sock_set_tmp_dh(ctx, dh)) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3245 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 3246 | err && *err ? *err : "", path); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3247 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3248 | err && *err ? *err : ""); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3249 | ret |= ERR_WARN; |
| 3250 | goto end; |
| 3251 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3252 | |
| 3253 | if (ssl_dh_ptr_index >= 0) { |
| 3254 | /* store a pointer to the DH params to avoid complaining about |
| 3255 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 3256 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 3257 | } |
| 3258 | } |
| 3259 | else if (global_dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3260 | if (!ssl_sock_set_tmp_dh(ctx, global_dh)) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3261 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 3262 | err && *err ? *err : "", path); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3263 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3264 | err && *err ? *err : ""); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3265 | ret |= ERR_WARN; |
| 3266 | goto end; |
| 3267 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3268 | } |
| 3269 | else { |
| 3270 | /* Clear openssl global errors stack */ |
| 3271 | ERR_clear_error(); |
| 3272 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3273 | /* We do not want DHE ciphers to be added to the cipher list |
| 3274 | * unless there is an explicit global dh option in the conf. |
| 3275 | */ |
| 3276 | if (global_ssl.default_dh_param) { |
| 3277 | if (global_ssl.default_dh_param <= 1024) { |
| 3278 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3279 | if (local_dh_1024 == NULL) |
| 3280 | local_dh_1024 = ssl_get_dh_1024(); |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3281 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3282 | if (local_dh_1024 == NULL) { |
| 3283 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3284 | err && *err ? *err : "", path); |
| 3285 | ret |= ERR_ALERT | ERR_FATAL; |
| 3286 | goto end; |
| 3287 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3288 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3289 | if (!ssl_sock_set_tmp_dh(ctx, local_dh_1024)) { |
| 3290 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3291 | err && *err ? *err : "", path); |
| 3292 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3293 | err && *err ? *err : ""); |
| 3294 | ret |= ERR_WARN; |
| 3295 | goto end; |
| 3296 | } |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3297 | } |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3298 | else { |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3299 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3300 | 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] | 3301 | #else |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3302 | 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] | 3303 | #endif |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3304 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3305 | } |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3306 | } |
| 3307 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3308 | end: |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3309 | ERR_clear_error(); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3310 | return ret; |
| 3311 | } |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3312 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3313 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3314 | |
| 3315 | /* Load a certificate chain into an SSL context. |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3316 | * Returns a bitfield containing the flags: |
| 3317 | * ERR_FATAL in any fatal error case |
| 3318 | * ERR_ALERT if the reason of the error is available in err |
| 3319 | * ERR_WARN if a warning is available into err |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3320 | * The caller is responsible of freeing the newly built or newly refcounted |
| 3321 | * find_chain element. |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3322 | * The value 0 means there is no error nor warning and |
| 3323 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3324 | */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3325 | 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] | 3326 | SSL_CTX *ctx, STACK_OF(X509) **find_chain, char **err) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3327 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3328 | int errcode = 0; |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3329 | int ret; |
| 3330 | |
| 3331 | ERR_clear_error(); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3332 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3333 | if (find_chain == NULL) { |
| 3334 | errcode |= ERR_FATAL; |
| 3335 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3336 | } |
| 3337 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3338 | if (!SSL_CTX_use_certificate(ctx, data->cert)) { |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3339 | ret = ERR_get_error(); |
| 3340 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s': %s.\n", |
| 3341 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3342 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3343 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3344 | } |
| 3345 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3346 | if (data->chain) { |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3347 | *find_chain = X509_chain_up_ref(data->chain); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3348 | } else { |
| 3349 | /* Find Certificate Chain in global */ |
| 3350 | struct issuer_chain *issuer; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3351 | issuer = ssl_get0_issuer_chain(data->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3352 | if (issuer) |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3353 | *find_chain = X509_chain_up_ref(issuer->chain); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3354 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3355 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3356 | if (!*find_chain) { |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3357 | /* always put a null chain stack in the SSL_CTX so it does not |
| 3358 | * try to build the chain from the verify store */ |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3359 | *find_chain = sk_X509_new_null(); |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3360 | } |
| 3361 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3362 | /* 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] | 3363 | #ifdef SSL_CTX_set1_chain |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3364 | if (!SSL_CTX_set1_chain(ctx, *find_chain)) { |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3365 | ret = ERR_get_error(); |
| 3366 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s': %s. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3367 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3368 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3369 | goto end; |
| 3370 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3371 | #else |
| 3372 | { /* legacy compat (< openssl 1.0.2) */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3373 | X509 *ca; |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3374 | while ((ca = sk_X509_shift(*find_chain))) |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3375 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3376 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3377 | err && *err ? *err : "", path); |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3378 | X509_free(ca); |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3379 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3380 | goto end; |
| 3381 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3382 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3383 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3384 | |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3385 | #ifdef SSL_CTX_build_cert_chain |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3386 | /* remove the Root CA from the SSL_CTX if the option is activated */ |
| 3387 | if (global_ssl.skip_self_issued_ca) { |
| 3388 | if (!SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT|SSL_BUILD_CHAIN_FLAG_UNTRUSTED|SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)) { |
| 3389 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3390 | err && *err ? *err : "", path); |
| 3391 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3392 | goto end; |
| 3393 | } |
| 3394 | } |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3395 | #endif |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3396 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3397 | end: |
| 3398 | return errcode; |
| 3399 | } |
| 3400 | |
| 3401 | |
| 3402 | /* Loads the info in ckch into ctx |
| 3403 | * Returns a bitfield containing the flags: |
| 3404 | * ERR_FATAL in any fatal error case |
| 3405 | * ERR_ALERT if the reason of the error is available in err |
| 3406 | * ERR_WARN if a warning is available into err |
| 3407 | * The value 0 means there is no error nor warning and |
| 3408 | * the operation succeed. |
| 3409 | */ |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 3410 | 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] | 3411 | { |
| 3412 | int errcode = 0; |
| 3413 | STACK_OF(X509) *find_chain = NULL; |
| 3414 | |
William Lallemand | 5de4951 | 2022-10-27 14:41:07 +0200 | [diff] [blame] | 3415 | ERR_clear_error(); |
| 3416 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3417 | if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) { |
William Lallemand | 5de4951 | 2022-10-27 14:41:07 +0200 | [diff] [blame] | 3418 | int ret; |
| 3419 | |
| 3420 | ret = ERR_get_error(); |
| 3421 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s': %s.\n", |
| 3422 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3423 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3424 | return errcode; |
| 3425 | } |
| 3426 | |
| 3427 | /* Load certificate chain */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3428 | 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] | 3429 | if (errcode & ERR_CODE) |
| 3430 | goto end; |
| 3431 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3432 | #ifndef OPENSSL_NO_DH |
| 3433 | /* store a NULL pointer to indicate we have not yet loaded |
| 3434 | a custom DH param file */ |
| 3435 | if (ssl_dh_ptr_index >= 0) { |
| 3436 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3437 | } |
| 3438 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3439 | errcode |= ssl_sock_load_dh_params(ctx, data, path, err); |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3440 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3441 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3442 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3443 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3444 | } |
| 3445 | #endif |
| 3446 | |
Ilya Shipitsin | 7bbf586 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 3447 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3448 | if (sctl_ex_index >= 0 && data->sctl) { |
| 3449 | if (ssl_sock_load_sctl(ctx, data->sctl) < 0) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3450 | 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] | 3451 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3452 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3453 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3454 | } |
| 3455 | } |
| 3456 | #endif |
| 3457 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 3458 | #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] | 3459 | /* Load OCSP Info into context |
| 3460 | * If OCSP update mode is set to 'on', an entry will be created in the |
| 3461 | * ocsp tree even if no ocsp_response was known during init, unless the |
| 3462 | * frontend's conf disables ocsp update explicitely. |
| 3463 | */ |
| 3464 | if (ssl_sock_load_ocsp(ctx, data, find_chain) < 0) { |
| 3465 | if (data->ocsp_response) |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 3466 | 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] | 3467 | err && *err ? *err : "", path); |
| 3468 | else |
| 3469 | 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", |
| 3470 | err && *err ? *err : "", path); |
| 3471 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3472 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3473 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3474 | #endif |
| 3475 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3476 | end: |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3477 | sk_X509_pop_free(find_chain, X509_free); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3478 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3479 | } |
| 3480 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3481 | |
| 3482 | /* Loads the info of a ckch built out of a backend certificate into an SSL ctx |
| 3483 | * Returns a bitfield containing the flags: |
| 3484 | * ERR_FATAL in any fatal error case |
| 3485 | * ERR_ALERT if the reason of the error is available in err |
| 3486 | * ERR_WARN if a warning is available into err |
| 3487 | * The value 0 means there is no error nor warning and |
| 3488 | * the operation succeed. |
| 3489 | */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3490 | 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] | 3491 | SSL_CTX *ctx, char **err) |
| 3492 | { |
| 3493 | int errcode = 0; |
| 3494 | STACK_OF(X509) *find_chain = NULL; |
| 3495 | |
| 3496 | /* Load the private key */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3497 | if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) { |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3498 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3499 | err && *err ? *err : "", path); |
| 3500 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3501 | } |
| 3502 | |
| 3503 | /* Load certificate chain */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3504 | 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] | 3505 | if (errcode & ERR_CODE) |
| 3506 | goto end; |
| 3507 | |
| 3508 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3509 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3510 | err && *err ? *err : "", path); |
| 3511 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3512 | } |
| 3513 | |
| 3514 | end: |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3515 | sk_X509_pop_free(find_chain, X509_free); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3516 | return errcode; |
| 3517 | } |
| 3518 | |
| 3519 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3520 | /* |
| 3521 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3522 | * |
| 3523 | * Returns a bitfield containing the flags: |
| 3524 | * ERR_FATAL in any fatal error case |
| 3525 | * ERR_ALERT if the reason of the error is available in err |
| 3526 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3527 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3528 | 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] | 3529 | 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] | 3530 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3531 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3532 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3533 | int order = 0; |
| 3534 | X509_NAME *xname; |
| 3535 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3536 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3537 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3538 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3539 | STACK_OF(GENERAL_NAME) *names; |
| 3540 | #endif |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3541 | struct ckch_data *data; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3542 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3543 | int errcode = 0; |
| 3544 | |
| 3545 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3546 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3547 | if (!ckchs || !ckchs->data) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3548 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3549 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3550 | data = ckchs->data; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3551 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3552 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3553 | if (!ctx) { |
| 3554 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3555 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3556 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3557 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3558 | } |
| 3559 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3560 | errcode |= ssl_sock_put_ckch_into_ctx(path, data, ctx, err); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3561 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3562 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3563 | |
| 3564 | ckch_inst = ckch_inst_new(); |
| 3565 | if (!ckch_inst) { |
| 3566 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3567 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3568 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3569 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3570 | } |
| 3571 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3572 | pkey = X509_get_pubkey(data->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3573 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3574 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3575 | switch(EVP_PKEY_base_id(pkey)) { |
| 3576 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3577 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3578 | break; |
| 3579 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3580 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3581 | break; |
| 3582 | case EVP_PKEY_DSA: |
| 3583 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3584 | break; |
| 3585 | } |
| 3586 | EVP_PKEY_free(pkey); |
| 3587 | } |
| 3588 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3589 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3590 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3591 | 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] | 3592 | if (order < 0) { |
| 3593 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
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 | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3596 | } |
| 3597 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3598 | } |
| 3599 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3600 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3601 | 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] | 3602 | if (names) { |
| 3603 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3604 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3605 | if (name->type == GEN_DNS) { |
| 3606 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3607 | 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] | 3608 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3609 | if (order < 0) { |
| 3610 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3611 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3612 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3613 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3614 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3615 | } |
| 3616 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3617 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3618 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3619 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3620 | xname = X509_get_subject_name(data->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3621 | i = -1; |
| 3622 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3623 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3624 | ASN1_STRING *value; |
| 3625 | |
| 3626 | value = X509_NAME_ENTRY_get_data(entry); |
| 3627 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3628 | 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] | 3629 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3630 | if (order < 0) { |
| 3631 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3632 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3633 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3634 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3635 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3636 | } |
| 3637 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3638 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3639 | * the tree, so it will be discovered and cleaned in time. |
| 3640 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3641 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3642 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3643 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3644 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3645 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3646 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3647 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3648 | } |
| 3649 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3650 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3651 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3652 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3653 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3654 | SSL_CTX_up_ref(ctx); |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 3655 | bind_conf->default_inst = ckch_inst; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3656 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3657 | |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3658 | /* Always keep a reference to the newly constructed SSL_CTX in the |
| 3659 | * instance. This way if the instance has no SNIs, the SSL_CTX will |
| 3660 | * still be linked. */ |
| 3661 | SSL_CTX_up_ref(ctx); |
| 3662 | ckch_inst->ctx = ctx; |
| 3663 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3664 | /* everything succeed, the ckch instance can be used */ |
| 3665 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3666 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3667 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3668 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3669 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 3670 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3671 | *ckchi = ckch_inst; |
| 3672 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3673 | |
| 3674 | error: |
| 3675 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3676 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3677 | if (ckch_inst->is_default) |
| 3678 | SSL_CTX_free(ctx); |
| 3679 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3680 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3681 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3682 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3683 | SSL_CTX_free(ctx); |
| 3684 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3685 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3686 | } |
| 3687 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3688 | |
| 3689 | /* |
| 3690 | * This function allocate a ckch_inst that will be used on the backend side |
| 3691 | * (server line) |
| 3692 | * |
| 3693 | * Returns a bitfield containing the flags: |
| 3694 | * ERR_FATAL in any fatal error case |
| 3695 | * ERR_ALERT if the reason of the error is available in err |
| 3696 | * ERR_WARN if a warning is available into err |
| 3697 | */ |
| 3698 | 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] | 3699 | struct ckch_inst **ckchi, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3700 | { |
| 3701 | SSL_CTX *ctx; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3702 | struct ckch_data *data; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3703 | struct ckch_inst *ckch_inst = NULL; |
| 3704 | int errcode = 0; |
| 3705 | |
| 3706 | *ckchi = NULL; |
| 3707 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3708 | if (!ckchs || !ckchs->data) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3709 | return ERR_FATAL; |
| 3710 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3711 | data = ckchs->data; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3712 | |
| 3713 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 3714 | if (!ctx) { |
| 3715 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3716 | err && *err ? *err : "", path); |
| 3717 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3718 | goto error; |
| 3719 | } |
| 3720 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3721 | 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] | 3722 | if (errcode & ERR_CODE) |
| 3723 | goto error; |
| 3724 | |
| 3725 | ckch_inst = ckch_inst_new(); |
| 3726 | if (!ckch_inst) { |
| 3727 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3728 | err && *err ? *err : "", path); |
| 3729 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3730 | goto error; |
| 3731 | } |
| 3732 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3733 | /* everything succeed, the ckch instance can be used */ |
| 3734 | ckch_inst->bind_conf = NULL; |
| 3735 | ckch_inst->ssl_conf = NULL; |
| 3736 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 3737 | ckch_inst->ctx = ctx; |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3738 | ckch_inst->is_server_instance = 1; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3739 | |
| 3740 | *ckchi = ckch_inst; |
| 3741 | return errcode; |
| 3742 | |
| 3743 | error: |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3744 | SSL_CTX_free(ctx); |
| 3745 | |
| 3746 | return errcode; |
| 3747 | } |
| 3748 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 3749 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3750 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 3751 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3752 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3753 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3754 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3755 | |
| 3756 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | e7eb1fe | 2020-09-16 16:17:51 +0200 | [diff] [blame] | 3757 | 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] | 3758 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3759 | if (errcode & ERR_CODE) |
| 3760 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3761 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3762 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3763 | |
| 3764 | /* succeed, add the instance to the ckch_store's list of instance */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3765 | LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3766 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3767 | } |
| 3768 | |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3769 | /* This function generates a <struct ckch_inst *> for a <struct server *>, and |
| 3770 | * fill the SSL_CTX of the server. |
| 3771 | * |
| 3772 | * 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] | 3773 | 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] | 3774 | struct server *server, struct ckch_inst **ckch_inst, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3775 | { |
| 3776 | int errcode = 0; |
| 3777 | |
| 3778 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 3779 | 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] | 3780 | |
| 3781 | if (errcode & ERR_CODE) |
| 3782 | return errcode; |
| 3783 | |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3784 | (*ckch_inst)->server = server; |
| 3785 | /* Keep the reference to the SSL_CTX in the server. */ |
| 3786 | SSL_CTX_up_ref((*ckch_inst)->ctx); |
| 3787 | server->ssl_ctx.ctx = (*ckch_inst)->ctx; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3788 | /* succeed, add the instance to the ckch_store's list of instance */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3789 | LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3790 | return errcode; |
| 3791 | } |
| 3792 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3793 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3794 | |
| 3795 | |
| 3796 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3797 | * done once. Zero is returned if the operation fails. No error is returned |
| 3798 | * if the random is said as not implemented, because we expect that openssl |
| 3799 | * will use another method once needed. |
| 3800 | */ |
Amaury Denoyelle | c593bcd | 2021-05-19 15:35:29 +0200 | [diff] [blame] | 3801 | int ssl_initialize_random(void) |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 3802 | { |
| 3803 | unsigned char random; |
| 3804 | static int random_initialized = 0; |
| 3805 | |
| 3806 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3807 | random_initialized = 1; |
| 3808 | |
| 3809 | return random_initialized; |
| 3810 | } |
| 3811 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3812 | /* Load a crt-list file, this is done in 2 parts: |
| 3813 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 3814 | * - generate the instances by iterating on entries in the crtlist struct |
| 3815 | * |
| 3816 | * Nothing is locked there, this function is used in the configuration parser. |
| 3817 | * |
| 3818 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3819 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3820 | 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] | 3821 | { |
| 3822 | struct crtlist *crtlist = NULL; |
| 3823 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3824 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3825 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3826 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3827 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3828 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3829 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 3830 | if (!bind_conf_node) { |
| 3831 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 3832 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3833 | goto error; |
| 3834 | } |
| 3835 | bind_conf_node->next = NULL; |
| 3836 | bind_conf_node->bind_conf = bind_conf; |
| 3837 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 3838 | /* strip trailing slashes, including first one */ |
| 3839 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 3840 | *end = 0; |
| 3841 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3842 | /* look for an existing crtlist or create one */ |
| 3843 | eb = ebst_lookup(&crtlists_tree, file); |
| 3844 | if (eb) { |
| 3845 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 3846 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 3847 | /* load a crt-list OR a directory */ |
| 3848 | if (dir) |
| 3849 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 3850 | else |
| 3851 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 3852 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3853 | if (!(cfgerr & ERR_CODE)) |
| 3854 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 3855 | } |
| 3856 | |
| 3857 | if (cfgerr & ERR_CODE) { |
| 3858 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 3859 | goto error; |
| 3860 | } |
| 3861 | |
| 3862 | /* generates ckch instance from the crtlist_entry */ |
| 3863 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3864 | struct ckch_store *store; |
| 3865 | struct ckch_inst *ckch_inst = NULL; |
| 3866 | |
| 3867 | store = entry->node.key; |
| 3868 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 3869 | if (cfgerr & ERR_CODE) { |
| 3870 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 3871 | goto error; |
| 3872 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3873 | LIST_APPEND(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 3874 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3875 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3876 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3877 | /* add the bind_conf to the list */ |
| 3878 | bind_conf_node->next = crtlist->bind_conf; |
| 3879 | crtlist->bind_conf = bind_conf_node; |
| 3880 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3881 | return cfgerr; |
| 3882 | error: |
| 3883 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3884 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3885 | struct ckch_inst *inst, *s_inst; |
| 3886 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3887 | lastentry = entry; /* which entry we tried to generate last */ |
| 3888 | if (lastentry) { |
| 3889 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 3890 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 3891 | break; |
| 3892 | |
| 3893 | 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] | 3894 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3895 | /* this was not generated for this bind_conf, skip */ |
| 3896 | if (inst->bind_conf != bind_conf) |
| 3897 | continue; |
| 3898 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 3899 | /* free the sni_ctx and instance */ |
| 3900 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 3901 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3902 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3903 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 3904 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3905 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3906 | return cfgerr; |
| 3907 | } |
| 3908 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3909 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 3910 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 3911 | { |
| 3912 | struct stat buf; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3913 | int cfgerr = 0; |
| 3914 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 3915 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3916 | int found = 0; /* did we found a file to load ? */ |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3917 | |
| 3918 | if ((ckchs = ckchs_lookup(path))) { |
| 3919 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3920 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3921 | found++; |
| 3922 | } else if (stat(path, &buf) == 0) { |
| 3923 | found++; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3924 | if (S_ISDIR(buf.st_mode) == 0) { |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3925 | ckchs = ckchs_load_cert_file(path, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3926 | if (!ckchs) |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3927 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3928 | 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] | 3929 | } else { |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3930 | 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] | 3931 | } |
| 3932 | } else { |
| 3933 | /* stat failed, could be a bundle */ |
| 3934 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3935 | char fp[MAXPATHLEN+1] = {0}; |
| 3936 | int n = 0; |
| 3937 | |
| 3938 | /* Load all possible certs and keys in separate ckch_store */ |
| 3939 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3940 | struct stat buf; |
| 3941 | int ret; |
| 3942 | |
| 3943 | ret = snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3944 | if (ret > sizeof(fp)) |
| 3945 | continue; |
| 3946 | |
| 3947 | if ((ckchs = ckchs_lookup(fp))) { |
| 3948 | 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] | 3949 | found++; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3950 | } else { |
| 3951 | if (stat(fp, &buf) == 0) { |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3952 | found++; |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 3953 | ckchs = ckchs_load_cert_file(fp, err); |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3954 | if (!ckchs) |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3955 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 3956 | cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 3957 | } |
| 3958 | } |
| 3959 | } |
William Lallemand | b7fdfdf | 2020-12-04 15:45:02 +0100 | [diff] [blame] | 3960 | #if HA_OPENSSL_VERSION_NUMBER < 0x10101000L |
| 3961 | if (found) { |
| 3962 | memprintf(err, "%sCan't load '%s'. Loading a multi certificates bundle requires OpenSSL >= 1.1.1\n", |
| 3963 | err && *err ? *err : "", path); |
| 3964 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3965 | } |
| 3966 | #endif |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3967 | } |
| 3968 | } |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 3969 | if (!found) { |
| 3970 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3971 | err && *err ? *err : "", path, strerror(errno)); |
| 3972 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3973 | } |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 3974 | |
| 3975 | return cfgerr; |
| 3976 | } |
| 3977 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 3978 | |
| 3979 | /* Create a full ssl context and ckch instance that will be used for a specific |
| 3980 | * backend server (server configuration line). |
| 3981 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 3982 | */ |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 3983 | 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] | 3984 | { |
| 3985 | struct stat buf; |
| 3986 | int cfgerr = 0; |
| 3987 | struct ckch_store *ckchs; |
| 3988 | int found = 0; /* did we found a file to load ? */ |
| 3989 | |
| 3990 | if ((ckchs = ckchs_lookup(path))) { |
| 3991 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 3992 | 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] | 3993 | found++; |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 3994 | } else { |
| 3995 | if (!create_if_none) { |
| 3996 | memprintf(err, "%sunable to stat SSL certificate '%s'.\n", |
| 3997 | err && *err ? *err : "", path); |
| 3998 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 3999 | goto out; |
| 4000 | } |
| 4001 | |
| 4002 | if (stat(path, &buf) == 0) { |
| 4003 | /* We do not manage directories on backend side. */ |
| 4004 | if (S_ISDIR(buf.st_mode) == 0) { |
| 4005 | ++found; |
| 4006 | ckchs = ckchs_load_cert_file(path, err); |
| 4007 | if (!ckchs) |
| 4008 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4009 | cfgerr |= ssl_sock_load_srv_ckchs(path, ckchs, server, &server->ssl_ctx.inst, err); |
| 4010 | } |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4011 | } |
| 4012 | } |
| 4013 | if (!found) { |
| 4014 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4015 | err && *err ? *err : "", path, strerror(errno)); |
| 4016 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4017 | } |
| 4018 | |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4019 | out: |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4020 | return cfgerr; |
| 4021 | } |
| 4022 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4023 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4024 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4025 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4026 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4027 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4028 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4029 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4030 | SSL_OP_NO_SSLv2 | |
| 4031 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4032 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4033 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4034 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 4035 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4036 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4037 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4038 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4039 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4040 | SSL_MODE_RELEASE_BUFFERS | |
| 4041 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 4042 | 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] | 4043 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4044 | int flags = MC_SSL_O_ALL; |
| 4045 | int cfgerr = 0; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 4046 | const int default_min_ver = CONF_TLSV12; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4047 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4048 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4049 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4050 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4051 | 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] | 4052 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 4053 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4054 | 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] | 4055 | else |
| 4056 | flags = conf_ssl_methods->flags; |
| 4057 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 4058 | min = conf_ssl_methods->min; |
| 4059 | max = conf_ssl_methods->max; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 4060 | |
| 4061 | /* default minimum is TLSV12, */ |
| 4062 | if (!min) { |
| 4063 | if (!max || (max >= default_min_ver)) { |
| 4064 | min = default_min_ver; |
| 4065 | } else { |
| 4066 | 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). " |
| 4067 | "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n", |
| 4068 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name); |
| 4069 | min = max; |
| 4070 | } |
| 4071 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4072 | /* 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] | 4073 | if (min) |
| 4074 | flags |= (methodVersions[min].flag - 1); |
| 4075 | if (max) |
| 4076 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4077 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4078 | min = max = CONF_TLSV_NONE; |
| 4079 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4080 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4081 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4082 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4083 | if (min) { |
| 4084 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4085 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 4086 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4087 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 4088 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4089 | hole = 0; |
| 4090 | } |
| 4091 | max = i; |
| 4092 | } |
| 4093 | else { |
| 4094 | min = max = i; |
| 4095 | } |
| 4096 | } |
| 4097 | else { |
| 4098 | if (min) |
| 4099 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4100 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4101 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4102 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4103 | 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] | 4104 | cfgerr += 1; |
| 4105 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4106 | /* save real min/max in bind_conf */ |
| 4107 | conf_ssl_methods->min = min; |
| 4108 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4109 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4110 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4111 | /* 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] | 4112 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4113 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4114 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4115 | else |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4116 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) { |
| 4117 | /* clear every version flags in case SSL_CTX_new() |
| 4118 | * returns an SSL_CTX with disabled versions */ |
| 4119 | SSL_CTX_clear_options(ctx, methodVersions[i].option); |
| 4120 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4121 | if (flags & methodVersions[i].flag) |
| 4122 | options |= methodVersions[i].option; |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4123 | |
| 4124 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4125 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4126 | /* 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] | 4127 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4128 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 4129 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4130 | |
| 4131 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 4132 | options |= SSL_OP_NO_TICKET; |
| 4133 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 4134 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 4135 | |
| 4136 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 4137 | options |= SSL_OP_NO_RENEGOTIATION; |
| 4138 | #endif |
| 4139 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4140 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4141 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 4142 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4143 | if (global_ssl.async) |
| 4144 | mode |= SSL_MODE_ASYNC; |
| 4145 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4146 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4147 | if (global_ssl.life_time) |
| 4148 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4149 | |
| 4150 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4151 | # ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4152 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 4153 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4154 | # elif defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 4155 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 4156 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 4157 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4158 | # endif /* ! SSL_OP_NO_ANTI_REPLAY */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 4159 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 4160 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4161 | # else /* ! OPENSSL_IS_BORINGSSL && ! HAVE_SSL_CLIENT_HELLO_CB */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4162 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4163 | # endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 4164 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4165 | #endif /* ! SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4166 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4167 | } |
| 4168 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4169 | |
| 4170 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 4171 | { |
| 4172 | if (first == block) { |
| 4173 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4174 | if (first->len > 0) |
| 4175 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | /* return first block from sh_ssl_sess */ |
| 4180 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 4181 | { |
| 4182 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 4183 | |
| 4184 | } |
| 4185 | |
| 4186 | /* store a session into the cache |
| 4187 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 4188 | * data: asn1 encoded session |
| 4189 | * data_len: asn1 encoded session length |
| 4190 | * Returns 1 id session was stored (else 0) |
| 4191 | */ |
| 4192 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 4193 | { |
| 4194 | struct shared_block *first; |
| 4195 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 4196 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4197 | 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] | 4198 | if (!first) { |
| 4199 | /* Could not retrieve enough free blocks to store that session */ |
| 4200 | return 0; |
| 4201 | } |
| 4202 | |
| 4203 | /* STORE the key in the first elem */ |
| 4204 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4205 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 4206 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4207 | |
| 4208 | /* it returns the already existing node |
| 4209 | or current node if none, never returns null */ |
| 4210 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4211 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4212 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4213 | /* release the reserved row */ |
| 4214 | shctx_row_dec_hot(ssl_shctx, first); |
| 4215 | /* replace the previous session already in the tree */ |
| 4216 | sh_ssl_sess = oldsh_ssl_sess; |
| 4217 | /* ignore the previous session data, only use the header */ |
| 4218 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4219 | shctx_row_inc_hot(ssl_shctx, first); |
| 4220 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4221 | } |
| 4222 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4223 | 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] | 4224 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4225 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4229 | |
| 4230 | return 1; |
| 4231 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4232 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4233 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4234 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4235 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4236 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4237 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4238 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4239 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4240 | |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4241 | /* RWLOCK: only read lock the SSL cache even when writing in it because there is |
| 4242 | * one cache per thread, it only prevents to flush it from the CLI in |
| 4243 | * another thread */ |
| 4244 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4245 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4246 | int len; |
| 4247 | unsigned char *ptr; |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4248 | const char *sni; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4249 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4250 | len = i2d_SSL_SESSION(sess, NULL); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4251 | sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4252 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4253 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4254 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4255 | } else { |
Willy Tarreau | 3bda3f4 | 2021-02-26 21:05:08 +0100 | [diff] [blame] | 4256 | ptr = realloc(s->ssl_ctx.reused_sess[tid].ptr, len); |
| 4257 | s->ssl_ctx.reused_sess[tid].ptr = ptr; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4258 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4259 | } |
| 4260 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4261 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4262 | &ptr); |
| 4263 | } |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4264 | |
| 4265 | if (s->ssl_ctx.reused_sess[tid].sni) { |
| 4266 | /* if the new sni is empty or isn' t the same as the old one */ |
| 4267 | if ((!sni) || strcmp(s->ssl_ctx.reused_sess[tid].sni, sni) != 0) { |
| 4268 | ha_free(&s->ssl_ctx.reused_sess[tid].sni); |
| 4269 | if (sni) |
| 4270 | s->ssl_ctx.reused_sess[tid].sni = strdup(sni); |
| 4271 | } |
| 4272 | } else if (sni) { |
| 4273 | /* if there wasn't an old sni but there is a new one */ |
| 4274 | s->ssl_ctx.reused_sess[tid].sni = strdup(sni); |
| 4275 | } |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4276 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4277 | } else { |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4278 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4279 | ha_free(&s->ssl_ctx.reused_sess[tid].ptr); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4280 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4286 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4287 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4288 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4289 | { |
| 4290 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4291 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4292 | unsigned char *p; |
| 4293 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4294 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4295 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4296 | |
| 4297 | /* 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] | 4298 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4299 | * note: SSL_SESSION_set1_id is using |
| 4300 | * a memcpy so we need to use a different pointer |
| 4301 | * than sid_data or sid_ctx_data to avoid valgrind |
| 4302 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4303 | */ |
| 4304 | |
| 4305 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4306 | |
| 4307 | /* copy value in an other buffer */ |
| 4308 | memcpy(encid, sid_data, sid_length); |
| 4309 | |
| 4310 | /* pad with 0 */ |
| 4311 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4312 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4313 | |
| 4314 | /* force length to zero to avoid ASN1 encoding */ |
| 4315 | SSL_SESSION_set1_id(sess, encid, 0); |
| 4316 | |
| 4317 | /* force length to zero to avoid ASN1 encoding */ |
| 4318 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4319 | |
| 4320 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4321 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4322 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4323 | goto err; |
| 4324 | |
| 4325 | p = encsess; |
| 4326 | |
| 4327 | /* process ASN1 session encoding before the lock */ |
| 4328 | i2d_SSL_SESSION(sess, &p); |
| 4329 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4330 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4331 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4332 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4333 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4334 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4335 | err: |
| 4336 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4337 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 4338 | 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] | 4339 | |
| 4340 | return 0; /* do not increment session reference count */ |
| 4341 | } |
| 4342 | |
| 4343 | /* 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] | 4344 | 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] | 4345 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4346 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4347 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4348 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4349 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4350 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4351 | |
Willy Tarreau | 4c19e99 | 2021-06-15 16:39:22 +0200 | [diff] [blame] | 4352 | _HA_ATOMIC_INC(&global.shctx_lookups); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4353 | |
| 4354 | /* allow the session to be freed automatically by openssl */ |
| 4355 | *do_copy = 0; |
| 4356 | |
| 4357 | /* tree key is zeros padded sessionid */ |
| 4358 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4359 | memcpy(tmpkey, key, key_len); |
| 4360 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4361 | key = tmpkey; |
| 4362 | } |
| 4363 | |
| 4364 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4365 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4366 | |
| 4367 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4368 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4369 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4370 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4371 | shctx_unlock(ssl_shctx); |
Willy Tarreau | 4c19e99 | 2021-06-15 16:39:22 +0200 | [diff] [blame] | 4372 | _HA_ATOMIC_INC(&global.shctx_misses); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4373 | return NULL; |
| 4374 | } |
| 4375 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4376 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4377 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4378 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4379 | 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] | 4380 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4381 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4382 | |
| 4383 | /* decode ASN1 session */ |
| 4384 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4385 | 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] | 4386 | /* Reset session id and session id contenxt */ |
| 4387 | if (sess) { |
| 4388 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4389 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4390 | } |
| 4391 | |
| 4392 | return sess; |
| 4393 | } |
| 4394 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4395 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4396 | /* 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] | 4397 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4398 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4399 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4400 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4401 | unsigned int sid_length; |
| 4402 | const unsigned char *sid_data; |
| 4403 | (void)ctx; |
| 4404 | |
| 4405 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4406 | /* tree key is zeros padded sessionid */ |
| 4407 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4408 | memcpy(tmpkey, sid_data, sid_length); |
| 4409 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4410 | sid_data = tmpkey; |
| 4411 | } |
| 4412 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4413 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4414 | |
| 4415 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4416 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4417 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4418 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4419 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4420 | } |
| 4421 | |
| 4422 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4423 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4424 | } |
| 4425 | |
| 4426 | /* Set session cache mode to server and disable openssl internal cache. |
| 4427 | * Set shared cache callbacks on an ssl context. |
| 4428 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4429 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4430 | { |
| 4431 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4432 | |
| 4433 | if (!ssl_shctx) { |
| 4434 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4435 | return; |
| 4436 | } |
| 4437 | |
| 4438 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4439 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4440 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4441 | |
| 4442 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4443 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4444 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4445 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4446 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4447 | |
| 4448 | /* |
| 4449 | * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format |
| 4450 | * |
| 4451 | * The format is: |
| 4452 | * * <Label> <space> <ClientRandom> <space> <Secret> |
| 4453 | * We only need to copy the secret as there is a sample fetch for the ClientRandom |
| 4454 | */ |
| 4455 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 4456 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4457 | void SSL_CTX_keylog(const SSL *ssl, const char *line) |
| 4458 | { |
| 4459 | struct ssl_keylog *keylog; |
| 4460 | char *lastarg = NULL; |
| 4461 | char *dst = NULL; |
| 4462 | |
| 4463 | keylog = SSL_get_ex_data(ssl, ssl_keylog_index); |
| 4464 | if (!keylog) |
| 4465 | return; |
| 4466 | |
| 4467 | lastarg = strrchr(line, ' '); |
| 4468 | if (lastarg == NULL || ++lastarg == NULL) |
| 4469 | return; |
| 4470 | |
| 4471 | dst = pool_alloc(pool_head_ssl_keylog_str); |
| 4472 | if (!dst) |
| 4473 | return; |
| 4474 | |
| 4475 | strncpy(dst, lastarg, SSL_KEYLOG_MAX_SECRET_SIZE-1); |
| 4476 | dst[SSL_KEYLOG_MAX_SECRET_SIZE-1] = '\0'; |
| 4477 | |
| 4478 | if (strncmp(line, "CLIENT_RANDOM ", strlen("CLIENT RANDOM ")) == 0) { |
| 4479 | if (keylog->client_random) |
| 4480 | goto error; |
| 4481 | keylog->client_random = dst; |
| 4482 | |
| 4483 | } else if (strncmp(line, "CLIENT_EARLY_TRAFFIC_SECRET ", strlen("CLIENT_EARLY_TRAFFIC_SECRET ")) == 0) { |
| 4484 | if (keylog->client_early_traffic_secret) |
| 4485 | goto error; |
| 4486 | keylog->client_early_traffic_secret = dst; |
| 4487 | |
| 4488 | } else if (strncmp(line, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ", strlen("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 4489 | if(keylog->client_handshake_traffic_secret) |
| 4490 | goto error; |
| 4491 | keylog->client_handshake_traffic_secret = dst; |
| 4492 | |
| 4493 | } else if (strncmp(line, "SERVER_HANDSHAKE_TRAFFIC_SECRET ", strlen("SERVER_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 4494 | if (keylog->server_handshake_traffic_secret) |
| 4495 | goto error; |
| 4496 | keylog->server_handshake_traffic_secret = dst; |
| 4497 | |
| 4498 | } else if (strncmp(line, "CLIENT_TRAFFIC_SECRET_0 ", strlen("CLIENT_TRAFFIC_SECRET_0 ")) == 0) { |
| 4499 | if (keylog->client_traffic_secret_0) |
| 4500 | goto error; |
| 4501 | keylog->client_traffic_secret_0 = dst; |
| 4502 | |
| 4503 | } else if (strncmp(line, "SERVER_TRAFFIC_SECRET_0 ", strlen("SERVER_TRAFFIC_SECRET_0 ")) == 0) { |
| 4504 | if (keylog->server_traffic_secret_0) |
| 4505 | goto error; |
| 4506 | keylog->server_traffic_secret_0 = dst; |
| 4507 | |
| 4508 | } else if (strncmp(line, "EARLY_EXPORTER_SECRET ", strlen("EARLY_EXPORTER_SECRET ")) == 0) { |
| 4509 | if (keylog->early_exporter_secret) |
| 4510 | goto error; |
| 4511 | keylog->early_exporter_secret = dst; |
| 4512 | |
| 4513 | } else if (strncmp(line, "EXPORTER_SECRET ", strlen("EXPORTER_SECRET ")) == 0) { |
| 4514 | if (keylog->exporter_secret) |
| 4515 | goto error; |
| 4516 | keylog->exporter_secret = dst; |
| 4517 | } else { |
| 4518 | goto error; |
| 4519 | } |
| 4520 | |
| 4521 | return; |
| 4522 | |
| 4523 | error: |
| 4524 | pool_free(pool_head_ssl_keylog_str, dst); |
| 4525 | |
| 4526 | return; |
| 4527 | } |
| 4528 | #endif |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4529 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 4530 | /* |
| 4531 | * This function applies the SSL configuration on a SSL_CTX |
| 4532 | * It returns an error code and fills the <err> buffer |
| 4533 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4534 | 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] | 4535 | { |
| 4536 | struct proxy *curproxy = bind_conf->frontend; |
| 4537 | int cfgerr = 0; |
| 4538 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4539 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4540 | const char *conf_ciphers; |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 4541 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4542 | const char *conf_ciphersuites; |
| 4543 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4544 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4545 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4546 | if (ssl_conf) { |
| 4547 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4548 | int i, min, max; |
| 4549 | int flags = MC_SSL_O_ALL; |
| 4550 | |
| 4551 | /* 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] | 4552 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4553 | 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] | 4554 | if (min) |
| 4555 | flags |= (methodVersions[min].flag - 1); |
| 4556 | if (max) |
| 4557 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4558 | min = max = CONF_TLSV_NONE; |
| 4559 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4560 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4561 | if (min) |
| 4562 | max = i; |
| 4563 | else |
| 4564 | min = max = i; |
| 4565 | } |
| 4566 | /* save real min/max */ |
| 4567 | conf_ssl_methods->min = min; |
| 4568 | conf_ssl_methods->max = max; |
| 4569 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4570 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4571 | 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] | 4572 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4573 | } |
| 4574 | } |
| 4575 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4576 | 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] | 4577 | case SSL_SOCK_VERIFY_NONE: |
| 4578 | verify = SSL_VERIFY_NONE; |
| 4579 | break; |
| 4580 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4581 | verify = SSL_VERIFY_PEER; |
| 4582 | break; |
| 4583 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4584 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4585 | break; |
| 4586 | } |
| 4587 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4588 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4589 | 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] | 4590 | 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] | 4591 | 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] | 4592 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4593 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4594 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 4595 | 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] | 4596 | 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] | 4597 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4598 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 4599 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 4600 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 4601 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4602 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4603 | } |
| 4604 | 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] | 4605 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 4606 | 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] | 4607 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4608 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4609 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4610 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4611 | 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] | 4612 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4613 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4614 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4615 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4616 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4617 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 4618 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4619 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4620 | 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] | 4621 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4622 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4623 | else { |
| 4624 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4625 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4626 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4627 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4628 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4629 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4630 | #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] | 4631 | if(bind_conf->keys_ref) { |
Remi Tricot-Le Breton | 8ea1f5f | 2022-02-08 17:45:58 +0100 | [diff] [blame] | 4632 | 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] | 4633 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4634 | 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] | 4635 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4636 | } |
| 4637 | } |
| 4638 | #endif |
| 4639 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4640 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4641 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4642 | if (conf_ciphers && |
| 4643 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4644 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4645 | 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] | 4646 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4647 | } |
| 4648 | |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 4649 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4650 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4651 | if (conf_ciphersuites && |
| 4652 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4653 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4654 | 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] | 4655 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4656 | } |
| 4657 | #endif |
| 4658 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4659 | #ifndef OPENSSL_NO_DH |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 4660 | if (!local_dh_1024) |
| 4661 | local_dh_1024 = ssl_get_dh_1024(); |
| 4662 | if (!local_dh_2048) |
| 4663 | local_dh_2048 = ssl_get_dh_2048(); |
| 4664 | if (!local_dh_4096) |
| 4665 | local_dh_4096 = ssl_get_dh_4096(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4666 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4667 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4668 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Ilya Shipitsin | 7ff7747 | 2021-02-08 16:55:06 +0500 | [diff] [blame] | 4669 | #ifdef SSL_CTRL_SET_MSG_CALLBACK |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4670 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4671 | #endif |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 4672 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | b60a77b | 2022-11-18 15:00:15 +0100 | [diff] [blame] | 4673 | /* only activate the keylog callback if it was required to prevent performance loss */ |
| 4674 | if (global_ssl.keylog > 0) |
| 4675 | SSL_CTX_set_keylog_callback(ctx, SSL_CTX_keylog); |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4676 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4677 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4678 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4679 | ssl_conf_cur = NULL; |
| 4680 | if (ssl_conf && ssl_conf->npn_str) |
| 4681 | ssl_conf_cur = ssl_conf; |
| 4682 | else if (bind_conf->ssl_conf.npn_str) |
| 4683 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4684 | if (ssl_conf_cur) |
| 4685 | 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] | 4686 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4687 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4688 | ssl_conf_cur = NULL; |
| 4689 | if (ssl_conf && ssl_conf->alpn_str) |
| 4690 | ssl_conf_cur = ssl_conf; |
| 4691 | else if (bind_conf->ssl_conf.alpn_str) |
| 4692 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4693 | if (ssl_conf_cur) |
| 4694 | 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] | 4695 | #endif |
Ilya Shipitsin | 0aa8c29 | 2020-11-04 00:39:07 +0500 | [diff] [blame] | 4696 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4697 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4698 | if (conf_curves) { |
| 4699 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4700 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4701 | 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] | 4702 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4703 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4704 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4705 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4706 | #endif /* defined(SSL_CTX_set1_curves_list) */ |
| 4707 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4708 | if (!conf_curves) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4709 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4710 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4711 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4712 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4713 | NULL); |
| 4714 | |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4715 | if (ecdhe && SSL_CTX_set1_curves_list(ctx, ecdhe) == 0) { |
| 4716 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4717 | err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4718 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4719 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4720 | #endif /* defined(SSL_CTX_set1_curves_list) */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4721 | #else |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4722 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 4723 | int i; |
| 4724 | EC_KEY *ecdh; |
| 4725 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4726 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4727 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4728 | ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4729 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4730 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4731 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4732 | 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] | 4733 | 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] | 4734 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4735 | } |
| 4736 | else { |
| 4737 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4738 | EC_KEY_free(ecdh); |
| 4739 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 4740 | #endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */ |
| 4741 | #endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */ |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4742 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4743 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4744 | return cfgerr; |
| 4745 | } |
| 4746 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4747 | |
| 4748 | /* |
| 4749 | * Prepare the SSL_CTX based on the bind line configuration. |
| 4750 | * Since the CA file loading is made depending on the verify option of the bind |
| 4751 | * line, the link between the SSL_CTX and the CA file tree entry is made here. |
| 4752 | * If we want to create a link between the CA file entry and the corresponding |
| 4753 | * ckch instance (for CA file hot update), it needs to be done after |
| 4754 | * ssl_sock_prepare_ctx. |
| 4755 | * Returns 0 in case of success. |
| 4756 | */ |
| 4757 | int ssl_sock_prep_ctx_and_inst(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4758 | SSL_CTX *ctx, struct ckch_inst *ckch_inst, char **err) |
| 4759 | { |
| 4760 | int errcode = 0; |
| 4761 | |
| 4762 | errcode |= ssl_sock_prepare_ctx(bind_conf, ssl_conf, ctx, err); |
| 4763 | if (!errcode && ckch_inst) |
| 4764 | ckch_inst_add_cafile_link(ckch_inst, bind_conf, ssl_conf, NULL); |
| 4765 | |
| 4766 | return errcode; |
| 4767 | } |
| 4768 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4769 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4770 | { |
| 4771 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4772 | size_t prefixlen, suffixlen; |
| 4773 | |
| 4774 | /* Trivial case */ |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4775 | if (strcasecmp(pattern, hostname) == 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4776 | return 1; |
| 4777 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4778 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4779 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4780 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4781 | pattern_wildcard = NULL; |
| 4782 | pattern_left_label_end = pattern; |
| 4783 | while (*pattern_left_label_end != '.') { |
| 4784 | switch (*pattern_left_label_end) { |
| 4785 | case 0: |
| 4786 | /* End of label not found */ |
| 4787 | return 0; |
| 4788 | case '*': |
| 4789 | /* If there is more than one wildcards */ |
| 4790 | if (pattern_wildcard) |
| 4791 | return 0; |
| 4792 | pattern_wildcard = pattern_left_label_end; |
| 4793 | break; |
| 4794 | } |
| 4795 | pattern_left_label_end++; |
| 4796 | } |
| 4797 | |
| 4798 | /* If it's not trivial and there is no wildcard, it can't |
| 4799 | * match */ |
| 4800 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4801 | return 0; |
| 4802 | |
| 4803 | /* Make sure all labels match except the leftmost */ |
| 4804 | hostname_left_label_end = strchr(hostname, '.'); |
| 4805 | if (!hostname_left_label_end |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4806 | || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4807 | return 0; |
| 4808 | |
| 4809 | /* Make sure the leftmost label of the hostname is long enough |
| 4810 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4811 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4812 | return 0; |
| 4813 | |
| 4814 | /* Finally compare the string on either side of the |
| 4815 | * wildcard */ |
| 4816 | prefixlen = pattern_wildcard - pattern; |
| 4817 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 4818 | if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0)) |
| 4819 | || (suffixlen && (strncasecmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4820 | return 0; |
| 4821 | |
| 4822 | return 1; |
| 4823 | } |
| 4824 | |
| 4825 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4826 | { |
| 4827 | SSL *ssl; |
| 4828 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4829 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4830 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4831 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4832 | |
| 4833 | int depth; |
| 4834 | X509 *cert; |
| 4835 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4836 | int i; |
| 4837 | X509_NAME *cert_subject; |
| 4838 | char *str; |
| 4839 | |
| 4840 | if (ok == 0) |
| 4841 | return ok; |
| 4842 | |
| 4843 | 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] | 4844 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 4845 | ssl_ctx = __conn_get_ssl_sock_ctx(conn); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4846 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4847 | /* We're checking if the provided hostnames match the desired one. The |
| 4848 | * desired hostname comes from the SNI we presented if any, or if not |
| 4849 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4850 | * directive. If neither is set, we don't care about the name so the |
| 4851 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4852 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4853 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4854 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4855 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4856 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4857 | if (!servername) |
| 4858 | return ok; |
| 4859 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4860 | |
| 4861 | /* We only need to verify the CN on the actual server cert, |
| 4862 | * not the indirect CAs */ |
| 4863 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4864 | if (depth != 0) |
| 4865 | return ok; |
| 4866 | |
| 4867 | /* At this point, the cert is *not* OK unless we can find a |
| 4868 | * hostname match */ |
| 4869 | ok = 0; |
| 4870 | |
| 4871 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4872 | /* It seems like this might happen if verify peer isn't set */ |
| 4873 | if (!cert) |
| 4874 | return ok; |
| 4875 | |
| 4876 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4877 | if (alt_names) { |
| 4878 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4879 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4880 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4881 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4882 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4883 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4884 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4885 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4886 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4887 | OPENSSL_free(str); |
| 4888 | } |
| 4889 | } |
| 4890 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4891 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4892 | } |
| 4893 | |
| 4894 | cert_subject = X509_get_subject_name(cert); |
| 4895 | i = -1; |
| 4896 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4897 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4898 | ASN1_STRING *value; |
| 4899 | value = X509_NAME_ENTRY_get_data(entry); |
| 4900 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4901 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4902 | OPENSSL_free(str); |
| 4903 | } |
| 4904 | } |
| 4905 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4906 | /* report the mismatch and indicate if SNI was used or not */ |
| 4907 | if (!ok && !conn->err_code) |
| 4908 | 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] | 4909 | return ok; |
| 4910 | } |
| 4911 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4912 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4913 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4914 | { |
| 4915 | int cfgerr = 0; |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 4916 | SSL_CTX *ctx; |
William Lallemand | 0adafb3 | 2022-12-13 18:17:44 +0100 | [diff] [blame] | 4917 | /* Automatic memory computations need to know we use SSL there |
| 4918 | * 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] | 4919 | if (!(srv->proxy->cap & PR_CAP_INT)) |
William Lallemand | 0adafb3 | 2022-12-13 18:17:44 +0100 | [diff] [blame] | 4920 | global.ssl_used_backend = 1; |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4921 | |
| 4922 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4923 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4924 | 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] | 4925 | ha_alert("out of memory.\n"); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4926 | cfgerr++; |
| 4927 | return cfgerr; |
| 4928 | } |
| 4929 | } |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 4930 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4931 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4932 | |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 4933 | if (srv->ssl_ctx.client_crt) { |
| 4934 | const int create_if_none = srv->flags & SRV_F_DYNAMIC ? 0 : 1; |
| 4935 | char *err = NULL; |
| 4936 | int err_code = 0; |
| 4937 | |
| 4938 | /* If there is a crt keyword there, the SSL_CTX will be created here. */ |
| 4939 | err_code = ssl_sock_load_srv_cert(srv->ssl_ctx.client_crt, srv, create_if_none, &err); |
| 4940 | if (err_code != ERR_NONE) { |
| 4941 | if ((err_code & ERR_WARN) && !(err_code & ERR_ALERT)) |
| 4942 | ha_warning("%s", err); |
| 4943 | else |
| 4944 | ha_alert("%s", err); |
| 4945 | |
| 4946 | if (err_code & (ERR_FATAL|ERR_ABORT)) |
| 4947 | cfgerr++; |
| 4948 | } |
| 4949 | ha_free(&err); |
| 4950 | } |
| 4951 | |
| 4952 | ctx = srv->ssl_ctx.ctx; |
| 4953 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4954 | /* The context will be uninitialized if there wasn't any "cert" option |
| 4955 | * in the server line. */ |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4956 | if (!ctx) { |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4957 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 4958 | if (!ctx) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 4959 | ha_alert("unable to allocate ssl context.\n"); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4960 | cfgerr++; |
| 4961 | return cfgerr; |
| 4962 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4963 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4964 | srv->ssl_ctx.ctx = ctx; |
| 4965 | } |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 4966 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4967 | 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] | 4968 | |
| 4969 | return cfgerr; |
| 4970 | } |
| 4971 | |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 4972 | /* Initialize an SSL context that will be used on the backend side. |
| 4973 | * Returns an error count. |
| 4974 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 4975 | 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] | 4976 | { |
| 4977 | struct proxy *curproxy = srv->proxy; |
| 4978 | int cfgerr = 0; |
| 4979 | long options = |
| 4980 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4981 | SSL_OP_NO_SSLv2 | |
| 4982 | SSL_OP_NO_COMPRESSION; |
| 4983 | long mode = |
| 4984 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4985 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 4986 | SSL_MODE_RELEASE_BUFFERS | |
| 4987 | SSL_MODE_SMALL_BUFFERS; |
| 4988 | int verify = SSL_VERIFY_NONE; |
| 4989 | const struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
| 4990 | int i, min, max, hole; |
| 4991 | int flags = MC_SSL_O_ALL; |
| 4992 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4993 | 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] | 4994 | ha_warning("no-sslv3/no-tlsv1x are ignored for this server. " |
| 4995 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n"); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4996 | else |
| 4997 | flags = conf_ssl_methods->flags; |
| 4998 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4999 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5000 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5001 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5002 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5003 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5004 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5005 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5006 | min = max = CONF_TLSV_NONE; |
| 5007 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5008 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5009 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5010 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5011 | if (min) { |
| 5012 | if (hole) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 5013 | 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] | 5014 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5015 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5016 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5017 | hole = 0; |
| 5018 | } |
| 5019 | max = i; |
| 5020 | } |
| 5021 | else { |
| 5022 | min = max = i; |
| 5023 | } |
| 5024 | } |
| 5025 | else { |
| 5026 | if (min) |
| 5027 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5028 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5029 | if (!min) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 5030 | 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] | 5031 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5032 | cfgerr += 1; |
| 5033 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5034 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5035 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5036 | /* 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] | 5037 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5038 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5039 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5040 | else |
| 5041 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5042 | if (flags & methodVersions[i].flag) |
| 5043 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5044 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5045 | /* 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] | 5046 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5047 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5048 | #endif |
| 5049 | |
| 5050 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5051 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5052 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5053 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5054 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5055 | if (global_ssl.async) |
| 5056 | mode |= SSL_MODE_ASYNC; |
| 5057 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5058 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5059 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5060 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 5061 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5062 | switch (srv->ssl_ctx.verify) { |
| 5063 | case SSL_SOCK_VERIFY_NONE: |
| 5064 | verify = SSL_VERIFY_NONE; |
| 5065 | break; |
| 5066 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5067 | verify = SSL_VERIFY_PEER; |
| 5068 | break; |
| 5069 | } |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5070 | SSL_CTX_set_verify(ctx, verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5071 | (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] | 5072 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5073 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5074 | /* set CAfile to verify */ |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5075 | if (!ssl_set_verify_locations_file(ctx, srv->ssl_ctx.ca_file)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5076 | ha_alert("unable to set CA file '%s'.\n", |
| 5077 | srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5078 | cfgerr++; |
| 5079 | } |
| 5080 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5081 | else { |
| 5082 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5083 | 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] | 5084 | else |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5085 | ha_alert("verify is enabled but no CA file specified.\n"); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5086 | cfgerr++; |
| 5087 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5088 | #ifdef X509_V_FLAG_CRL_CHECK |
| 5089 | if (srv->ssl_ctx.crl_file) { |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5090 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5091 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5092 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5093 | ha_alert("unable to configure CRL file '%s'.\n", |
| 5094 | srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5095 | cfgerr++; |
| 5096 | } |
| 5097 | else { |
| 5098 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5099 | } |
| 5100 | } |
| 5101 | #endif |
| 5102 | } |
| 5103 | |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5104 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 5105 | SSL_CTX_sess_set_new_cb(ctx, ssl_sess_new_srv_cb); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5106 | if (srv->ssl_ctx.ciphers && |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5107 | !SSL_CTX_set_cipher_list(ctx, srv->ssl_ctx.ciphers)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5108 | ha_alert("unable to set SSL cipher list to '%s'.\n", |
| 5109 | srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5110 | cfgerr++; |
| 5111 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5112 | |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 5113 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5114 | if (srv->ssl_ctx.ciphersuites && |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5115 | !SSL_CTX_set_ciphersuites(ctx, srv->ssl_ctx.ciphersuites)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5116 | ha_alert("unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 5117 | srv->ssl_ctx.ciphersuites); |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5118 | cfgerr++; |
| 5119 | } |
| 5120 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5121 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 5122 | if (srv->ssl_ctx.npn_str) |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5123 | 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] | 5124 | #endif |
| 5125 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5126 | if (srv->ssl_ctx.alpn_str) |
| 5127 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 5128 | #endif |
| 5129 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5130 | |
| 5131 | return cfgerr; |
| 5132 | } |
| 5133 | |
| 5134 | /* |
| 5135 | * Prepare the frontend's SSL_CTX based on the server line configuration. |
| 5136 | * Since the CA file loading is made depending on the verify option of the |
| 5137 | * server line, the link between the SSL_CTX and the CA file tree entry is |
| 5138 | * made here. |
| 5139 | * If we want to create a link between the CA file entry and the corresponding |
| 5140 | * ckch instance (for CA file hot update), it needs to be done after |
| 5141 | * ssl_sock_prepare_srv_ssl_ctx. |
| 5142 | * Returns an error count. |
| 5143 | */ |
| 5144 | int ssl_sock_prep_srv_ctx_and_inst(const struct server *srv, SSL_CTX *ctx, |
| 5145 | struct ckch_inst *ckch_inst) |
| 5146 | { |
| 5147 | int cfgerr = 0; |
| 5148 | |
| 5149 | cfgerr += ssl_sock_prepare_srv_ssl_ctx(srv, ctx); |
| 5150 | if (!cfgerr && ckch_inst) |
| 5151 | ckch_inst_add_cafile_link(ckch_inst, NULL, NULL, srv); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5152 | |
| 5153 | return cfgerr; |
| 5154 | } |
| 5155 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5156 | |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5157 | /* |
| 5158 | * Create an initial CTX used to start the SSL connections. |
| 5159 | * May be used by QUIC xprt which makes usage of SSL sessions initialized from SSL_CTXs. |
| 5160 | * Returns 0 if succeeded, or something >0 if not. |
| 5161 | */ |
| 5162 | #ifdef USE_QUIC |
| 5163 | static int ssl_initial_ctx(struct bind_conf *bind_conf) |
| 5164 | { |
| 5165 | if (bind_conf->xprt == xprt_get(XPRT_QUIC)) |
| 5166 | return ssl_quic_initial_ctx(bind_conf); |
| 5167 | else |
| 5168 | return ssl_sock_initial_ctx(bind_conf); |
| 5169 | } |
| 5170 | #else |
| 5171 | static int ssl_initial_ctx(struct bind_conf *bind_conf) |
| 5172 | { |
| 5173 | return ssl_sock_initial_ctx(bind_conf); |
| 5174 | } |
| 5175 | #endif |
| 5176 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5177 | /* 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] | 5178 | * be NULL, in which case nothing is done. Returns the number of errors |
| 5179 | * encountered. |
| 5180 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5181 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5182 | { |
| 5183 | struct ebmb_node *node; |
| 5184 | struct sni_ctx *sni; |
| 5185 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5186 | int errcode = 0; |
| 5187 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5188 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5189 | /* Automatic memory computations need to know we use SSL there */ |
| 5190 | global.ssl_used_frontend = 1; |
| 5191 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5192 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 5193 | if (!bind_conf->initial_ctx) { |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5194 | err += ssl_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5195 | /* It should not be necessary to call this function, but it's |
| 5196 | necessary first to check and move all initialisation related |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5197 | to initial_ctx in ssl_initial_ctx. */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5198 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, NULL, bind_conf->initial_ctx, NULL, &errmsg); |
| 5199 | } |
| 5200 | if (bind_conf->default_ctx) { |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 5201 | 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] | 5202 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5203 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5204 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5205 | while (node) { |
| 5206 | sni = ebmb_entry(node, struct sni_ctx, name); |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5207 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5208 | /* only initialize the CTX on its first occurrence and |
| 5209 | if it is not the default_ctx */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5210 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, sni->conf, sni->ctx, sni->ckch_inst, &errmsg); |
| 5211 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5212 | node = ebmb_next(node); |
| 5213 | } |
| 5214 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5215 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5216 | while (node) { |
| 5217 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5218 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5219 | /* only initialize the CTX on its first occurrence and |
| 5220 | if it is not the default_ctx */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5221 | 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] | 5222 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5223 | node = ebmb_next(node); |
| 5224 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5225 | |
| 5226 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5227 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5228 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5229 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5230 | err++; |
| 5231 | } |
| 5232 | |
| 5233 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5234 | return err; |
| 5235 | } |
| 5236 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5237 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 5238 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 5239 | * alerts are directly emitted since the rest of the stack does it below. |
| 5240 | */ |
| 5241 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 5242 | { |
| 5243 | struct proxy *px = bind_conf->frontend; |
| 5244 | int alloc_ctx; |
| 5245 | int err; |
| 5246 | |
Willy Tarreau | 11ba404 | 2022-05-20 15:56:32 +0200 | [diff] [blame] | 5247 | if (!(bind_conf->options & BC_O_USE_SSL)) { |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5248 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5249 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 5250 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5251 | } |
| 5252 | return 0; |
| 5253 | } |
| 5254 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5255 | if (bind_conf->strict_sni && !(bind_conf->options & BC_O_GENERATE_CERTS)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5256 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 5257 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5258 | } |
| 5259 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5260 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 5261 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5262 | return -1; |
| 5263 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5264 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 5265 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5266 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 5267 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
Willy Tarreau | 9135859 | 2021-06-15 08:08:04 +0200 | [diff] [blame] | 5268 | sizeof(*sh_ssl_sess_tree), (global.nbthread > 1)); |
Frédéric Lécaille | 4c8aa11 | 2018-10-25 20:22:46 +0200 | [diff] [blame] | 5269 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5270 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 5271 | 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"); |
| 5272 | else |
| 5273 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 5274 | return -1; |
| 5275 | } |
| 5276 | /* free block callback */ |
| 5277 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 5278 | /* init the root tree within the extra space */ |
| 5279 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 5280 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5281 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5282 | err = 0; |
| 5283 | /* initialize all certificate contexts */ |
| 5284 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 5285 | |
| 5286 | /* initialize CA variables if the certificates generation is enabled */ |
| 5287 | err += ssl_sock_load_ca(bind_conf); |
| 5288 | |
| 5289 | return -err; |
| 5290 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5291 | |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5292 | /* release ssl context allocated for servers. Most of the field free here |
| 5293 | * must also be allocated in srv_ssl_settings_cpy() */ |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5294 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 5295 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5296 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5297 | ha_free(&srv->ssl_ctx.alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5298 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 5299 | #ifdef OPENSSL_NPN_NEGOTIATED |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5300 | ha_free(&srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 5301 | #endif |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 5302 | if (srv->ssl_ctx.reused_sess) { |
| 5303 | int i; |
| 5304 | |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 5305 | for (i = 0; i < global.nbthread; i++) { |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5306 | ha_free(&srv->ssl_ctx.reused_sess[i].ptr); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 5307 | ha_free(&srv->ssl_ctx.reused_sess[i].sni); |
| 5308 | } |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5309 | ha_free(&srv->ssl_ctx.reused_sess); |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 5310 | } |
| 5311 | |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5312 | if (srv->ssl_ctx.ctx) { |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5313 | SSL_CTX_free(srv->ssl_ctx.ctx); |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5314 | srv->ssl_ctx.ctx = NULL; |
| 5315 | } |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5316 | |
| 5317 | ha_free(&srv->ssl_ctx.ca_file); |
| 5318 | ha_free(&srv->ssl_ctx.crl_file); |
| 5319 | ha_free(&srv->ssl_ctx.client_crt); |
| 5320 | ha_free(&srv->ssl_ctx.verify_host); |
| 5321 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5322 | ha_free(&srv->sni_expr); |
William Lallemand | 43c2ce4 | 2022-03-16 17:48:19 +0100 | [diff] [blame] | 5323 | release_sample_expr(srv->ssl_ctx.sni); |
| 5324 | srv->ssl_ctx.sni = NULL; |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5325 | #endif |
| 5326 | ha_free(&srv->ssl_ctx.ciphers); |
| 5327 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
| 5328 | ha_free(&srv->ssl_ctx.ciphersuites); |
| 5329 | #endif |
William Lallemand | e69563f | 2021-12-30 14:45:19 +0100 | [diff] [blame] | 5330 | /* If there is a certificate we must unlink the ckch instance */ |
| 5331 | ckch_inst_free(srv->ssl_ctx.inst); |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5332 | } |
| 5333 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5334 | /* 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] | 5335 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 5336 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5337 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5338 | { |
| 5339 | struct ebmb_node *node, *back; |
| 5340 | struct sni_ctx *sni; |
| 5341 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5342 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5343 | while (node) { |
| 5344 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5345 | back = ebmb_next(node); |
| 5346 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5347 | SSL_CTX_free(sni->ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5348 | LIST_DELETE(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5349 | free(sni); |
| 5350 | node = back; |
| 5351 | } |
| 5352 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5353 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5354 | while (node) { |
| 5355 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5356 | back = ebmb_next(node); |
| 5357 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5358 | SSL_CTX_free(sni->ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5359 | LIST_DELETE(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5360 | free(sni); |
| 5361 | node = back; |
| 5362 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 5363 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5364 | SSL_CTX_free(bind_conf->initial_ctx); |
| 5365 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5366 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5367 | bind_conf->default_ctx = NULL; |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 5368 | bind_conf->default_inst = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5369 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5370 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 5371 | |
| 5372 | |
| 5373 | void ssl_sock_deinit() |
| 5374 | { |
| 5375 | crtlist_deinit(); /* must be free'd before the ckchs */ |
| 5376 | ckch_deinit(); |
| 5377 | } |
| 5378 | REGISTER_POST_DEINIT(ssl_sock_deinit); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5379 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5380 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5381 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5382 | { |
| 5383 | ssl_sock_free_ca(bind_conf); |
| 5384 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5385 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5386 | free(bind_conf->ca_sign_file); |
| 5387 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5388 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5389 | free(bind_conf->keys_ref->filename); |
| 5390 | free(bind_conf->keys_ref->tlskeys); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5391 | LIST_DELETE(&bind_conf->keys_ref->list); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5392 | free(bind_conf->keys_ref); |
| 5393 | } |
| 5394 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5395 | bind_conf->ca_sign_pass = NULL; |
| 5396 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5397 | } |
| 5398 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5399 | /* Load CA cert file and private key used to generate certificates */ |
| 5400 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5401 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5402 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5403 | struct proxy *px = bind_conf->frontend; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5404 | struct ckch_data *data = NULL; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5405 | int ret = 0; |
| 5406 | char *err = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5407 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5408 | if (!(bind_conf->options & BC_O_GENERATE_CERTS)) |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5409 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5410 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5411 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5412 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5413 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5414 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5415 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5416 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5417 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5418 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5419 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5420 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5421 | "no CA certificate File configured at [%s:%d].\n", |
| 5422 | px->id, bind_conf->file, bind_conf->line); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5423 | goto failed; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5424 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5425 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5426 | /* Allocate cert structure */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5427 | data = calloc(1, sizeof(*data)); |
| 5428 | if (!data) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5429 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain allocation failure\n", |
| 5430 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 5431 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5432 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5433 | |
| 5434 | /* Try to parse file */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5435 | 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] | 5436 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain loading failed: %s\n", |
| 5437 | 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] | 5438 | free(err); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5439 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5440 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5441 | |
| 5442 | /* Fail if missing cert or pkey */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5443 | if ((!data->cert) || (!data->key)) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5444 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain missing certificate or private key\n", |
| 5445 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 5446 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5447 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5448 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5449 | /* Final assignment to bind */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5450 | bind_conf->ca_sign_ckch = data; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5451 | return ret; |
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 | failed: |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5454 | if (data) { |
| 5455 | ssl_sock_free_cert_key_and_chain_contents(data); |
| 5456 | free(data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5457 | } |
| 5458 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5459 | bind_conf->options &= ~BC_O_GENERATE_CERTS; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5460 | ret++; |
| 5461 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5462 | } |
| 5463 | |
| 5464 | /* Release CA cert and private key used to generate certificated */ |
| 5465 | void |
| 5466 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5467 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5468 | if (bind_conf->ca_sign_ckch) { |
| 5469 | 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] | 5470 | ha_free(&bind_conf->ca_sign_ckch); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5471 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5472 | } |
| 5473 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5474 | /* |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5475 | * Try to allocate the BIO and SSL session objects of <conn> connection with <bio> and |
| 5476 | * <ssl> as addresses, <bio_meth> as BIO method and <ssl_ctx> as SSL context inherited settings. |
| 5477 | * Connect the allocated BIO to the allocated SSL session. Also set <ctx> as address of custom |
| 5478 | * 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] | 5479 | * 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] | 5480 | * as parameters to this function. |
| 5481 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <conn> to |
| 5482 | * CO_ER_SSL_NO_MEM. |
| 5483 | */ |
| 5484 | int ssl_bio_and_sess_init(struct connection *conn, SSL_CTX *ssl_ctx, |
| 5485 | SSL **ssl, BIO **bio, BIO_METHOD *bio_meth, void *ctx) |
| 5486 | { |
| 5487 | int retry = 1; |
| 5488 | |
| 5489 | retry: |
| 5490 | /* Alloc a new SSL session. */ |
| 5491 | *ssl = SSL_new(ssl_ctx); |
| 5492 | if (!*ssl) { |
| 5493 | if (!retry--) |
| 5494 | goto err; |
| 5495 | |
| 5496 | pool_gc(NULL); |
| 5497 | goto retry; |
| 5498 | } |
| 5499 | |
| 5500 | *bio = BIO_new(bio_meth); |
| 5501 | if (!*bio) { |
| 5502 | SSL_free(*ssl); |
| 5503 | *ssl = NULL; |
| 5504 | if (!retry--) |
| 5505 | goto err; |
| 5506 | |
| 5507 | pool_gc(NULL); |
| 5508 | goto retry; |
| 5509 | } |
| 5510 | |
| 5511 | BIO_set_data(*bio, ctx); |
| 5512 | SSL_set_bio(*ssl, *bio, *bio); |
| 5513 | |
| 5514 | /* set connection pointer. */ |
| 5515 | if (!SSL_set_ex_data(*ssl, ssl_app_data_index, conn)) { |
| 5516 | SSL_free(*ssl); |
| 5517 | *ssl = NULL; |
| 5518 | if (!retry--) |
| 5519 | goto err; |
| 5520 | |
| 5521 | pool_gc(NULL); |
| 5522 | goto retry; |
| 5523 | } |
| 5524 | |
| 5525 | return 0; |
| 5526 | |
| 5527 | err: |
| 5528 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5529 | return -1; |
| 5530 | } |
| 5531 | |
Olivier Houchard | bc5ce92 | 2021-03-05 23:47:00 +0100 | [diff] [blame] | 5532 | /* This function is called when all the XPRT have been initialized. We can |
| 5533 | * now attempt to start the SSL handshake. |
| 5534 | */ |
| 5535 | static int ssl_sock_start(struct connection *conn, void *xprt_ctx) |
| 5536 | { |
| 5537 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5538 | |
| 5539 | if (ctx->xprt->start) { |
| 5540 | int ret; |
| 5541 | |
| 5542 | ret = ctx->xprt->start(conn, ctx->xprt_ctx); |
| 5543 | if (ret < 0) |
| 5544 | return ret; |
| 5545 | } |
| 5546 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 5547 | |
| 5548 | return 0; |
| 5549 | } |
| 5550 | |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5551 | /* |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5552 | * This function is called if SSL * context is not yet allocated. The function |
| 5553 | * is designed to be called before any other data-layer operation and sets the |
| 5554 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5555 | * It returns 0 on success and -1 in error case. |
| 5556 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5557 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5558 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5559 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5560 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5561 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5562 | return 0; |
| 5563 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5564 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 5565 | if (!ctx) { |
| 5566 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5567 | return -1; |
| 5568 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5569 | ctx->wait_event.tasklet = tasklet_new(); |
| 5570 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5571 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5572 | pool_free(ssl_sock_ctx_pool, ctx); |
| 5573 | return -1; |
| 5574 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5575 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 5576 | ctx->wait_event.tasklet->context = ctx; |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 5577 | 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] | 5578 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5579 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5580 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5581 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 5582 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 5583 | ctx->xprt_st = 0; |
| 5584 | ctx->xprt_ctx = NULL; |
Remi Tricot-Le Breton | 1fe0fad | 2021-09-29 18:56:52 +0200 | [diff] [blame] | 5585 | ctx->error_code = 0; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5586 | |
| 5587 | /* Only work with sockets for now, this should be adapted when we'll |
| 5588 | * add QUIC support. |
| 5589 | */ |
| 5590 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5591 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5592 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 5593 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5594 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5595 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5596 | if (global.maxsslconn && global.sslconns >= global.maxsslconn) { |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5597 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5598 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5599 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5600 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5601 | /* If it is in client mode initiate SSL session |
| 5602 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5603 | if (objt_server(conn->target)) { |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5604 | if (ssl_bio_and_sess_init(conn, __objt_server(conn->target)->ssl_ctx.ctx, |
| 5605 | &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5606 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5607 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5608 | SSL_set_connect_state(ctx->ssl); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 5609 | 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] | 5610 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5611 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 5612 | 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] | 5613 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5614 | SSL_SESSION_free(sess); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 5615 | ha_free(&__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5616 | } else if (sess) { |
| 5617 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5618 | } |
| 5619 | } |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 5620 | 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] | 5621 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5622 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5623 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5624 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5625 | _HA_ATOMIC_INC(&global.sslconns); |
| 5626 | _HA_ATOMIC_INC(&global.totalsslconns); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5627 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5628 | return 0; |
| 5629 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5630 | else if (objt_listener(conn->target)) { |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5631 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5632 | |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5633 | if (ssl_bio_and_sess_init(conn, bc->initial_ctx, |
| 5634 | &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5635 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5636 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5637 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 5638 | if (bc->ssl_conf.early_data) { |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 5639 | b_alloc(&ctx->early_buf); |
| 5640 | SSL_set_max_early_data(ctx->ssl, |
| 5641 | /* Only allow early data if we managed to allocate |
| 5642 | * a buffer. |
| 5643 | */ |
| 5644 | (!b_is_null(&ctx->early_buf)) ? |
| 5645 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 5646 | } |
| 5647 | #endif |
| 5648 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5649 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5650 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5651 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5652 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5653 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Willy Tarreau | a84986a | 2021-02-03 11:21:38 +0100 | [diff] [blame] | 5654 | if (bc->ssl_conf.early_data) |
| 5655 | conn->flags |= CO_FL_EARLY_SSL_HS; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5656 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5657 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 5658 | _HA_ATOMIC_INC(&global.sslconns); |
| 5659 | _HA_ATOMIC_INC(&global.totalsslconns); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5660 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5661 | return 0; |
| 5662 | } |
| 5663 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5664 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5665 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5666 | if (ctx && ctx->wait_event.tasklet) |
| 5667 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5668 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5669 | return -1; |
| 5670 | } |
| 5671 | |
| 5672 | |
| 5673 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5674 | * updates the FD status if it wants some polling before being called again. |
| 5675 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5676 | * otherwise it returns non-zero and removes itself from the connection's |
| 5677 | * flags (the bit is provided in <flag> by the caller). |
| 5678 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5679 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5680 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 5681 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5682 | int ret; |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 5683 | struct ssl_counters *counters = NULL; |
| 5684 | struct ssl_counters *counters_px = NULL; |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 5685 | struct listener *li; |
| 5686 | struct server *srv; |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5687 | socklen_t lskerr; |
| 5688 | int skerr; |
| 5689 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5690 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5691 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5692 | return 0; |
| 5693 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5694 | /* get counters */ |
| 5695 | switch (obj_type(conn->target)) { |
| 5696 | case OBJ_TYPE_LISTENER: |
Amaury Denoyelle | 2bf5d41 | 2021-07-26 09:59:06 +0200 | [diff] [blame] | 5697 | li = __objt_listener(conn->target); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5698 | counters = EXTRA_COUNTERS_GET(li->extra_counters, &ssl_stats_module); |
| 5699 | counters_px = EXTRA_COUNTERS_GET(li->bind_conf->frontend->extra_counters_fe, |
| 5700 | &ssl_stats_module); |
| 5701 | break; |
| 5702 | |
| 5703 | case OBJ_TYPE_SERVER: |
Amaury Denoyelle | 2bf5d41 | 2021-07-26 09:59:06 +0200 | [diff] [blame] | 5704 | srv = __objt_server(conn->target); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 5705 | counters = EXTRA_COUNTERS_GET(srv->extra_counters, &ssl_stats_module); |
| 5706 | counters_px = EXTRA_COUNTERS_GET(srv->proxy->extra_counters_be, |
| 5707 | &ssl_stats_module); |
| 5708 | break; |
| 5709 | |
| 5710 | default: |
| 5711 | break; |
| 5712 | } |
| 5713 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 5714 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5715 | goto out_error; |
| 5716 | |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5717 | /* don't start calculating a handshake on a dead connection */ |
| 5718 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) |
| 5719 | goto out_error; |
| 5720 | |
| 5721 | /* FIXME/WT: for now we don't have a clear way to inspect the connection |
| 5722 | * status from the lower layers, so let's check the FD directly. Ideally |
| 5723 | * the xprt layers should provide some status indicating their knowledge |
| 5724 | * of shutdowns or error. |
| 5725 | */ |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 5726 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 5727 | |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 5728 | skerr = 0; |
| 5729 | lskerr = sizeof(skerr); |
| 5730 | if ((getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) < 0) || |
| 5731 | skerr != 0) |
| 5732 | goto out_error; |
| 5733 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5734 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5735 | /* |
| 5736 | * Check if we have early data. If we do, we have to read them |
| 5737 | * before SSL_do_handshake() is called, And there's no way to |
| 5738 | * detect early data, except to try to read them |
| 5739 | */ |
| 5740 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5741 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5742 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 5743 | while (1) { |
| 5744 | ret = SSL_read_early_data(ctx->ssl, |
| 5745 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 5746 | &read_data); |
| 5747 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5748 | goto check_error; |
| 5749 | if (read_data > 0) { |
| 5750 | conn->flags |= CO_FL_EARLY_DATA; |
| 5751 | b_add(&ctx->early_buf, read_data); |
| 5752 | } |
| 5753 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5754 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5755 | if (!b_data(&ctx->early_buf)) |
| 5756 | b_free(&ctx->early_buf); |
| 5757 | break; |
| 5758 | } |
| 5759 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5760 | } |
| 5761 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5762 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5763 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5764 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5765 | * the reneg handshake. |
| 5766 | * Here we use SSL_peek as a workaround for reneg. |
| 5767 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 5768 | 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] | 5769 | char c; |
| 5770 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5771 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5772 | if (ret <= 0) { |
| 5773 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5774 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5775 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5776 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5777 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5778 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5779 | 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] | 5780 | return 0; |
| 5781 | } |
| 5782 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5783 | /* handshake may have been completed but we have |
| 5784 | * no more data to read. |
| 5785 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5786 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5787 | ret = 1; |
| 5788 | goto reneg_ok; |
| 5789 | } |
| 5790 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5791 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5792 | 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] | 5793 | return 0; |
| 5794 | } |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5795 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5796 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5797 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5798 | return 0; |
| 5799 | } |
| 5800 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5801 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5802 | /* if errno is null, then connection was successfully established */ |
| 5803 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5804 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5805 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5806 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5807 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5808 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5809 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5810 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5811 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5812 | /* 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] | 5813 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5814 | empty_handshake = state == TLS_ST_BEFORE; |
| 5815 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5816 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5817 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5818 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5819 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5820 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5821 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5822 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5823 | else |
| 5824 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5825 | } |
| 5826 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5827 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5828 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5829 | else |
| 5830 | conn->err_code = CO_ER_SSL_ABORT; |
| 5831 | } |
| 5832 | } |
| 5833 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5834 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5835 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5836 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5837 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5838 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5839 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5840 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5841 | goto out_error; |
| 5842 | } |
| 5843 | else { |
| 5844 | /* Fail on all other handshake errors */ |
| 5845 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5846 | * buffer, causing an RST to be emitted upon close() on |
| 5847 | * TCP sockets. We first try to drain possibly pending |
| 5848 | * data to avoid this as much as possible. |
| 5849 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 5850 | conn_ctrl_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5851 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5852 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5853 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5854 | goto out_error; |
| 5855 | } |
| 5856 | } |
| 5857 | /* read some data: consider handshake completed */ |
| 5858 | goto reneg_ok; |
| 5859 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5860 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5861 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5862 | if (ret != 1) { |
| 5863 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5864 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5865 | |
Remi Tricot-Le Breton | 1fe0fad | 2021-09-29 18:56:52 +0200 | [diff] [blame] | 5866 | if (!ctx->error_code) |
| 5867 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 7c6898e | 2021-07-29 09:45:51 +0200 | [diff] [blame] | 5868 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5869 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5870 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5871 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5872 | 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] | 5873 | return 0; |
| 5874 | } |
| 5875 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5876 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5877 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5878 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5879 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5880 | return 0; |
| 5881 | } |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5882 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5883 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5884 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5885 | return 0; |
| 5886 | } |
| 5887 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5888 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5889 | /* if errno is null, then connection was successfully established */ |
| 5890 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5891 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5892 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5893 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5894 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5895 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5896 | #else |
| 5897 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5898 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5899 | /* 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] | 5900 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5901 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5902 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5903 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5904 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5905 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5906 | if (empty_handshake) { |
| 5907 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5908 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5909 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5910 | else |
| 5911 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5912 | } |
| 5913 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5914 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5915 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5916 | else |
| 5917 | conn->err_code = CO_ER_SSL_ABORT; |
| 5918 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5919 | } |
| 5920 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5921 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5922 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5923 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5924 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5925 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5926 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5927 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5928 | goto out_error; |
| 5929 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5930 | else { |
| 5931 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5932 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5933 | * buffer, causing an RST to be emitted upon close() on |
| 5934 | * TCP sockets. We first try to drain possibly pending |
| 5935 | * data to avoid this as much as possible. |
| 5936 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 5937 | conn_ctrl_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5938 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5939 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5940 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5941 | goto out_error; |
| 5942 | } |
| 5943 | } |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 5944 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5945 | else { |
| 5946 | /* |
| 5947 | * If the server refused the early data, we have to send a |
| 5948 | * 425 to the client, as we no longer have the data to sent |
| 5949 | * them again. |
| 5950 | */ |
| 5951 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5952 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5953 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5954 | goto out_error; |
| 5955 | } |
| 5956 | } |
| 5957 | } |
| 5958 | #endif |
| 5959 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5960 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5961 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5962 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5963 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5964 | /* ASYNC engine API doesn't support moving read/write |
| 5965 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 5966 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5967 | */ |
| 5968 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5969 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5970 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5971 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5972 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5973 | if (objt_server(conn->target)) { |
| 5974 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5975 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5976 | 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] | 5977 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5978 | else { |
| 5979 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5980 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5981 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5982 | } |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 5983 | |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 5984 | if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 5985 | HA_ATOMIC_INC(&counters->sess); |
| 5986 | HA_ATOMIC_INC(&counters_px->sess); |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 5987 | } |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 5988 | } |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 5989 | else if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 5990 | HA_ATOMIC_INC(&counters->reused_sess); |
| 5991 | HA_ATOMIC_INC(&counters_px->reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5992 | } |
| 5993 | |
| 5994 | /* The connection is now established at both layers, it's time to leave */ |
| 5995 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5996 | return 1; |
| 5997 | |
| 5998 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5999 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6000 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6001 | ERR_clear_error(); |
| 6002 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6003 | /* free resumed session if exists */ |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6004 | if (objt_server(conn->target)) { |
| 6005 | struct server *s = __objt_server(conn->target); |
| 6006 | /* RWLOCK: only rdlock the SSL cache even when writing in it because there is |
| 6007 | * one cache per thread, it only prevents to flush it from the CLI in |
| 6008 | * another thread */ |
| 6009 | |
| 6010 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 6011 | if (s->ssl_ctx.reused_sess[tid].ptr) |
| 6012 | ha_free(&s->ssl_ctx.reused_sess[tid].ptr); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6013 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6014 | } |
| 6015 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6016 | if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6017 | HA_ATOMIC_INC(&counters->failed_handshake); |
| 6018 | HA_ATOMIC_INC(&counters_px->failed_handshake); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6019 | } |
| 6020 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6021 | /* Fail on all other handshake errors */ |
| 6022 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6023 | if (!conn->err_code) |
| 6024 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6025 | return 0; |
| 6026 | } |
| 6027 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6028 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6029 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6030 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6031 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6032 | * unless the transport layer was already released. |
| 6033 | */ |
| 6034 | 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] | 6035 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6036 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6037 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6038 | if (!ctx) |
| 6039 | return -1; |
| 6040 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6041 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6042 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6043 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6044 | ctx->subs = es; |
| 6045 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6046 | |
| 6047 | /* we may have to subscribe to lower layers for new events */ |
| 6048 | event_type &= ~ctx->wait_event.events; |
| 6049 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6050 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6051 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6052 | } |
| 6053 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6054 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6055 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6056 | * subscribe() call. It always returns zero. |
| 6057 | */ |
| 6058 | 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] | 6059 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6060 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6061 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6062 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6063 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6064 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6065 | es->events &= ~event_type; |
| 6066 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6067 | ctx->subs = NULL; |
| 6068 | |
| 6069 | /* If we subscribed, and we're not doing the handshake, |
| 6070 | * then we subscribed because the upper layer asked for it, |
| 6071 | * as the upper layer is no longer interested, we can |
| 6072 | * unsubscribe too. |
| 6073 | */ |
| 6074 | event_type &= ctx->wait_event.events; |
| 6075 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6076 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6077 | |
| 6078 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6079 | } |
| 6080 | |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6081 | /* The connection has been taken over, so destroy the old tasklet and create |
| 6082 | * a new one. The original thread ID must be passed into orig_tid |
| 6083 | * It should be called with the takeover lock for the old thread held. |
| 6084 | * Returns 0 on success, and -1 on failure |
| 6085 | */ |
| 6086 | static int ssl_takeover(struct connection *conn, void *xprt_ctx, int orig_tid) |
| 6087 | { |
| 6088 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6089 | struct tasklet *tl = tasklet_new(); |
| 6090 | |
| 6091 | if (!tl) |
| 6092 | return -1; |
| 6093 | |
| 6094 | ctx->wait_event.tasklet->context = NULL; |
| 6095 | tasklet_wakeup_on(ctx->wait_event.tasklet, orig_tid); |
| 6096 | ctx->wait_event.tasklet = tl; |
| 6097 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6098 | ctx->wait_event.tasklet->context = ctx; |
| 6099 | return 0; |
| 6100 | } |
| 6101 | |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6102 | /* notify the next xprt that the connection is about to become idle and that it |
| 6103 | * may be stolen at any time after the function returns and that any tasklet in |
| 6104 | * the chain must be careful before dereferencing its context. |
| 6105 | */ |
| 6106 | static void ssl_set_idle(struct connection *conn, void *xprt_ctx) |
| 6107 | { |
| 6108 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6109 | |
| 6110 | if (!ctx || !ctx->wait_event.tasklet) |
| 6111 | return; |
| 6112 | |
| 6113 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 6114 | if (ctx->xprt) |
| 6115 | xprt_set_idle(conn, ctx->xprt, ctx->xprt_ctx); |
| 6116 | } |
| 6117 | |
| 6118 | /* notify the next xprt that the connection is not idle anymore and that it may |
| 6119 | * not be stolen before the next xprt_set_idle(). |
| 6120 | */ |
| 6121 | static void ssl_set_used(struct connection *conn, void *xprt_ctx) |
| 6122 | { |
| 6123 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6124 | |
| 6125 | if (!ctx || !ctx->wait_event.tasklet) |
| 6126 | return; |
| 6127 | |
| 6128 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 6129 | if (ctx->xprt) |
| 6130 | xprt_set_used(conn, ctx->xprt, ctx->xprt_ctx); |
| 6131 | } |
| 6132 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6133 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6134 | * Returns 0 on success, and non-zero on failure. |
| 6135 | */ |
| 6136 | 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) |
| 6137 | { |
| 6138 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6139 | |
| 6140 | if (oldxprt_ops != NULL) |
| 6141 | *oldxprt_ops = ctx->xprt; |
| 6142 | if (oldxprt_ctx != NULL) |
| 6143 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6144 | ctx->xprt = toadd_ops; |
| 6145 | ctx->xprt_ctx = toadd_ctx; |
| 6146 | return 0; |
| 6147 | } |
| 6148 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6149 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6150 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6151 | * XPRT. |
| 6152 | */ |
| 6153 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6154 | { |
| 6155 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6156 | |
| 6157 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6158 | ctx->xprt_ctx = newctx; |
| 6159 | ctx->xprt = newops; |
| 6160 | return 0; |
| 6161 | } |
| 6162 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6163 | } |
| 6164 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6165 | 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] | 6166 | { |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6167 | struct tasklet *tl = (struct tasklet *)t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6168 | struct ssl_sock_ctx *ctx = context; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6169 | struct connection *conn; |
| 6170 | int conn_in_list; |
| 6171 | int ret = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6172 | |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6173 | if (state & TASK_F_USR1) { |
| 6174 | /* the tasklet was idling on an idle connection, it might have |
| 6175 | * been stolen, let's be careful! |
| 6176 | */ |
| 6177 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6178 | if (tl->context == NULL) { |
| 6179 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6180 | tasklet_free(tl); |
| 6181 | return NULL; |
| 6182 | } |
| 6183 | conn = ctx->conn; |
| 6184 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 6185 | if (conn_in_list) |
| 6186 | conn_delete_from_tree(&conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6187 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6188 | } else { |
| 6189 | conn = ctx->conn; |
| 6190 | conn_in_list = 0; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6191 | } |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6192 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6193 | /* First if we're doing an handshake, try that */ |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6194 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6195 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6196 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6197 | /* handshake completed, leave the bulk queue */ |
Willy Tarreau | 4c48edb | 2021-03-09 17:58:02 +0100 | [diff] [blame] | 6198 | _HA_ATOMIC_AND(&tl->state, ~TASK_HEAVY); |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6199 | } |
| 6200 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6201 | /* If we had an error, or the handshake is done and I/O is available, |
| 6202 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6203 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6204 | * we can't be sure conn_fd_handler() will be called again. |
| 6205 | */ |
| 6206 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6207 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6208 | int woke = 0; |
| 6209 | |
| 6210 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6211 | if (ctx->subs) { |
| 6212 | tasklet_wakeup(ctx->subs->tasklet); |
| 6213 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6214 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6215 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6216 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6217 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6218 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6219 | * upper layers know. If we have no mux, create it, |
| 6220 | * and once we have a mux, call its wake method if we didn't |
| 6221 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6222 | */ |
| 6223 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6224 | if (!ctx->conn->mux) |
| 6225 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6226 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6227 | ret = ctx->conn->mux->wake(ctx->conn); |
| 6228 | goto leave; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6229 | } |
| 6230 | } |
Ilya Shipitsin | 761d64c | 2021-01-07 11:59:58 +0500 | [diff] [blame] | 6231 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6232 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6233 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6234 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6235 | tasklet_wakeup(ctx->subs->tasklet); |
| 6236 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6237 | if (!ctx->subs->events) |
| 6238 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6239 | } |
| 6240 | #endif |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6241 | leave: |
| 6242 | if (!ret && conn_in_list) { |
| 6243 | struct server *srv = objt_server(conn->target); |
| 6244 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6245 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6246 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6247 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6248 | else |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6249 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6250 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6251 | } |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 6252 | return t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6253 | } |
| 6254 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6255 | /* 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] | 6256 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6257 | * buffer wraps, in which case a second call may be performed. The connection's |
| 6258 | * flags are updated with whatever special event is detected (error, read0, |
| 6259 | * empty). The caller is responsible for taking care of those events and |
| 6260 | * avoiding the call if inappropriate. The function does not call the |
| 6261 | * connection's polling update function, so the caller is responsible for this. |
| 6262 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6263 | 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] | 6264 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6265 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 6266 | ssize_t ret; |
| 6267 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6268 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6269 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6270 | goto out_error; |
| 6271 | |
Ilya Shipitsin | 761d64c | 2021-01-07 11:59:58 +0500 | [diff] [blame] | 6272 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6273 | if (b_data(&ctx->early_buf)) { |
| 6274 | try = b_contig_space(buf); |
| 6275 | if (try > b_data(&ctx->early_buf)) |
| 6276 | try = b_data(&ctx->early_buf); |
| 6277 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 6278 | b_add(buf, try); |
| 6279 | b_del(&ctx->early_buf, try); |
| 6280 | if (b_data(&ctx->early_buf) == 0) |
| 6281 | b_free(&ctx->early_buf); |
| 6282 | return try; |
| 6283 | } |
| 6284 | #endif |
| 6285 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6286 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6287 | /* a handshake was requested */ |
| 6288 | return 0; |
| 6289 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6290 | /* read the largest possible block. For this, we perform only one call |
| 6291 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 6292 | * in which case we accept to do it once again. A new attempt is made on |
| 6293 | * EINTR too. |
| 6294 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 6295 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6296 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6297 | try = b_contig_space(buf); |
| 6298 | if (!try) |
| 6299 | break; |
| 6300 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6301 | if (try > count) |
| 6302 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6303 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6304 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6305 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6306 | if (conn->flags & CO_FL_ERROR) { |
| 6307 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6308 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6309 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6310 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6311 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6312 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6313 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6314 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6315 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6316 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6317 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6318 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6319 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6320 | 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] | 6321 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6322 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6323 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6324 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6325 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6326 | break; |
| 6327 | } |
| 6328 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6329 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6330 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6331 | SUB_RETRY_RECV, |
| 6332 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6333 | /* handshake is running, and it may need to re-enable read */ |
| 6334 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6335 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6336 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6337 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6338 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6339 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6340 | break; |
| 6341 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6342 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6343 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 6344 | goto read0; |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6345 | else if (ret == SSL_ERROR_SSL) { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6346 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
| 6347 | if (ctx && !ctx->error_code) |
Remi Tricot-Le Breton | 9543d5a | 2021-09-29 18:56:53 +0200 | [diff] [blame] | 6348 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6349 | conn->err_code = CO_ERR_SSL_FATAL; |
| 6350 | } |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6351 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 6352 | * stack before shutting down the connection for |
| 6353 | * reading. */ |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 6354 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN || errno == EWOULDBLOCK)) |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6355 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6356 | /* otherwise it's a real error */ |
| 6357 | goto out_error; |
| 6358 | } |
| 6359 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6360 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6361 | return done; |
| 6362 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6363 | clear_ssl_error: |
| 6364 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6365 | ssl_sock_dump_errors(conn, NULL); |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6366 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6367 | read0: |
| 6368 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6369 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6370 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6371 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6372 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6373 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6374 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6375 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6376 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6377 | } |
| 6378 | |
| 6379 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6380 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 6381 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 6382 | * 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] | 6383 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 6384 | * a second call may be performed. The connection's flags are updated with |
| 6385 | * whatever special event is detected (error, empty). The caller is responsible |
| 6386 | * for taking care of those events and avoiding the call if inappropriate. The |
| 6387 | * 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] | 6388 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 6389 | * caller to take care of this. It's up to the caller to update the buffer's |
| 6390 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6391 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6392 | 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] | 6393 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6394 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6395 | ssize_t ret; |
| 6396 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6397 | |
| 6398 | done = 0; |
| 6399 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6400 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6401 | goto out_error; |
| 6402 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6403 | 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] | 6404 | /* a handshake was requested */ |
| 6405 | return 0; |
| 6406 | |
| 6407 | /* send the largest possible block. For this we perform only one call |
| 6408 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 6409 | * in which case we accept to do it once again. |
| 6410 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6411 | while (count) { |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6412 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6413 | size_t written_data; |
| 6414 | #endif |
| 6415 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6416 | try = b_contig_data(buf, done); |
| 6417 | if (try > count) |
| 6418 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6419 | |
Thomas Prückl | 1024393 | 2022-04-27 13:04:54 +0200 | [diff] [blame] | 6420 | if (global_ssl.hard_max_record && try > global_ssl.hard_max_record) |
| 6421 | try = global_ssl.hard_max_record; |
| 6422 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 6423 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6424 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6425 | global_ssl.max_record && try > global_ssl.max_record) { |
| 6426 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6427 | } |
| 6428 | else { |
| 6429 | /* we need to keep the information about the fact that |
| 6430 | * we're not limiting the upcoming send(), because if it |
| 6431 | * fails, we'll have to retry with at least as many data. |
| 6432 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6433 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6434 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6435 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6436 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6437 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6438 | unsigned int max_early; |
| 6439 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6440 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6441 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6442 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6443 | if (SSL_get0_session(ctx->ssl)) |
| 6444 | 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] | 6445 | else |
| 6446 | max_early = 0; |
| 6447 | } |
| 6448 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6449 | if (try + ctx->sent_early_data > max_early) { |
| 6450 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6451 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6452 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6453 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6454 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6455 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6456 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6457 | 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] | 6458 | if (ret == 1) { |
| 6459 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6460 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6461 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6462 | 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] | 6463 | /* Initiate the handshake, now */ |
| 6464 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 6465 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6466 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6467 | } |
| 6468 | |
| 6469 | } else |
| 6470 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6471 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6472 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6473 | if (conn->flags & CO_FL_ERROR) { |
| 6474 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6475 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6476 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6477 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6478 | /* A send succeeded, so we can consider ourself connected */ |
| 6479 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6480 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6481 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6482 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6483 | } |
| 6484 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6485 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6486 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6487 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6488 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6489 | /* handshake is running, and it may need to re-enable write */ |
| 6490 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6491 | 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] | 6492 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6493 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6494 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6495 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6496 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6497 | break; |
| 6498 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6499 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6500 | break; |
| 6501 | } |
| 6502 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6503 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6504 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6505 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6506 | SUB_RETRY_RECV, |
| 6507 | &ctx->wait_event); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6508 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6509 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6510 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6511 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6512 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6513 | break; |
| 6514 | } |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6515 | else if (ret == SSL_ERROR_SSL || ret == SSL_ERROR_SYSCALL) { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6516 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
| 6517 | |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 6518 | if (ctx && !ctx->error_code) |
Remi Tricot-Le Breton | 9543d5a | 2021-09-29 18:56:53 +0200 | [diff] [blame] | 6519 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6520 | conn->err_code = CO_ERR_SSL_FATAL; |
| 6521 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6522 | goto out_error; |
| 6523 | } |
| 6524 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6525 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6526 | return done; |
| 6527 | |
| 6528 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6529 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6530 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6531 | ERR_clear_error(); |
| 6532 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6533 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6534 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6535 | } |
| 6536 | |
Willy Tarreau | 832e242 | 2021-05-13 10:11:03 +0200 | [diff] [blame] | 6537 | void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6538 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6539 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6540 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6541 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6542 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6543 | if (ctx->wait_event.events != 0) |
| 6544 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 6545 | ctx->wait_event.events, |
| 6546 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6547 | if (ctx->subs) { |
| 6548 | ctx->subs->events = 0; |
| 6549 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6550 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6551 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 6552 | if (ctx->xprt->close) |
| 6553 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6554 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6555 | if (global_ssl.async) { |
| 6556 | OSSL_ASYNC_FD all_fd[32], afd; |
| 6557 | size_t num_all_fds = 0; |
| 6558 | int i; |
| 6559 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6560 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6561 | if (num_all_fds > 32) { |
| 6562 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 6563 | return; |
| 6564 | } |
| 6565 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6566 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6567 | |
| 6568 | /* If an async job is pending, we must try to |
| 6569 | to catch the end using polling before calling |
| 6570 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6571 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6572 | for (i=0 ; i < num_all_fds ; i++) { |
| 6573 | /* switch on an handler designed to |
| 6574 | * handle the SSL_free |
| 6575 | */ |
| 6576 | afd = all_fd[i]; |
| 6577 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6578 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6579 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 6580 | /* To ensure that the fd cache won't be used |
| 6581 | * and we'll catch a real RD event. |
| 6582 | */ |
| 6583 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6584 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6585 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6586 | pool_free(ssl_sock_ctx_pool, ctx); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 6587 | _HA_ATOMIC_INC(&jobs); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6588 | return; |
| 6589 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6590 | /* Else we can remove the fds from the fdtab |
| 6591 | * and call SSL_free. |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 6592 | * note: we do a fd_stop_both and not a delete |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6593 | * because the fd is owned by the engine. |
| 6594 | * the engine is responsible to close |
| 6595 | */ |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 6596 | for (i=0 ; i < num_all_fds ; i++) { |
| 6597 | /* We want to remove the fd from the fdtab |
| 6598 | * but we flag it to disown because the |
| 6599 | * close is performed by the engine itself |
| 6600 | */ |
| 6601 | fdtab[all_fd[i]].state |= FD_DISOWN; |
| 6602 | fd_delete(all_fd[i]); |
| 6603 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6604 | } |
| 6605 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6606 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6607 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6608 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6609 | pool_free(ssl_sock_ctx_pool, ctx); |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 6610 | _HA_ATOMIC_DEC(&global.sslconns); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6611 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6612 | } |
| 6613 | |
| 6614 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 6615 | * any case, flags the connection as reusable if no handshake was in progress. |
| 6616 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6617 | 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] | 6618 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6619 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6620 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6621 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6622 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 6623 | if (!clean) |
| 6624 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6625 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6626 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6627 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6628 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6629 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6630 | ERR_clear_error(); |
| 6631 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6632 | } |
| 6633 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6634 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 6635 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6636 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 6637 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6638 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6639 | X509 *crt; |
| 6640 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6641 | if (!ctx) |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6642 | return 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 6643 | crt = SSL_get_certificate(ctx->ssl); |
| 6644 | if (!crt) |
| 6645 | return 0; |
| 6646 | |
| 6647 | return cert_get_pkey_algo(crt, out); |
| 6648 | } |
| 6649 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6650 | /* used for ppv2 cert signature (can be used for logging) */ |
| 6651 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 6652 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6653 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6654 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6655 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 6656 | X509 *crt; |
| 6657 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6658 | if (!ctx) |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6659 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6660 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6661 | if (!crt) |
| 6662 | return NULL; |
| 6663 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6664 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6665 | } |
| 6666 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6667 | /* used for ppv2 authority */ |
| 6668 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6669 | { |
| 6670 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6671 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6672 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6673 | if (!ctx) |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6674 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6675 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6676 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6677 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6678 | #endif |
| 6679 | } |
| 6680 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6681 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6682 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6683 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6684 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6685 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6686 | if (!ctx) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6687 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6688 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6689 | } |
| 6690 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6691 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6692 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 6693 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6694 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6695 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6696 | if (!ctx) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6697 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6698 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6699 | } |
| 6700 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6701 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6702 | { |
| 6703 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6704 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6705 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6706 | if (!ctx) |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 6707 | return; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6708 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6709 | #endif |
| 6710 | } |
| 6711 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6712 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6713 | * to disable SNI. |
| 6714 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6715 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6716 | { |
| 6717 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6718 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6719 | struct server *s; |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6720 | char *prev_name; |
| 6721 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6722 | if (!ctx) |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6723 | return; |
Willy Tarreau | 77bfa66 | 2021-12-23 11:12:13 +0100 | [diff] [blame] | 6724 | |
| 6725 | BUG_ON(!(conn->flags & CO_FL_WAIT_L6_CONN)); |
| 6726 | BUG_ON(!(conn->flags & CO_FL_SSL_WAIT_HS)); |
| 6727 | |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6728 | s = __objt_server(conn->target); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6729 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6730 | /* 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] | 6731 | * new connection will present a new SNI. compare with the SNI |
| 6732 | * previously stored in the reused_sess */ |
| 6733 | /* the RWLOCK is used to ensure that we are not trying to flush the |
| 6734 | * cache from the CLI */ |
| 6735 | |
| 6736 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
| 6737 | prev_name = s->ssl_ctx.reused_sess[tid].sni; |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6738 | if ((!prev_name && hostname) || |
| 6739 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6740 | SSL_set_session(ctx->ssl, NULL); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 6741 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6742 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6743 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6744 | #endif |
| 6745 | } |
| 6746 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6747 | /* Extract peer certificate's common name into the chunk dest |
| 6748 | * Returns |
| 6749 | * the len of the extracted common name |
| 6750 | * or 0 if no CN found in DN |
| 6751 | * or -1 on error case (i.e. no peer certificate) |
| 6752 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6753 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6754 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6755 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6756 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6757 | X509 *crt = NULL; |
| 6758 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6759 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6760 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6761 | .area = (char *)&find_cn, |
| 6762 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6763 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6764 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6765 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6766 | if (!ctx) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6767 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6768 | |
| 6769 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6770 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6771 | if (!crt) |
| 6772 | goto out; |
| 6773 | |
| 6774 | name = X509_get_subject_name(crt); |
| 6775 | if (!name) |
| 6776 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6777 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6778 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6779 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6780 | if (crt) |
| 6781 | X509_free(crt); |
| 6782 | |
| 6783 | return result; |
| 6784 | } |
| 6785 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6786 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6787 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6788 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6789 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6790 | X509 *crt = NULL; |
| 6791 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6792 | if (!ctx) |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6793 | return 0; |
| 6794 | |
| 6795 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6796 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6797 | if (!crt) |
| 6798 | return 0; |
| 6799 | |
| 6800 | X509_free(crt); |
| 6801 | return 1; |
| 6802 | } |
| 6803 | |
| 6804 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6805 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6806 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6807 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6808 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6809 | if (!ctx) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6810 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6811 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6812 | } |
| 6813 | |
| 6814 | /* returns result from SSL verify */ |
| 6815 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6816 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6817 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6818 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6819 | if (!ctx) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6820 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6821 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6822 | } |
| 6823 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6824 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6825 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6826 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6827 | * freed by the caller. NPN is also checked if available since older versions |
| 6828 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6829 | */ |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 6830 | 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] | 6831 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6832 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6833 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6834 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6835 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6836 | return 0; |
| 6837 | |
| 6838 | *str = NULL; |
| 6839 | |
| 6840 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6841 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6842 | if (*str) |
| 6843 | return 1; |
| 6844 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6845 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6846 | 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] | 6847 | if (*str) |
| 6848 | return 1; |
| 6849 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6850 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6851 | return 0; |
| 6852 | } |
| 6853 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6854 | /* "issuers-chain-path" load chain certificate in global */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6855 | 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] | 6856 | { |
| 6857 | X509 *ca; |
| 6858 | X509_NAME *name = NULL; |
| 6859 | ASN1_OCTET_STRING *skid = NULL; |
| 6860 | STACK_OF(X509) *chain = NULL; |
| 6861 | struct issuer_chain *issuer; |
| 6862 | struct eb64_node *node; |
| 6863 | char *path; |
| 6864 | u64 key; |
| 6865 | int ret = 0; |
| 6866 | |
| 6867 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 6868 | if (chain == NULL) { |
| 6869 | chain = sk_X509_new_null(); |
| 6870 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 6871 | name = X509_get_subject_name(ca); |
| 6872 | } |
| 6873 | if (!sk_X509_push(chain, ca)) { |
| 6874 | X509_free(ca); |
| 6875 | goto end; |
| 6876 | } |
| 6877 | } |
| 6878 | if (!chain) { |
| 6879 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 6880 | goto end; |
| 6881 | } |
| 6882 | if (!skid) { |
| 6883 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 6884 | goto end; |
| 6885 | } |
| 6886 | if (!name) { |
| 6887 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 6888 | goto end; |
| 6889 | } |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 6890 | key = XXH3(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6891 | 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] | 6892 | issuer = container_of(node, typeof(*issuer), node); |
| 6893 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 6894 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 6895 | goto end; |
| 6896 | } |
| 6897 | } |
| 6898 | issuer = calloc(1, sizeof *issuer); |
| 6899 | path = strdup(fp); |
| 6900 | if (!issuer || !path) { |
| 6901 | free(issuer); |
| 6902 | free(path); |
| 6903 | goto end; |
| 6904 | } |
| 6905 | issuer->node.key = key; |
| 6906 | issuer->path = path; |
| 6907 | issuer->chain = chain; |
| 6908 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6909 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6910 | ret = 1; |
| 6911 | end: |
| 6912 | if (skid) |
| 6913 | ASN1_OCTET_STRING_free(skid); |
| 6914 | if (chain) |
| 6915 | sk_X509_pop_free(chain, X509_free); |
| 6916 | return ret; |
| 6917 | } |
| 6918 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 6919 | struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6920 | { |
| 6921 | AUTHORITY_KEYID *akid; |
| 6922 | struct issuer_chain *issuer = NULL; |
| 6923 | |
| 6924 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
William Lallemand | f69cd68 | 2020-11-19 16:24:13 +0100 | [diff] [blame] | 6925 | if (akid && akid->keyid) { |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6926 | struct eb64_node *node; |
| 6927 | u64 hk; |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 6928 | 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] | 6929 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 6930 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 6931 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 6932 | issuer = ti; |
| 6933 | break; |
| 6934 | } |
| 6935 | } |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6936 | } |
Remi Tricot-Le Breton | 1621dc1 | 2022-11-03 15:16:48 +0100 | [diff] [blame] | 6937 | AUTHORITY_KEYID_free(akid); |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 6938 | return issuer; |
| 6939 | } |
| 6940 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 6941 | void ssl_free_global_issuers(void) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6942 | { |
| 6943 | struct eb64_node *node, *back; |
| 6944 | struct issuer_chain *issuer; |
| 6945 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 6946 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 6947 | while (node) { |
| 6948 | issuer = container_of(node, typeof(*issuer), node); |
| 6949 | back = eb64_next(node); |
| 6950 | eb64_delete(node); |
| 6951 | free(issuer->path); |
| 6952 | sk_X509_pop_free(issuer->chain, X509_free); |
| 6953 | free(issuer); |
| 6954 | node = back; |
| 6955 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6956 | } |
| 6957 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 6958 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6959 | static int ssl_check_async_engine_count(void) { |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 6960 | int err_code = ERR_NONE; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6961 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6962 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 6963 | 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] | 6964 | err_code = ERR_ABORT; |
| 6965 | } |
| 6966 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 6967 | } |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 6968 | #endif |
| 6969 | |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6970 | /* "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] | 6971 | * 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] | 6972 | */ |
Willy Tarreau | 8050efe | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 6973 | 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] | 6974 | { |
| 6975 | const struct ssl_sock_ctx *sctx = ctx; |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6976 | int ret = 0; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6977 | |
| 6978 | if (!sctx) |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6979 | return ret; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6980 | |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6981 | if (sctx->conn != conn) { |
| 6982 | chunk_appendf(&trash, " xctx.conn=%p(BOGUS)", sctx->conn); |
| 6983 | ret = 1; |
| 6984 | } |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 6985 | chunk_appendf(&trash, " xctx.st=%d", sctx->xprt_st); |
| 6986 | |
| 6987 | if (sctx->xprt) { |
| 6988 | chunk_appendf(&trash, " .xprt=%s", sctx->xprt->name); |
| 6989 | if (sctx->xprt_ctx) |
| 6990 | chunk_appendf(&trash, " .xctx=%p", sctx->xprt_ctx); |
| 6991 | } |
| 6992 | |
| 6993 | chunk_appendf(&trash, " .wait.ev=%d", sctx->wait_event.events); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 6994 | |
| 6995 | /* as soon as a shutdown is reported the lower layer unregisters its |
| 6996 | * subscriber, so the situations below are transient and rare enough to |
| 6997 | * be reported as suspicious. In any case they shouldn't last. |
| 6998 | */ |
| 6999 | if ((sctx->wait_event.events & 1) && (conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_ERROR))) |
| 7000 | ret = 1; |
| 7001 | if ((sctx->wait_event.events & 2) && (conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR))) |
| 7002 | ret = 1; |
| 7003 | |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7004 | chunk_appendf(&trash, " .subs=%p", sctx->subs); |
| 7005 | if (sctx->subs) { |
| 7006 | 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] | 7007 | if (sctx->subs->tasklet->calls >= 1000000) |
| 7008 | ret = 1; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7009 | chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 7010 | sctx->subs->tasklet->calls, |
| 7011 | sctx->subs->tasklet->context); |
| 7012 | resolve_sym_name(&trash, NULL, sctx->subs->tasklet->process); |
| 7013 | chunk_appendf(&trash, ")"); |
| 7014 | } |
| 7015 | chunk_appendf(&trash, " .sent_early=%d", sctx->sent_early_data); |
| 7016 | chunk_appendf(&trash, " .early_in=%d", (int)sctx->early_buf.data); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7017 | return ret; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7018 | } |
| 7019 | |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7020 | #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] | 7021 | /* 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] | 7022 | * each reference. The variable <ref> must point to the current node's list |
| 7023 | * 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] | 7024 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7025 | static inline |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7026 | 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] | 7027 | { |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7028 | /* Get next list entry. */ |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7029 | ref = ref->n; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7030 | |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7031 | /* If the entry is the last of the list, return NULL. */ |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7032 | if (ref == end) |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7033 | return NULL; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7034 | |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7035 | return LIST_ELEM(ref, struct tls_keys_ref *, list); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7036 | } |
| 7037 | |
| 7038 | static inline |
| 7039 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 7040 | { |
| 7041 | int id; |
| 7042 | char *error; |
| 7043 | |
| 7044 | /* If the reference starts by a '#', this is numeric id. */ |
| 7045 | if (reference[0] == '#') { |
| 7046 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 7047 | id = strtol(reference + 1, &error, 10); |
| 7048 | if (*error != '\0') |
| 7049 | return NULL; |
| 7050 | |
| 7051 | /* Perform the unique id lookup. */ |
| 7052 | return tlskeys_ref_lookupid(id); |
| 7053 | } |
| 7054 | |
| 7055 | /* Perform the string lookup. */ |
| 7056 | return tlskeys_ref_lookup(reference); |
| 7057 | } |
| 7058 | #endif |
| 7059 | |
| 7060 | |
| 7061 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7062 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7063 | /* 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] | 7064 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) |
| 7065 | { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7066 | struct show_keys_ctx *ctx = appctx->svcctx; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7067 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7068 | switch (ctx->state) { |
| 7069 | case SHOW_KEYS_INIT: |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7070 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7071 | * quit the function with returning 0. The function is called |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7072 | * later and restart at the state "SHOW_KEYS_INIT". |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7073 | */ |
| 7074 | chunk_reset(&trash); |
| 7075 | |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7076 | if (ctx->dump_entries) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7077 | chunk_appendf(&trash, "# id secret\n"); |
| 7078 | else |
| 7079 | chunk_appendf(&trash, "# id (file)\n"); |
| 7080 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7081 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7082 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7083 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7084 | /* Now, we start the browsing of the references lists. |
| 7085 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 7086 | * available field of this pointer is <list>. It is used with the function |
| 7087 | * tlskeys_list_get_next() for retruning the first available entry |
| 7088 | */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7089 | if (ctx->next_ref == NULL) |
| 7090 | ctx->next_ref = tlskeys_list_get_next(&tlskeys_reference, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7091 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7092 | ctx->state = SHOW_KEYS_LIST; |
Willy Tarreau | a551f4f | 2022-11-14 07:34:43 +0100 | [diff] [blame] | 7093 | __fallthrough; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7094 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7095 | case SHOW_KEYS_LIST: |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7096 | while (ctx->next_ref) { |
| 7097 | struct tls_keys_ref *ref = ctx->next_ref; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7098 | |
| 7099 | chunk_reset(&trash); |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7100 | if (ctx->dump_entries && ctx->next_index == 0) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7101 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7102 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7103 | if (ctx->next_index == 0) |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7104 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 7105 | |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7106 | if (ctx->dump_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7107 | int head; |
| 7108 | |
| 7109 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 7110 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7111 | while (ctx->next_index < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7112 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7113 | |
| 7114 | chunk_reset(t2); |
| 7115 | /* 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] | 7116 | if (ref->key_size_bits == 128) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7117 | 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] | 7118 | sizeof(struct tls_sess_key_128), |
| 7119 | t2->area, t2->size); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7120 | 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] | 7121 | t2->area); |
| 7122 | } |
| 7123 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7124 | 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] | 7125 | sizeof(struct tls_sess_key_256), |
| 7126 | t2->area, t2->size); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7127 | 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] | 7128 | t2->area); |
| 7129 | } |
| 7130 | else { |
| 7131 | /* This case should never happen */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7132 | 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] | 7133 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7134 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7135 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7136 | /* let's try again later from this stream. We add ourselves into |
| 7137 | * this stream's users so that it can remove us upon termination. |
| 7138 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7139 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7140 | return 0; |
| 7141 | } |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7142 | ctx->next_index++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7143 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7144 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7145 | ctx->next_index = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7146 | } |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7147 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7148 | /* let's try again later from this stream. We add ourselves into |
| 7149 | * this stream's users so that it can remove us upon termination. |
| 7150 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7151 | return 0; |
| 7152 | } |
| 7153 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7154 | if (ctx->names_only == 0) /* don't display everything if not necessary */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7155 | break; |
| 7156 | |
| 7157 | /* get next list entry and check the end of the list */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7158 | ctx->next_ref = tlskeys_list_get_next(&ref->list, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7159 | } |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7160 | ctx->state = SHOW_KEYS_DONE; |
Willy Tarreau | a551f4f | 2022-11-14 07:34:43 +0100 | [diff] [blame] | 7161 | __fallthrough; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7162 | |
| 7163 | default: |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7164 | return 1; |
| 7165 | } |
| 7166 | return 0; |
| 7167 | } |
| 7168 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7169 | /* 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] | 7170 | 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] | 7171 | { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7172 | struct show_keys_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 7173 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7174 | /* no parameter, shows only file list */ |
| 7175 | if (!*args[2]) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7176 | ctx->names_only = 1; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7177 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7178 | } |
| 7179 | |
| 7180 | if (args[2][0] == '*') { |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7181 | /* list every TLS ticket keys */ |
| 7182 | ctx->names_only = 1; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7183 | } else { |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7184 | ctx->next_ref = tlskeys_ref_lookup_ref(args[2]); |
| 7185 | if (!ctx->next_ref) |
| 7186 | 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] | 7187 | } |
| 7188 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7189 | ctx->dump_entries = 1; |
| 7190 | return 0; |
| 7191 | } |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7192 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7193 | static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
| 7194 | { |
| 7195 | struct tls_keys_ref *ref; |
| 7196 | int ret; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7197 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7198 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 7199 | if (!*args[3] || !*args[4]) |
| 7200 | 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] | 7201 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7202 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 7203 | if (!ref) |
| 7204 | 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] | 7205 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7206 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
| 7207 | if (ret < 0) |
| 7208 | 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] | 7209 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7210 | trash.data = ret; |
| 7211 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 7212 | 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] | 7213 | |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7214 | 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] | 7215 | } |
Remi Tricot-Le Breton | c8d814e | 2022-12-20 11:11:17 +0100 | [diff] [blame] | 7216 | #endif |
| 7217 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7218 | |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7219 | #ifdef HAVE_SSL_PROVIDERS |
| 7220 | struct provider_name { |
| 7221 | const char *name; |
| 7222 | struct list list; |
| 7223 | }; |
| 7224 | |
| 7225 | |
| 7226 | static int ssl_provider_get_name_cb(OSSL_PROVIDER *provider, void *cbdata) |
| 7227 | { |
| 7228 | struct list *provider_names = cbdata; |
| 7229 | struct provider_name *item = NULL; |
| 7230 | const char *name = OSSL_PROVIDER_get0_name(provider); |
| 7231 | |
| 7232 | if (!provider_names) |
| 7233 | return 0; |
| 7234 | |
| 7235 | item = calloc(1, sizeof(*item)); |
| 7236 | |
| 7237 | if (!item) |
| 7238 | return 0; |
| 7239 | |
| 7240 | item->name = name; |
| 7241 | LIST_APPEND(provider_names, &item->list); |
| 7242 | |
| 7243 | return 1; |
| 7244 | } |
| 7245 | |
| 7246 | static void ssl_provider_get_name_list(struct list *provider_names) |
| 7247 | { |
| 7248 | if (!provider_names) |
| 7249 | return; |
| 7250 | |
| 7251 | OSSL_PROVIDER_do_all(NULL, ssl_provider_get_name_cb, provider_names); |
| 7252 | } |
| 7253 | |
| 7254 | static void ssl_provider_clear_name_list(struct list *provider_names) |
| 7255 | { |
| 7256 | struct provider_name *item = NULL, *item_s = NULL; |
| 7257 | |
| 7258 | if (provider_names) { |
| 7259 | list_for_each_entry_safe(item, item_s, provider_names, list) { |
| 7260 | LIST_DELETE(&item->list); |
| 7261 | free(item); |
| 7262 | } |
| 7263 | } |
| 7264 | } |
| 7265 | |
| 7266 | static int cli_io_handler_show_providers(struct appctx *appctx) |
| 7267 | { |
| 7268 | struct buffer *trash = get_trash_chunk(); |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7269 | struct list provider_names; |
| 7270 | struct provider_name *name; |
| 7271 | |
| 7272 | LIST_INIT(&provider_names); |
| 7273 | |
| 7274 | chunk_appendf(trash, "Loaded providers : \n"); |
| 7275 | |
| 7276 | ssl_provider_get_name_list(&provider_names); |
| 7277 | |
| 7278 | list_for_each_entry(name, &provider_names, list) { |
| 7279 | chunk_appendf(trash, "\t- %s\n", name->name); |
| 7280 | } |
| 7281 | |
| 7282 | ssl_provider_clear_name_list(&provider_names); |
| 7283 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7284 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7285 | goto yield; |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7286 | |
| 7287 | return 1; |
| 7288 | |
| 7289 | yield: |
| 7290 | return 0; |
| 7291 | } |
| 7292 | #endif |
Remi Tricot-Le Breton | eeaa29b | 2022-12-20 11:11:07 +0100 | [diff] [blame] | 7293 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7294 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7295 | /* register cli keywords */ |
| 7296 | static struct cli_kw_list cli_kws = {{ },{ |
| 7297 | #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] | 7298 | { { "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 }, |
| 7299 | { { "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] | 7300 | #endif |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7301 | #ifdef HAVE_SSL_PROVIDERS |
| 7302 | { { "show", "ssl", "providers", NULL }, "show ssl providers : show loaded SSL providers", NULL, cli_io_handler_show_providers }, |
| 7303 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7304 | { { NULL }, NULL, NULL, NULL } |
| 7305 | }}; |
| 7306 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7307 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7308 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 7309 | /* transport-layer operations for SSL sockets */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 7310 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7311 | .snd_buf = ssl_sock_from_buf, |
| 7312 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 7313 | .subscribe = ssl_subscribe, |
| 7314 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 7315 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 7316 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7317 | .rcv_pipe = NULL, |
| 7318 | .snd_pipe = NULL, |
| 7319 | .shutr = NULL, |
| 7320 | .shutw = ssl_sock_shutw, |
| 7321 | .close = ssl_sock_close, |
| 7322 | .init = ssl_sock_init, |
Olivier Houchard | bc5ce92 | 2021-03-05 23:47:00 +0100 | [diff] [blame] | 7323 | .start = ssl_sock_start, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 7324 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 7325 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 7326 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 7327 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7328 | .get_alpn = ssl_sock_get_alpn, |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 7329 | .takeover = ssl_takeover, |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 7330 | .set_idle = ssl_set_idle, |
| 7331 | .set_used = ssl_set_used, |
Willy Tarreau | de82795 | 2022-04-11 10:43:28 +0200 | [diff] [blame] | 7332 | .get_ssl_sock_ctx = ssl_sock_get_ctx, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 7333 | .name = "SSL", |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7334 | .show_fd = ssl_sock_show_fd, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7335 | }; |
| 7336 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7337 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 7338 | struct session *sess, struct stream *s, int flags) |
| 7339 | { |
| 7340 | struct connection *conn; |
| 7341 | |
| 7342 | conn = objt_conn(sess->origin); |
| 7343 | |
Willy Tarreau | b52d4d2 | 2022-05-27 10:44:39 +0200 | [diff] [blame] | 7344 | if (conn) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7345 | 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] | 7346 | sc_ep_set(s->scf, SE_FL_WAIT_FOR_HS); |
Christopher Faulet | 6e1bbc4 | 2022-12-12 08:08:15 +0100 | [diff] [blame] | 7347 | s->req.flags |= CF_READ_EVENT; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7348 | return ACT_RET_YIELD; |
| 7349 | } |
| 7350 | } |
| 7351 | return (ACT_RET_CONT); |
| 7352 | } |
| 7353 | |
| 7354 | 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) |
| 7355 | { |
| 7356 | rule->action_ptr = ssl_action_wait_for_hs; |
| 7357 | |
| 7358 | return ACT_RET_PRS_OK; |
| 7359 | } |
| 7360 | |
| 7361 | static struct action_kw_list http_req_actions = {ILH, { |
| 7362 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 7363 | { /* END */ } |
| 7364 | }}; |
| 7365 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7366 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 7367 | |
Ilya Shipitsin | f00cdb1 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 7368 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7369 | |
| 7370 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7371 | { |
| 7372 | if (ptr) { |
| 7373 | chunk_destroy(ptr); |
| 7374 | free(ptr); |
| 7375 | } |
| 7376 | } |
| 7377 | |
| 7378 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7379 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7380 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7381 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7382 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7383 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 7384 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7385 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 7386 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7387 | static void ssl_sock_keylog_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7388 | { |
| 7389 | struct ssl_keylog *keylog; |
| 7390 | |
| 7391 | if (!ptr) |
| 7392 | return; |
| 7393 | |
| 7394 | keylog = ptr; |
| 7395 | |
| 7396 | pool_free(pool_head_ssl_keylog_str, keylog->client_random); |
| 7397 | pool_free(pool_head_ssl_keylog_str, keylog->client_early_traffic_secret); |
| 7398 | pool_free(pool_head_ssl_keylog_str, keylog->client_handshake_traffic_secret); |
| 7399 | pool_free(pool_head_ssl_keylog_str, keylog->server_handshake_traffic_secret); |
| 7400 | pool_free(pool_head_ssl_keylog_str, keylog->client_traffic_secret_0); |
| 7401 | pool_free(pool_head_ssl_keylog_str, keylog->server_traffic_secret_0); |
| 7402 | pool_free(pool_head_ssl_keylog_str, keylog->exporter_secret); |
| 7403 | pool_free(pool_head_ssl_keylog_str, keylog->early_exporter_secret); |
| 7404 | |
| 7405 | pool_free(pool_head_ssl_keylog, ptr); |
| 7406 | } |
| 7407 | #endif |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7408 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 7409 | static void ssl_sock_clt_crt_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7410 | { |
| 7411 | if (!ptr) |
| 7412 | return; |
| 7413 | |
| 7414 | X509_free((X509*)ptr); |
| 7415 | } |
| 7416 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 7417 | static void ssl_sock_clt_sni_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 7418 | { |
| 7419 | pool_free(ssl_sock_client_sni_pool, ptr); |
| 7420 | } |
| 7421 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 7422 | static void __ssl_sock_init(void) |
| 7423 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7424 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7425 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7426 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7427 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7428 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7429 | if (global_ssl.listen_default_ciphers) |
| 7430 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 7431 | if (global_ssl.connect_default_ciphers) |
| 7432 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 7433 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7434 | if (global_ssl.listen_default_ciphersuites) |
| 7435 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 7436 | if (global_ssl.connect_default_ciphersuites) |
| 7437 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 7438 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 7439 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 7440 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7441 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7442 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7443 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7444 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7445 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7446 | n = sk_SSL_COMP_num(cm); |
| 7447 | while (n--) { |
| 7448 | (void) sk_SSL_COMP_pop(cm); |
| 7449 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 7450 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 7451 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7452 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7453 | ssl_locking_init(); |
| 7454 | #endif |
Ilya Shipitsin | f00cdb1 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 7455 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 7456 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 7457 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 7458 | |
| 7459 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 7460 | ocsp_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_ocsp_free_func); |
| 7461 | #endif |
| 7462 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 7463 | 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] | 7464 | 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] | 7465 | #ifdef USE_QUIC |
| 7466 | ssl_qc_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7467 | #endif /* USE_QUIC */ |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 7468 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 7469 | ssl_keylog_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_keylog_free_func); |
| 7470 | #endif |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 7471 | 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] | 7472 | 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] | 7473 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7474 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7475 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7476 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 7477 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7478 | hap_register_post_check(tlskeys_finalize_config); |
| 7479 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7480 | |
| 7481 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 7482 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 7483 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 7484 | hap_register_post_deinit(ssl_free_global_issuers); |
| 7485 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7486 | #ifndef OPENSSL_NO_DH |
| 7487 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 7488 | hap_register_post_deinit(ssl_free_dh); |
| 7489 | #endif |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 7490 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7491 | hap_register_post_deinit(ssl_free_engines); |
| 7492 | #endif |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 7493 | #ifdef HAVE_SSL_PROVIDERS |
| 7494 | hap_register_post_deinit(ssl_unload_providers); |
| 7495 | #endif |
Remi Tricot-Le Breton | 78a36e3 | 2022-02-11 12:04:45 +0100 | [diff] [blame] | 7496 | #if HA_OPENSSL_VERSION_NUMBER < 0x3000000fL |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7497 | /* Load SSL string for the verbose & debug mode. */ |
| 7498 | ERR_load_SSL_strings(); |
Remi Tricot-Le Breton | 78a36e3 | 2022-02-11 12:04:45 +0100 | [diff] [blame] | 7499 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 7500 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 7501 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 7502 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 7503 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 7504 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 7505 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 7506 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 7507 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 7508 | |
| 7509 | HA_SPIN_INIT(&ckch_lock); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 7510 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7511 | HA_SPIN_INIT(&ocsp_tree_lock); |
| 7512 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 7513 | /* Try to register dedicated SSL/TLS protocol message callbacks for |
| 7514 | * heartbleed attack (CVE-2014-0160) and clienthello. |
| 7515 | */ |
| 7516 | hap_register_post_check(ssl_sock_register_msg_callbacks); |
| 7517 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 7518 | /* Try to free all callbacks that were registered by using |
| 7519 | * ssl_sock_register_msg_callback(). |
| 7520 | */ |
| 7521 | hap_register_post_deinit(ssl_sock_unregister_msg_callbacks); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7522 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7523 | INITCALL0(STG_REGISTER, __ssl_sock_init); |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 7524 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7525 | /* Compute and register the version string */ |
| 7526 | static void ssl_register_build_options() |
| 7527 | { |
| 7528 | char *ptr = NULL; |
| 7529 | int i; |
| 7530 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7531 | memprintf(&ptr, "Built with OpenSSL version : " |
| 7532 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 7533 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7534 | #else /* OPENSSL_IS_BORINGSSL */ |
| 7535 | OPENSSL_VERSION_TEXT |
| 7536 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7537 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 7538 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7539 | #endif |
| 7540 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7541 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7542 | "no (library version too old)" |
| 7543 | #elif defined(OPENSSL_NO_TLSEXT) |
| 7544 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 7545 | #else |
| 7546 | "yes" |
| 7547 | #endif |
| 7548 | "", ptr); |
| 7549 | |
| 7550 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 7551 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 7552 | "yes" |
| 7553 | #else |
| 7554 | #ifdef OPENSSL_NO_TLSEXT |
| 7555 | "no (because of OPENSSL_NO_TLSEXT)" |
| 7556 | #else |
| 7557 | "no (version might be too old, 0.9.8f min needed)" |
| 7558 | #endif |
| 7559 | #endif |
| 7560 | "", ptr); |
| 7561 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 7562 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 7563 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7564 | if (methodVersions[i].option) |
| 7565 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 7566 | |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7567 | #ifdef HAVE_SSL_PROVIDERS |
| 7568 | { |
| 7569 | struct list provider_names; |
| 7570 | struct provider_name *name; |
| 7571 | LIST_INIT(&provider_names); |
| 7572 | ssl_provider_get_name_list(&provider_names); |
| 7573 | |
| 7574 | memprintf(&ptr, "%s\nOpenSSL providers loaded :", ptr); |
| 7575 | |
| 7576 | list_for_each_entry(name, &provider_names, list) { |
| 7577 | memprintf(&ptr, "%s %s", ptr, name->name); |
| 7578 | } |
| 7579 | |
| 7580 | ssl_provider_clear_name_list(&provider_names); |
| 7581 | } |
| 7582 | #endif |
| 7583 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7584 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7585 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 7586 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 7587 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 7588 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 7589 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7590 | void ssl_free_engines(void) { |
| 7591 | struct ssl_engine_list *wl, *wlb; |
| 7592 | /* free up engine list */ |
| 7593 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 7594 | ENGINE_finish(wl->e); |
| 7595 | ENGINE_free(wl->e); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 7596 | LIST_DELETE(&wl->list); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7597 | free(wl); |
| 7598 | } |
| 7599 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 7600 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7601 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 7602 | #ifdef HAVE_SSL_PROVIDERS |
| 7603 | void ssl_unload_providers(void) { |
| 7604 | struct ssl_provider_list *prov, *provb; |
| 7605 | list_for_each_entry_safe(prov, provb, &openssl_providers, list) { |
| 7606 | OSSL_PROVIDER_unload(prov->provider); |
| 7607 | LIST_DELETE(&prov->list); |
| 7608 | free(prov); |
| 7609 | } |
| 7610 | } |
| 7611 | #endif |
| 7612 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7613 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7614 | void ssl_free_dh(void) { |
| 7615 | if (local_dh_1024) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7616 | HASSL_DH_free(local_dh_1024); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7617 | local_dh_1024 = NULL; |
| 7618 | } |
| 7619 | if (local_dh_2048) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7620 | HASSL_DH_free(local_dh_2048); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7621 | local_dh_2048 = NULL; |
| 7622 | } |
| 7623 | if (local_dh_4096) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7624 | HASSL_DH_free(local_dh_4096); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7625 | local_dh_4096 = NULL; |
| 7626 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7627 | if (global_dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 7628 | HASSL_DH_free(global_dh); |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 7629 | global_dh = NULL; |
| 7630 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7631 | } |
| 7632 | #endif |
| 7633 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 7634 | static void __ssl_sock_deinit(void) |
| 7635 | { |
| 7636 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7637 | if (ssl_ctx_lru_tree) { |
| 7638 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 7639 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 7640 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7641 | #endif |
| 7642 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7643 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7644 | ERR_remove_state(0); |
| 7645 | ERR_free_strings(); |
| 7646 | |
| 7647 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7648 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7649 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7650 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7651 | CRYPTO_cleanup_all_ex_data(); |
| 7652 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 7653 | BIO_meth_free(ha_meth); |
Remi Tricot-Le Breton | 6477bbd | 2022-12-20 11:11:13 +0100 | [diff] [blame] | 7654 | |
| 7655 | #if !defined OPENSSL_NO_OCSP |
| 7656 | ssl_destroy_ocsp_update_task(); |
| 7657 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7658 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 7659 | REGISTER_POST_DEINIT(__ssl_sock_deinit); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 7660 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 7661 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7662 | /* |
| 7663 | * Local variables: |
| 7664 | * c-indent-level: 8 |
| 7665 | * c-basic-offset: 8 |
| 7666 | * End: |
| 7667 | */ |