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 | eeaa29b | 2022-12-20 11:11:07 +0100 | [diff] [blame] | 86 | #include <haproxy/http_client.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 | 6916493 | 2021-06-11 10:28:09 +0200 | [diff] [blame] | 864 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP && !defined HAVE_ASN1_TIME_TO_TM) |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 865 | /* |
| 866 | * This function returns the number of seconds elapsed |
| 867 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 868 | * date presented un ASN1_GENERALIZEDTIME. |
| 869 | * |
| 870 | * In parsing error case, it returns -1. |
| 871 | */ |
| 872 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 873 | { |
| 874 | long epoch; |
| 875 | char *p, *end; |
| 876 | const unsigned short month_offset[12] = { |
| 877 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 878 | }; |
Remi Tricot-Le Breton | a3a0cce | 2021-06-09 17:16:18 +0200 | [diff] [blame] | 879 | unsigned long year, month; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 880 | |
| 881 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 882 | |
| 883 | p = (char *)d->data; |
| 884 | end = p + d->length; |
| 885 | |
| 886 | if (end - p < 4) return -1; |
| 887 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 888 | p += 4; |
| 889 | if (end - p < 2) return -1; |
| 890 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 891 | if (month < 1 || month > 12) return -1; |
| 892 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 893 | We consider leap years and the current month (<marsh or not) */ |
| 894 | epoch = ( ((year - 1970) * 365) |
| 895 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 896 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 897 | + month_offset[month-1] |
| 898 | ) * 24 * 60 * 60; |
| 899 | p += 2; |
| 900 | if (end - p < 2) return -1; |
| 901 | /* Add the number of seconds of completed days of current month */ |
| 902 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 903 | p += 2; |
| 904 | if (end - p < 2) return -1; |
| 905 | /* Add the completed hours of the current day */ |
| 906 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 907 | p += 2; |
| 908 | if (end - p < 2) return -1; |
| 909 | /* Add the completed minutes of the current hour */ |
| 910 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 911 | p += 2; |
| 912 | if (p == end) return -1; |
| 913 | /* Test if there is available seconds */ |
| 914 | if (p[0] < '0' || p[0] > '9') |
| 915 | goto nosec; |
| 916 | if (end - p < 2) return -1; |
| 917 | /* Add the seconds of the current minute */ |
| 918 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 919 | p += 2; |
| 920 | if (p == end) return -1; |
| 921 | /* Ignore seconds float part if present */ |
| 922 | if (p[0] == '.') { |
| 923 | do { |
| 924 | if (++p == end) return -1; |
| 925 | } while (p[0] >= '0' && p[0] <= '9'); |
| 926 | } |
| 927 | |
| 928 | nosec: |
| 929 | if (p[0] == 'Z') { |
| 930 | if (end - p != 1) return -1; |
| 931 | return epoch; |
| 932 | } |
| 933 | else if (p[0] == '+') { |
| 934 | if (end - p != 5) return -1; |
| 935 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 936 | return epoch - ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 937 | } |
| 938 | else if (p[0] == '-') { |
| 939 | if (end - p != 5) return -1; |
| 940 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 941 | return epoch + ((10 * (p[1] - '0') + p[2] - '0') * 60 * 60 + (10 * (p[3] - '0') + p[4] - '0')) * 60; |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | return -1; |
| 945 | } |
Remi Tricot-Le Breton | 6916493 | 2021-06-11 10:28:09 +0200 | [diff] [blame] | 946 | #endif |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 947 | |
Remi Tricot-Le Breton | 6916493 | 2021-06-11 10:28:09 +0200 | [diff] [blame] | 948 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 949 | /* |
| 950 | * struct alignment works here such that the key.key is the same as key_data |
| 951 | * Do not change the placement of key_data |
| 952 | */ |
| 953 | struct certificate_ocsp { |
| 954 | struct ebmb_node key; |
| 955 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
Remi Tricot-Le Breton | 5aa1dce | 2021-06-10 13:51:12 +0200 | [diff] [blame] | 956 | unsigned int key_length; |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 957 | struct buffer response; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 958 | int refcount; |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 959 | long expire; |
Remi Tricot-Le Breton | bdd3c79 | 2022-12-20 11:11:09 +0100 | [diff] [blame] | 960 | struct eb64_node next_update; /* Key of items inserted in ocsp_update_tree (sorted by absolute date) */ |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 961 | }; |
| 962 | |
| 963 | struct ocsp_cbk_arg { |
| 964 | int is_single; |
| 965 | int single_kt; |
| 966 | union { |
| 967 | struct certificate_ocsp *s_ocsp; |
| 968 | /* |
| 969 | * m_ocsp will have multiple entries dependent on key type |
| 970 | * Entry 0 - DSA |
| 971 | * Entry 1 - ECDSA |
| 972 | * Entry 2 - RSA |
| 973 | */ |
| 974 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 975 | }; |
| 976 | }; |
| 977 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 978 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 979 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 980 | __decl_thread(HA_SPINLOCK_T ocsp_tree_lock); |
| 981 | |
Remi Tricot-Le Breton | bdd3c79 | 2022-12-20 11:11:09 +0100 | [diff] [blame] | 982 | static struct eb_root ocsp_update_tree = EB_ROOT; /* updatable ocsp responses sorted by next_update in absolute time */ |
| 983 | #define SSL_OCSP_UPDATE_DELAY_MAX 60*60 /* 1H */ |
| 984 | #define SSL_OCSP_UPDATE_DELAY_MIN 5*60 /* 5 minutes */ |
| 985 | #define SSL_OCSP_UPDATE_MARGIN 60 /* 1 minute */ |
| 986 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 987 | /* This function starts to check if the OCSP response (in DER format) contained |
| 988 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 989 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 990 | * contained in the OCSP Response and exits on error if no match. |
| 991 | * If it's a valid OCSP Response: |
| 992 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 993 | * pointed by 'ocsp'. |
| 994 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 995 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 996 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 997 | * already present in the container, it will be overwritten. |
| 998 | * |
| 999 | * Note: OCSP response containing more than one OCSP Single response is not |
| 1000 | * considered valid. |
| 1001 | * |
| 1002 | * Returns 0 on success, 1 in error case. |
| 1003 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1004 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 1005 | struct certificate_ocsp *ocsp, |
| 1006 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1007 | { |
| 1008 | OCSP_RESPONSE *resp; |
| 1009 | OCSP_BASICRESP *bs = NULL; |
| 1010 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1011 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1012 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1013 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 1014 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1015 | int reason; |
| 1016 | int ret = 1; |
Remi Tricot-Le Breton | 6916493 | 2021-06-11 10:28:09 +0200 | [diff] [blame] | 1017 | #ifdef HAVE_ASN1_TIME_TO_TM |
| 1018 | struct tm nextupd_tm = {0}; |
| 1019 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1020 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1021 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 1022 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1023 | if (!resp) { |
| 1024 | memprintf(err, "Unable to parse OCSP response"); |
| 1025 | goto out; |
| 1026 | } |
| 1027 | |
| 1028 | rc = OCSP_response_status(resp); |
| 1029 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 1030 | memprintf(err, "OCSP response status not successful"); |
| 1031 | goto out; |
| 1032 | } |
| 1033 | |
| 1034 | bs = OCSP_response_get1_basic(resp); |
| 1035 | if (!bs) { |
| 1036 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 1037 | goto out; |
| 1038 | } |
| 1039 | |
| 1040 | count_sr = OCSP_resp_count(bs); |
| 1041 | if (count_sr > 1) { |
| 1042 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 1043 | goto out; |
| 1044 | } |
| 1045 | |
| 1046 | sr = OCSP_resp_get0(bs, 0); |
| 1047 | if (!sr) { |
| 1048 | memprintf(err, "Failed to get OCSP single response"); |
| 1049 | goto out; |
| 1050 | } |
| 1051 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1052 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 1053 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1054 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 1055 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 1056 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1057 | goto out; |
| 1058 | } |
| 1059 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 1060 | if (!nextupd) { |
| 1061 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 1062 | goto out; |
| 1063 | } |
| 1064 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 1065 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1066 | if (!rc) { |
| 1067 | memprintf(err, "OCSP single response: no longer valid."); |
| 1068 | goto out; |
| 1069 | } |
| 1070 | |
| 1071 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1072 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1073 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 1074 | goto out; |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | if (!ocsp) { |
| 1079 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 1080 | unsigned char *p; |
| 1081 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1082 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1083 | if (!rc) { |
| 1084 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 1085 | goto out; |
| 1086 | } |
| 1087 | |
| 1088 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 1089 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 1090 | goto out; |
| 1091 | } |
| 1092 | |
| 1093 | p = key; |
| 1094 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1095 | i2d_OCSP_CERTID(id, &p); |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1096 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1097 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1098 | if (!ocsp) { |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1099 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1100 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 1101 | goto out; |
| 1102 | } |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1103 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /* According to comments on "chunk_dup", the |
| 1107 | previous chunk buffer will be freed */ |
| 1108 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 1109 | memprintf(err, "OCSP response: Memory allocation error"); |
| 1110 | goto out; |
| 1111 | } |
| 1112 | |
Remi Tricot-Le Breton | 6916493 | 2021-06-11 10:28:09 +0200 | [diff] [blame] | 1113 | #ifdef HAVE_ASN1_TIME_TO_TM |
| 1114 | if (ASN1_TIME_to_tm(nextupd, &nextupd_tm) == 0) { |
| 1115 | memprintf(err, "OCSP single response: Invalid \"Next Update\" time"); |
| 1116 | goto out; |
| 1117 | } |
| 1118 | ocsp->expire = my_timegm(&nextupd_tm) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 1119 | #else |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1120 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
Remi Tricot-Le Breton | a3a0cce | 2021-06-09 17:16:18 +0200 | [diff] [blame] | 1121 | if (ocsp->expire < 0) { |
| 1122 | memprintf(err, "OCSP single response: Invalid \"Next Update\" time"); |
| 1123 | goto out; |
| 1124 | } |
Remi Tricot-Le Breton | 6916493 | 2021-06-11 10:28:09 +0200 | [diff] [blame] | 1125 | #endif |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1126 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1127 | ret = 0; |
| 1128 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 1129 | ERR_clear_error(); |
| 1130 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1131 | if (bs) |
| 1132 | OCSP_BASICRESP_free(bs); |
| 1133 | |
| 1134 | if (resp) |
| 1135 | OCSP_RESPONSE_free(resp); |
| 1136 | |
| 1137 | return ret; |
| 1138 | } |
| 1139 | /* |
| 1140 | * External function use to update the OCSP response in the OCSP response's |
| 1141 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 1142 | * to update in DER format. |
| 1143 | * |
| 1144 | * Returns 0 on success, 1 in error case. |
| 1145 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1146 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1147 | { |
| 1148 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 1149 | } |
| 1150 | |
Remi Tricot-Le Breton | 47a4f12 | 2022-12-20 11:11:04 +0100 | [diff] [blame] | 1151 | |
| 1152 | /* |
| 1153 | * Extract the first OCSP URI (if any) contained in <cert> and write it into |
| 1154 | * <out>. |
| 1155 | * Returns 0 in case of success, 1 otherwise. |
| 1156 | */ |
| 1157 | int ssl_ocsp_get_uri_from_cert(X509 *cert, struct buffer *out, char **err) |
| 1158 | { |
| 1159 | STACK_OF(OPENSSL_STRING) *ocsp_uri_stk = NULL; |
| 1160 | int ret = 1; |
| 1161 | |
| 1162 | if (!cert || !out) |
| 1163 | goto end; |
| 1164 | |
| 1165 | ocsp_uri_stk = X509_get1_ocsp(cert); |
| 1166 | if (ocsp_uri_stk == NULL) { |
| 1167 | memprintf(err, "%sNo OCSP URL stack!\n", *err ? *err : ""); |
| 1168 | goto end; |
| 1169 | } |
| 1170 | |
| 1171 | chunk_strcpy(out, sk_OPENSSL_STRING_value(ocsp_uri_stk, 0)); |
| 1172 | if (b_data(out) == 0) { |
| 1173 | memprintf(err, "%sNo OCSP URL!\n", *err ? *err : ""); |
| 1174 | goto end; |
| 1175 | } |
| 1176 | |
| 1177 | ret = 0; |
| 1178 | |
| 1179 | end: |
| 1180 | X509_email_free(ocsp_uri_stk); |
| 1181 | return ret; |
| 1182 | } |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1183 | |
Remi Tricot-Le Breton | e09d2ae | 2022-12-20 11:11:05 +0100 | [diff] [blame] | 1184 | /* |
| 1185 | * Create the url and request body that make a proper OCSP request for the |
| 1186 | * <certid>. The <req_url> parameter should already hold the OCSP URI that was |
| 1187 | * extracted from the corresponding certificate. Depending on the size of the |
| 1188 | * certid we will either append data to the <req_url> to create a proper URL |
| 1189 | * that will be sent with a GET command, or the <req_body> will be constructed |
| 1190 | * in case of a POST. |
| 1191 | * Returns 0 in case of success. |
| 1192 | */ |
| 1193 | int ssl_ocsp_create_request_details(const OCSP_CERTID *certid, struct buffer *req_url, |
| 1194 | struct buffer *req_body, char **err) |
| 1195 | { |
| 1196 | int errcode = -1; |
| 1197 | OCSP_REQUEST *ocsp; |
| 1198 | struct buffer *bin_request = get_trash_chunk(); |
| 1199 | unsigned char *outbuf = (unsigned char*)b_orig(bin_request); |
| 1200 | |
| 1201 | ocsp = OCSP_REQUEST_new(); |
| 1202 | if (ocsp == NULL) { |
| 1203 | memprintf(err, "%sCan't create OCSP_REQUEST\n", *err ? *err : ""); |
| 1204 | goto end; |
| 1205 | } |
| 1206 | |
| 1207 | if (OCSP_request_add0_id(ocsp, (OCSP_CERTID*)certid) == NULL) { |
| 1208 | memprintf(err, "%sOCSP_request_add0_id() error\n", *err ? *err : ""); |
| 1209 | goto end; |
| 1210 | } |
| 1211 | |
| 1212 | bin_request->data = i2d_OCSP_REQUEST(ocsp, &outbuf); |
| 1213 | if (b_data(bin_request) <= 0) { |
| 1214 | memprintf(err, "%si2d_OCSP_REQUEST() error\n", *err ? *err : ""); |
| 1215 | goto end; |
| 1216 | } |
| 1217 | |
| 1218 | errcode = 0; |
| 1219 | |
| 1220 | /* HTTP based OCSP requests can use either the GET or the POST method to |
| 1221 | * submit their requests. To enable HTTP caching, small requests (that |
| 1222 | * after encoding are less than 255 bytes), MAY be submitted using GET. |
| 1223 | * If HTTP caching is not important, or the request is greater than 255 |
| 1224 | * bytes, the request SHOULD be submitted using POST. |
| 1225 | */ |
| 1226 | if (b_data(bin_request)+b_data(req_url) < 0xff) { |
| 1227 | struct buffer *b64buf = get_trash_chunk(); |
| 1228 | char *ret = NULL; |
| 1229 | int base64_ret = 0; |
| 1230 | |
| 1231 | chunk_strcat(req_url, "/"); |
| 1232 | |
| 1233 | base64_ret = a2base64(b_orig(bin_request), b_data(bin_request), |
| 1234 | b_orig(b64buf), b_size(b64buf)); |
| 1235 | |
| 1236 | if (base64_ret < 0) { |
| 1237 | memprintf(err, "%sa2base64() error\n", *err ? *err : ""); |
| 1238 | } |
| 1239 | |
| 1240 | b64buf->data = base64_ret; |
| 1241 | |
| 1242 | ret = encode_chunk((char*)b_stop(req_url), b_orig(req_url)+b_size(req_url), '%', |
| 1243 | query_encode_map, b64buf); |
| 1244 | if (ret && *ret == '\0') { |
| 1245 | req_url->data = ret-b_orig(req_url); |
| 1246 | } |
| 1247 | } |
| 1248 | else { |
| 1249 | chunk_cpy(req_body, bin_request); |
| 1250 | } |
| 1251 | |
| 1252 | end: |
| 1253 | OCSP_REQUEST_free(ocsp); |
| 1254 | |
| 1255 | return errcode; |
| 1256 | } |
| 1257 | |
Remi Tricot-Le Breton | c0b4058 | 2022-12-20 11:11:06 +0100 | [diff] [blame] | 1258 | /* |
| 1259 | * Parse an OCSP_RESPONSE contained in <respbuf> and check its validity in |
| 1260 | * regard to the contents of <ckch> or the <issuer> certificate. |
| 1261 | * Certificate_ocsp structure does not keep a reference to the corresponding |
| 1262 | * ckch_store so outside of a CLI context (see "send ssl ocsp-response" |
| 1263 | * command), we only have an easy access to the issuer's certificate whose |
| 1264 | * reference is held in the structure. |
| 1265 | * Return 0 in case of success, 1 otherwise. |
| 1266 | */ |
| 1267 | int ssl_ocsp_check_response(STACK_OF(X509) *chain, X509 *issuer, |
| 1268 | struct buffer *respbuf, char **err) |
| 1269 | { |
| 1270 | int ret = 1; |
| 1271 | int n; |
| 1272 | OCSP_RESPONSE *response = NULL; |
| 1273 | OCSP_BASICRESP *basic = NULL; |
| 1274 | X509_STORE *store = NULL; |
| 1275 | const unsigned char *start = (const unsigned char*)b_orig(respbuf); |
| 1276 | |
| 1277 | if (!chain && !issuer) { |
| 1278 | memprintf(err, "check_ocsp_response needs a certificate validation chain or an issuer certificate"); |
| 1279 | goto end; |
| 1280 | } |
| 1281 | |
| 1282 | response = d2i_OCSP_RESPONSE(NULL, &start, b_data(respbuf)); |
| 1283 | if (!response) { |
| 1284 | memprintf(err, "d2i_OCSP_RESPONSE() failed"); |
| 1285 | goto end; |
| 1286 | } |
| 1287 | |
| 1288 | n = OCSP_response_status(response); |
| 1289 | |
| 1290 | if (n != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 1291 | memprintf(err, "OCSP response not successful (%d: %s)", |
| 1292 | n, OCSP_response_status_str(n)); |
| 1293 | goto end; |
| 1294 | } |
| 1295 | |
| 1296 | basic = OCSP_response_get1_basic(response); |
| 1297 | if (basic == NULL) { |
| 1298 | memprintf(err, "OCSP_response_get1_basic() failed"); |
| 1299 | goto end; |
| 1300 | } |
| 1301 | |
| 1302 | /* Add ocsp issuer certificate to a store in order verify the ocsp |
| 1303 | * response. */ |
| 1304 | store = X509_STORE_new(); |
| 1305 | if (!store) { |
| 1306 | memprintf(err, "X509_STORE_new() failed"); |
| 1307 | goto end; |
| 1308 | } |
| 1309 | X509_STORE_add_cert(store, issuer); |
| 1310 | |
| 1311 | if (OCSP_basic_verify(basic, chain, store, 0) != 1) { |
| 1312 | memprintf(err, "OCSP_basic_verify() failed"); |
| 1313 | goto end; |
| 1314 | } |
| 1315 | |
| 1316 | ret = 0; |
| 1317 | |
| 1318 | end: |
| 1319 | X509_STORE_free(store); |
| 1320 | OCSP_RESPONSE_free(response); |
| 1321 | OCSP_BASICRESP_free(basic); |
| 1322 | return ret; |
| 1323 | } |
Remi Tricot-Le Breton | bdd3c79 | 2022-12-20 11:11:09 +0100 | [diff] [blame] | 1324 | |
| 1325 | /* |
| 1326 | * Insert a certificate_ocsp structure into the ocsp_update_tree tree, in which |
| 1327 | * entries are sorted by absolute date of the next update. The next_update key |
| 1328 | * will be the smallest out of the actual expire value of the response and |
| 1329 | * now+1H. This arbitrary 1H value ensures that ocsp responses are updated |
| 1330 | * periodically even when they have a long expire time, while not overloading |
| 1331 | * the system too much (in theory). Likewise, a minimum 5 minutes interval is |
| 1332 | * defined in order to avoid updating too often responses that have a really |
| 1333 | * short expire time or even no 'Next Update' at all. |
| 1334 | */ |
| 1335 | static int ssl_ocsp_update_insert(struct certificate_ocsp *ocsp) |
| 1336 | { |
| 1337 | int update_margin = (ocsp->expire >= SSL_OCSP_UPDATE_MARGIN) ? SSL_OCSP_UPDATE_MARGIN : 0; |
| 1338 | |
| 1339 | ocsp->next_update.key = MIN(now.tv_sec + SSL_OCSP_UPDATE_DELAY_MAX, |
| 1340 | ocsp->expire - update_margin); |
| 1341 | |
| 1342 | /* An already existing valid OCSP response that expires within less than |
| 1343 | * SSL_OCSP_UPDATE_DELAY_MIN or has no 'Next Update' field should not be |
| 1344 | * updated more than once every 5 minutes in order to avoid continuous |
| 1345 | * update of the same response. */ |
| 1346 | if (b_data(&ocsp->response)) |
| 1347 | ocsp->next_update.key = MAX(ocsp->next_update.key, SSL_OCSP_UPDATE_DELAY_MIN); |
| 1348 | |
| 1349 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
| 1350 | eb64_insert(&ocsp_update_tree, &ocsp->next_update); |
| 1351 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
| 1352 | |
| 1353 | return 0; |
| 1354 | } |
Remi Tricot-Le Breton | 47a4f12 | 2022-12-20 11:11:04 +0100 | [diff] [blame] | 1355 | #endif /* defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP */ |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 1356 | |
| 1357 | /* |
| 1358 | * Initialize an HMAC context <hctx> using the <key> and <md> parameters. |
| 1359 | * Returns -1 in case of error, 1 otherwise. |
| 1360 | */ |
| 1361 | static int ssl_hmac_init(MAC_CTX *hctx, unsigned char *key, int key_len, const EVP_MD *md) |
| 1362 | { |
| 1363 | #ifdef HAVE_OSSL_PARAM |
| 1364 | OSSL_PARAM params[3]; |
| 1365 | |
| 1366 | params[0] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, key, key_len); |
| 1367 | params[1] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST, (char*)EVP_MD_name(md), 0); |
| 1368 | params[2] = OSSL_PARAM_construct_end(); |
| 1369 | if (EVP_MAC_CTX_set_params(hctx, params) == 0) |
| 1370 | return -1; /* error in mac initialisation */ |
| 1371 | |
| 1372 | #else |
| 1373 | HMAC_Init_ex(hctx, key, key_len, md, NULL); |
| 1374 | #endif |
| 1375 | return 1; |
| 1376 | } |
| 1377 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1378 | #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] | 1379 | |
| 1380 | 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] | 1381 | { |
Frédéric Lécaille | 6aec1f3 | 2022-09-06 17:04:55 +0200 | [diff] [blame] | 1382 | struct tls_keys_ref *ref = NULL; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1383 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1384 | int head; |
| 1385 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1386 | int ret = -1; /* error by default */ |
Frédéric Lécaille | 6aec1f3 | 2022-09-06 17:04:55 +0200 | [diff] [blame] | 1387 | struct connection *conn = SSL_get_ex_data(s, ssl_app_data_index); |
| 1388 | #ifdef USE_QUIC |
| 1389 | struct quic_conn *qc = SSL_get_ex_data(s, ssl_qc_app_data_index); |
| 1390 | #endif |
| 1391 | |
| 1392 | if (conn) |
| 1393 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
| 1394 | #ifdef USE_QUIC |
| 1395 | else if (qc) |
| 1396 | ref = qc->li->bind_conf->keys_ref; |
| 1397 | #endif |
| 1398 | |
| 1399 | if (!ref) { |
| 1400 | /* must never happen */ |
| 1401 | ABORT_NOW(); |
| 1402 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1403 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1404 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1405 | |
| 1406 | keys = ref->tlskeys; |
| 1407 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1408 | |
| 1409 | if (enc) { |
| 1410 | memcpy(key_name, keys[head].name, 16); |
| 1411 | |
| 1412 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1413 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1414 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1415 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1416 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1417 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 1418 | goto end; |
| 1419 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 1420 | if (ssl_hmac_init(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0) |
| 1421 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1422 | ret = 1; |
| 1423 | } |
| 1424 | else if (ref->key_size_bits == 256 ) { |
| 1425 | |
| 1426 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 1427 | goto end; |
| 1428 | |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 1429 | if (ssl_hmac_init(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0) |
| 1430 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1431 | ret = 1; |
| 1432 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1433 | } else { |
| 1434 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1435 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1436 | goto found; |
| 1437 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1438 | ret = 0; |
| 1439 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1440 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1441 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1442 | if (ref->key_size_bits == 128) { |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 1443 | if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT()) < 0) |
| 1444 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1445 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1446 | goto end; |
| 1447 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1448 | ret = i ? 2 : 1; |
| 1449 | } |
| 1450 | else if (ref->key_size_bits == 256) { |
Remi Tricot-Le Breton | c9414e2 | 2022-02-08 17:45:59 +0100 | [diff] [blame] | 1451 | if (ssl_hmac_init(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT()) < 0) |
| 1452 | goto end; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1453 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1454 | goto end; |
| 1455 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1456 | ret = i ? 2 : 1; |
| 1457 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1458 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1459 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1460 | end: |
| 1461 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1462 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1466 | { |
| 1467 | struct tls_keys_ref *ref; |
| 1468 | |
| 1469 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1470 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1471 | return ref; |
| 1472 | return NULL; |
| 1473 | } |
| 1474 | |
| 1475 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1476 | { |
| 1477 | struct tls_keys_ref *ref; |
| 1478 | |
| 1479 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1480 | if (ref->unique_id == unique_id) |
| 1481 | return ref; |
| 1482 | return NULL; |
| 1483 | } |
| 1484 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1485 | /* Update the key into ref: if keysize doesn't |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1486 | * match existing ones, this function returns -1 |
| 1487 | * else it returns 0 on success. |
| 1488 | */ |
| 1489 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1490 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1491 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1492 | if (ref->key_size_bits == 128) { |
| 1493 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1494 | return -1; |
| 1495 | } |
| 1496 | else if (ref->key_size_bits == 256) { |
| 1497 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1498 | return -1; |
| 1499 | } |
| 1500 | else |
| 1501 | return -1; |
| 1502 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1503 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1504 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1505 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1506 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1507 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1508 | |
| 1509 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1510 | } |
| 1511 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1512 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1513 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1514 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1515 | |
| 1516 | if(!ref) { |
| 1517 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1518 | return 1; |
| 1519 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1520 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1521 | memprintf(err, "Invalid key size"); |
| 1522 | return 1; |
| 1523 | } |
| 1524 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1525 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1526 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1527 | |
| 1528 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1529 | * automatic ids. It's called just after the basic checks. It returns |
| 1530 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1531 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1532 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1533 | { |
| 1534 | int i = 0; |
| 1535 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1536 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1537 | |
| 1538 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1539 | if (ref->unique_id == -1) { |
| 1540 | /* Look for the first free id. */ |
| 1541 | while (1) { |
| 1542 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1543 | if (ref2->unique_id == i) { |
| 1544 | i++; |
| 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | if (&ref2->list == &tlskeys_reference) |
| 1549 | break; |
| 1550 | } |
| 1551 | |
| 1552 | /* Uses the unique id and increment it for the next entry. */ |
| 1553 | ref->unique_id = i; |
| 1554 | i++; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | /* This sort the reference list by id. */ |
| 1559 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1560 | LIST_DELETE(&ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1561 | list_for_each_entry(ref3, &tkr, list) { |
| 1562 | if (ref->unique_id < ref3->unique_id) { |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1563 | LIST_APPEND(&ref3->list, &ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1564 | break; |
| 1565 | } |
| 1566 | } |
| 1567 | if (&ref3->list == &tkr) |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1568 | LIST_APPEND(&tkr, &ref->list); |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | /* swap root */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 1572 | LIST_INSERT(&tkr, &tlskeys_reference); |
| 1573 | LIST_DELETE(&tkr); |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 1574 | return ERR_NONE; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1575 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1576 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1577 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1578 | #ifndef OPENSSL_NO_OCSP |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1579 | int ocsp_ex_index = -1; |
| 1580 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1581 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1582 | { |
| 1583 | switch (evp_keytype) { |
| 1584 | case EVP_PKEY_RSA: |
| 1585 | return 2; |
| 1586 | case EVP_PKEY_DSA: |
| 1587 | return 0; |
| 1588 | case EVP_PKEY_EC: |
| 1589 | return 1; |
| 1590 | } |
| 1591 | |
| 1592 | return -1; |
| 1593 | } |
| 1594 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1595 | /* |
| 1596 | * Callback used to set OCSP status extension content in server hello. |
| 1597 | */ |
| 1598 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1599 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1600 | struct certificate_ocsp *ocsp; |
| 1601 | struct ocsp_cbk_arg *ocsp_arg; |
| 1602 | char *ssl_buf; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1603 | SSL_CTX *ctx; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1604 | EVP_PKEY *ssl_pkey; |
| 1605 | int key_type; |
| 1606 | int index; |
| 1607 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1608 | ctx = SSL_get_SSL_CTX(ssl); |
| 1609 | if (!ctx) |
| 1610 | return SSL_TLSEXT_ERR_NOACK; |
| 1611 | |
| 1612 | ocsp_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
| 1613 | if (!ocsp_arg) |
| 1614 | return SSL_TLSEXT_ERR_NOACK; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1615 | |
| 1616 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1617 | if (!ssl_pkey) |
| 1618 | return SSL_TLSEXT_ERR_NOACK; |
| 1619 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1620 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1621 | |
| 1622 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1623 | ocsp = ocsp_arg->s_ocsp; |
| 1624 | else { |
| 1625 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1626 | * the certificate type |
| 1627 | */ |
| 1628 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1629 | |
| 1630 | if (index < 0) |
| 1631 | return SSL_TLSEXT_ERR_NOACK; |
| 1632 | |
| 1633 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1634 | |
| 1635 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1636 | |
| 1637 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1638 | !ocsp->response.area || |
| 1639 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1640 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1641 | return SSL_TLSEXT_ERR_NOACK; |
| 1642 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1643 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1644 | if (!ssl_buf) |
| 1645 | return SSL_TLSEXT_ERR_NOACK; |
| 1646 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1647 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1648 | SSL_set_tlsext_status_ocsp_resp(ssl, (unsigned char*)ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1649 | |
| 1650 | return SSL_TLSEXT_ERR_OK; |
| 1651 | } |
| 1652 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1653 | #endif |
| 1654 | |
Ilya Shipitsin | b3201a3 | 2020-10-18 09:11:50 +0500 | [diff] [blame] | 1655 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1656 | |
| 1657 | |
| 1658 | /* |
| 1659 | * Decrease the refcount of the struct ocsp_response and frees it if it's not |
| 1660 | * used anymore. Also removes it from the tree if free'd. |
| 1661 | */ |
| 1662 | static void ssl_sock_free_ocsp(struct certificate_ocsp *ocsp) |
| 1663 | { |
| 1664 | if (!ocsp) |
| 1665 | return; |
| 1666 | |
| 1667 | ocsp->refcount--; |
| 1668 | if (ocsp->refcount <= 0) { |
| 1669 | ebmb_delete(&ocsp->key); |
Remi Tricot-Le Breton | bdd3c79 | 2022-12-20 11:11:09 +0100 | [diff] [blame] | 1670 | eb64_delete(&ocsp->next_update); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1671 | chunk_destroy(&ocsp->response); |
Remi Tricot-Le Breton | bdd3c79 | 2022-12-20 11:11:09 +0100 | [diff] [blame] | 1672 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1673 | free(ocsp); |
| 1674 | } |
| 1675 | } |
| 1676 | |
| 1677 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1678 | /* |
| 1679 | * 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] | 1680 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1681 | * status extension, the issuer's certificate is mandatory. It should be |
| 1682 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1683 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1684 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1685 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1686 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1687 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1688 | * |
| 1689 | * 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] | 1690 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1691 | */ |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1692 | 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] | 1693 | { |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1694 | X509 *x, *issuer; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1695 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1696 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1697 | char *warn = NULL; |
| 1698 | unsigned char *p; |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1699 | #ifndef USE_OPENSSL_WOLFSSL |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1700 | void (*callback) (void); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 1701 | #else |
| 1702 | tlsextStatusCb callback; |
| 1703 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1704 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1705 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 1706 | x = data->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1707 | if (!x) |
| 1708 | goto out; |
| 1709 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 1710 | issuer = data->ocsp_issuer; |
Emmanuel Hocdet | 6f507c7 | 2020-02-18 15:56:39 +0100 | [diff] [blame] | 1711 | /* take issuer from chain over ocsp_issuer, is what is done historicaly */ |
| 1712 | if (chain) { |
| 1713 | /* check if one of the certificate of the chain is the issuer */ |
| 1714 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1715 | X509 *ti = sk_X509_value(chain, i); |
| 1716 | if (X509_check_issued(ti, x) == X509_V_OK) { |
| 1717 | issuer = ti; |
| 1718 | break; |
| 1719 | } |
| 1720 | } |
| 1721 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1722 | if (!issuer) |
| 1723 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1724 | |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1725 | data->ocsp_cid = OCSP_cert_to_id(0, x, issuer); |
| 1726 | if (!data->ocsp_cid) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1727 | goto out; |
| 1728 | |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1729 | i = i2d_OCSP_CERTID(data->ocsp_cid, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1730 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1731 | goto out; |
| 1732 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1733 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1734 | if (!ocsp) |
| 1735 | goto out; |
| 1736 | |
| 1737 | p = ocsp->key_data; |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1738 | ocsp->key_length = i2d_OCSP_CERTID(data->ocsp_cid, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1739 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1740 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1741 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1742 | if (iocsp == ocsp) |
| 1743 | ocsp = NULL; |
| 1744 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1745 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1746 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1747 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1748 | #endif |
| 1749 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1750 | |
| 1751 | if (!callback) { |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1752 | struct ocsp_cbk_arg *cb_arg; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1753 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1754 | |
William Lallemand | a560c06 | 2020-07-31 11:43:20 +0200 | [diff] [blame] | 1755 | cb_arg = calloc(1, sizeof(*cb_arg)); |
| 1756 | if (!cb_arg) |
| 1757 | goto out; |
| 1758 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1759 | cb_arg->is_single = 1; |
| 1760 | cb_arg->s_ocsp = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1761 | iocsp->refcount++; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1762 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1763 | pkey = X509_get_pubkey(x); |
| 1764 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1765 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1766 | |
| 1767 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1768 | 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 */ |
| 1769 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1770 | } else { |
| 1771 | /* |
| 1772 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1773 | * Update that cb_arg with the new cert's staple |
| 1774 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1775 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1776 | struct certificate_ocsp *tmp_ocsp; |
| 1777 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1778 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1779 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1780 | |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1781 | cb_arg = SSL_CTX_get_ex_data(ctx, ocsp_ex_index); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1782 | |
| 1783 | /* |
| 1784 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1785 | * the order of operations below matter, take care when changing it |
| 1786 | */ |
| 1787 | tmp_ocsp = cb_arg->s_ocsp; |
| 1788 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1789 | cb_arg->s_ocsp = NULL; |
| 1790 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1791 | cb_arg->is_single = 0; |
| 1792 | cb_arg->single_kt = 0; |
| 1793 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1794 | pkey = X509_get_pubkey(x); |
| 1795 | key_type = EVP_PKEY_base_id(pkey); |
| 1796 | EVP_PKEY_free(pkey); |
| 1797 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1798 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1799 | if (index >= 0 && !cb_arg->m_ocsp[index]) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1800 | cb_arg->m_ocsp[index] = iocsp; |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 1801 | iocsp->refcount++; |
| 1802 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1803 | } |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 1804 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1805 | |
| 1806 | ret = 0; |
| 1807 | |
| 1808 | warn = NULL; |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1809 | if (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] | 1810 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1811 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | out: |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1815 | if (ret && data->ocsp_cid) |
| 1816 | OCSP_CERTID_free(data->ocsp_cid); |
| 1817 | |
| 1818 | if (!ret && data->ocsp_response) { |
| 1819 | ha_free(&data->ocsp_response->area); |
| 1820 | ha_free(&data->ocsp_response); |
| 1821 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1822 | |
| 1823 | if (ocsp) |
Remi Tricot-Le Breton | aa529f7 | 2022-11-03 15:16:49 +0100 | [diff] [blame] | 1824 | ssl_sock_free_ocsp(ocsp); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1825 | |
| 1826 | if (warn) |
| 1827 | free(warn); |
| 1828 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1829 | return ret; |
| 1830 | } |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 1831 | #endif |
| 1832 | |
| 1833 | #ifdef OPENSSL_IS_BORINGSSL |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 1834 | 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] | 1835 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1836 | 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] | 1837 | } |
| 1838 | #endif |
| 1839 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1840 | |
Ilya Shipitsin | 7bbf586 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 1841 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1842 | |
| 1843 | #define CT_EXTENSION_TYPE 18 |
| 1844 | |
William Lallemand | 03c331c | 2020-05-13 10:10:01 +0200 | [diff] [blame] | 1845 | int sctl_ex_index = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1846 | |
| 1847 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1848 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1849 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1850 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1851 | *out = (unsigned char *) sctl->area; |
| 1852 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1853 | |
| 1854 | return 1; |
| 1855 | } |
| 1856 | |
| 1857 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1858 | { |
| 1859 | return 1; |
| 1860 | } |
| 1861 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1862 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1863 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1864 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1865 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1866 | 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] | 1867 | goto out; |
| 1868 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1869 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1870 | |
| 1871 | ret = 0; |
| 1872 | |
| 1873 | out: |
| 1874 | return ret; |
| 1875 | } |
| 1876 | |
| 1877 | #endif |
| 1878 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1879 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1880 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1881 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1882 | #ifdef USE_QUIC |
| 1883 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 1884 | #endif /* USE_QUIC */ |
| 1885 | struct ssl_sock_ctx *ctx = NULL; |
| 1886 | |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1887 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1888 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1889 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1890 | if (conn) |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 1891 | ctx = conn_get_ssl_sock_ctx(conn); |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1892 | #ifdef USE_QUIC |
| 1893 | else if (qc) |
| 1894 | ctx = qc->xprt_ctx; |
| 1895 | #endif /* USE_QUIC */ |
Amaury Denoyelle | 7c564bf | 2022-01-24 11:04:05 +0100 | [diff] [blame] | 1896 | |
| 1897 | if (!ctx) { |
| 1898 | /* must never happen */ |
| 1899 | ABORT_NOW(); |
| 1900 | return; |
| 1901 | } |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1902 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1903 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1904 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1905 | * change this to #if and do not assign a default value to this macro! |
| 1906 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1907 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1908 | /* Disable renegotiation (CVE-2009-3555) */ |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 1909 | 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] | 1910 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1911 | conn->err_code = CO_ER_SSL_RENEG; |
| 1912 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1913 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1914 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1915 | |
| 1916 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1917 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1918 | /* Long certificate chains optimz |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 1919 | If write and read bios are different, we |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1920 | consider that the buffering was activated, |
| 1921 | so we rise the output buffer size from 4k |
| 1922 | to 16k */ |
| 1923 | write_bio = SSL_get_wbio(ssl); |
| 1924 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1925 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1926 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1927 | } |
| 1928 | } |
| 1929 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1930 | } |
| 1931 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1932 | /* Callback is called for each certificate of the chain during a verify |
| 1933 | ok is set to 1 if preverify detect no error on current certificate. |
| 1934 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1935 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1936 | { |
| 1937 | SSL *ssl; |
| 1938 | struct connection *conn; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1939 | struct ssl_sock_ctx *ctx = NULL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1940 | int err, depth; |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1941 | X509 *client_crt; |
| 1942 | STACK_OF(X509) *certs; |
Frédéric Lécaille | 48e46f9 | 2022-10-14 09:34:00 +0200 | [diff] [blame] | 1943 | struct bind_conf *bind_conf = NULL; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1944 | struct quic_conn *qc = NULL; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1945 | |
| 1946 | 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] | 1947 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1948 | client_crt = SSL_get_ex_data(ssl, ssl_client_crt_ref_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1949 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1950 | if (conn) { |
| 1951 | bind_conf = __objt_listener(conn->target)->bind_conf; |
| 1952 | ctx = __conn_get_ssl_sock_ctx(conn); |
| 1953 | } |
| 1954 | #ifdef USE_QUIC |
| 1955 | else { |
| 1956 | qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
Amaury Denoyelle | ba303de | 2022-10-17 18:46:49 +0200 | [diff] [blame] | 1957 | BUG_ON(!qc); /* Must never happen */ |
| 1958 | bind_conf = qc->li->bind_conf; |
| 1959 | ctx = qc->xprt_ctx; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1960 | } |
| 1961 | #endif |
| 1962 | |
Frédéric Lécaille | 48e46f9 | 2022-10-14 09:34:00 +0200 | [diff] [blame] | 1963 | BUG_ON(!ctx || !bind_conf); |
Christopher Faulet | 881cce9 | 2022-11-23 09:27:13 +0100 | [diff] [blame] | 1964 | ALREADY_CHECKED(ctx); |
| 1965 | ALREADY_CHECKED(bind_conf); |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 1966 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1967 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1968 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1969 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1970 | err = X509_STORE_CTX_get_error(x_store); |
| 1971 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1972 | if (ok) /* no errors */ |
| 1973 | return ok; |
| 1974 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 1975 | /* Keep a reference to the client's certificate in order to be able to |
| 1976 | * dump some fetches values in a log even when the verification process |
| 1977 | * fails. */ |
| 1978 | if (depth == 0) { |
| 1979 | X509_free(client_crt); |
| 1980 | client_crt = X509_STORE_CTX_get0_cert(x_store); |
| 1981 | if (client_crt) { |
| 1982 | X509_up_ref(client_crt); |
| 1983 | SSL_set_ex_data(ssl, ssl_client_crt_ref_index, client_crt); |
| 1984 | } |
| 1985 | } |
| 1986 | else { |
| 1987 | /* An error occurred on a CA certificate of the certificate |
| 1988 | * chain, we might never call this verify callback on the client |
| 1989 | * certificate's depth (which is 0) so we try to store the |
| 1990 | * reference right now. */ |
Remi Tricot-Le Breton | f95c295 | 2021-08-20 09:51:23 +0200 | [diff] [blame] | 1991 | certs = X509_STORE_CTX_get1_chain(x_store); |
| 1992 | if (certs) { |
| 1993 | client_crt = sk_X509_value(certs, 0); |
| 1994 | if (client_crt) { |
| 1995 | X509_up_ref(client_crt); |
| 1996 | 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] | 1997 | } |
| 1998 | sk_X509_pop_free(certs, X509_free); |
| 1999 | } |
| 2000 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 2001 | |
| 2002 | /* check if CA error needs to be ignored */ |
| 2003 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 2004 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 2005 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 2006 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2007 | } |
| 2008 | |
Remi Tricot-Le Breton | 9b25982 | 2022-11-10 10:48:58 +0100 | [diff] [blame] | 2009 | if (err <= SSL_MAX_VFY_ERROR_CODE && |
William Lallemand | 4639689 | 2022-11-10 16:45:24 +0100 | [diff] [blame] | 2010 | 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] | 2011 | goto err_ignored; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 2012 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 2013 | /* TODO: for QUIC connection, this error code is lost */ |
| 2014 | if (conn) |
| 2015 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 2016 | return 0; |
| 2017 | } |
| 2018 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 2019 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 2020 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 2021 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 2022 | /* check if certificate error needs to be ignored */ |
Remi Tricot-Le Breton | 9b25982 | 2022-11-10 10:48:58 +0100 | [diff] [blame] | 2023 | if (err <= SSL_MAX_VFY_ERROR_CODE && |
William Lallemand | 4639689 | 2022-11-10 16:45:24 +0100 | [diff] [blame] | 2024 | 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] | 2025 | goto err_ignored; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 2026 | |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 2027 | /* TODO: for QUIC connection, this error code is lost */ |
| 2028 | if (conn) |
| 2029 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 2030 | return 0; |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 2031 | |
| 2032 | err_ignored: |
| 2033 | ssl_sock_dump_errors(conn, qc); |
| 2034 | ERR_clear_error(); |
| 2035 | return 1; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 2036 | } |
| 2037 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 2038 | #ifdef TLS1_RT_HEARTBEAT |
| 2039 | static void ssl_sock_parse_heartbeat(struct connection *conn, int write_p, int version, |
| 2040 | int content_type, const void *buf, size_t len, |
| 2041 | SSL *ssl) |
| 2042 | { |
| 2043 | /* test heartbeat received (write_p is set to 0 |
| 2044 | for a received record) */ |
| 2045 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 2046 | struct ssl_sock_ctx *ctx = __conn_get_ssl_sock_ctx(conn); |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 2047 | const unsigned char *p = buf; |
| 2048 | unsigned int payload; |
| 2049 | |
| 2050 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
| 2051 | |
| 2052 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 2053 | if (*p != TLS1_HB_REQUEST) |
| 2054 | return; |
| 2055 | |
| 2056 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
| 2057 | goto kill_it; |
| 2058 | |
| 2059 | payload = (p[1] * 256) + p[2]; |
| 2060 | if (3 + payload + 16 <= len) |
| 2061 | return; /* OK no problem */ |
| 2062 | kill_it: |
| 2063 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 2064 | * advertised payload is larger than the advertised packet |
| 2065 | * length, so we have garbage in the buffer between the |
| 2066 | * payload and the end of the buffer (p+len). We can't know |
| 2067 | * if the SSL stack is patched, and we don't know if we can |
| 2068 | * safely wipe out the area between p+3+len and payload. |
| 2069 | * So instead, we prevent the response from being sent by |
| 2070 | * setting the max_send_fragment to 0 and we report an SSL |
| 2071 | * error, which will kill this connection. It will be reported |
| 2072 | * above as SSL_ERROR_SSL while an other handshake failure with |
| 2073 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 2074 | */ |
| 2075 | ssl->max_send_fragment = 0; |
| 2076 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 2077 | } |
| 2078 | } |
| 2079 | #endif |
| 2080 | |
| 2081 | static void ssl_sock_parse_clienthello(struct connection *conn, int write_p, int version, |
| 2082 | int content_type, const void *buf, size_t len, |
| 2083 | SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2084 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 2085 | struct ssl_capture *capture; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2086 | uchar *msg; |
| 2087 | uchar *end; |
| 2088 | uchar *extensions_end; |
| 2089 | uchar *ec_start = NULL; |
| 2090 | uchar *ec_formats_start = NULL; |
| 2091 | uchar *list_end; |
| 2092 | ushort protocol_version; |
| 2093 | ushort extension_id; |
| 2094 | ushort ec_len = 0; |
| 2095 | uchar ec_formats_len = 0; |
| 2096 | int offset = 0; |
| 2097 | int rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2098 | |
| 2099 | /* This function is called for "from client" and "to server" |
| 2100 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 2101 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2102 | */ |
| 2103 | |
| 2104 | /* "write_p" is set to 0 is the bytes are received messages, |
| 2105 | * otherwise it is set to 1. |
| 2106 | */ |
| 2107 | if (write_p != 0) |
| 2108 | return; |
| 2109 | |
| 2110 | /* content_type contains the type of message received or sent |
| 2111 | * according with the SSL/TLS protocol spec. This message is |
| 2112 | * encoded with one byte. The value 256 (two bytes) is used |
| 2113 | * for designing the SSL/TLS record layer. According with the |
| 2114 | * rfc6101, the expected message (other than 256) are: |
| 2115 | * - change_cipher_spec(20) |
| 2116 | * - alert(21) |
| 2117 | * - handshake(22) |
| 2118 | * - application_data(23) |
| 2119 | * - (255) |
| 2120 | * We are interessed by the handshake and specially the client |
| 2121 | * hello. |
| 2122 | */ |
| 2123 | if (content_type != 22) |
| 2124 | return; |
| 2125 | |
| 2126 | /* The message length is at least 4 bytes, containing the |
| 2127 | * message type and the message length. |
| 2128 | */ |
| 2129 | if (len < 4) |
| 2130 | return; |
| 2131 | |
| 2132 | /* First byte of the handshake message id the type of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2133 | * message. The known types are: |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2134 | * - hello_request(0) |
| 2135 | * - client_hello(1) |
| 2136 | * - server_hello(2) |
| 2137 | * - certificate(11) |
| 2138 | * - server_key_exchange (12) |
| 2139 | * - certificate_request(13) |
| 2140 | * - server_hello_done(14) |
| 2141 | * We are interested by the client hello. |
| 2142 | */ |
| 2143 | msg = (unsigned char *)buf; |
| 2144 | if (msg[0] != 1) |
| 2145 | return; |
| 2146 | |
| 2147 | /* Next three bytes are the length of the message. The total length |
| 2148 | * must be this decoded length + 4. If the length given as argument |
| 2149 | * is not the same, we abort the protocol dissector. |
| 2150 | */ |
| 2151 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 2152 | if (len < rec_len + 4) |
| 2153 | return; |
| 2154 | msg += 4; |
| 2155 | end = msg + rec_len; |
| 2156 | if (end < msg) |
| 2157 | return; |
| 2158 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2159 | /* Expect 2 bytes for protocol version |
| 2160 | * (1 byte for major and 1 byte for minor) |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 2161 | */ |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2162 | if (msg + 2 > end) |
| 2163 | return; |
| 2164 | protocol_version = (msg[0] << 8) + msg[1]; |
| 2165 | msg += 2; |
| 2166 | |
| 2167 | /* Expect the random, composed by 4 bytes for the unix time and |
| 2168 | * 28 bytes for unix payload. So we jump 4 + 28. |
| 2169 | */ |
| 2170 | msg += 4 + 28; |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 2171 | if (msg > end) |
| 2172 | return; |
| 2173 | |
| 2174 | /* Next, is session id: |
| 2175 | * if present, we have to jump by length + 1 for the size information |
| 2176 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2177 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 2178 | if (msg[0] > 0) |
| 2179 | msg += msg[0]; |
| 2180 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2181 | if (msg > end) |
| 2182 | return; |
| 2183 | |
| 2184 | /* Next two bytes are the ciphersuite length. */ |
| 2185 | if (msg + 2 > end) |
| 2186 | return; |
| 2187 | rec_len = (msg[0] << 8) + msg[1]; |
| 2188 | msg += 2; |
| 2189 | if (msg + rec_len > end || msg + rec_len < msg) |
| 2190 | return; |
| 2191 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2192 | capture = pool_zalloc(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 2193 | if (!capture) |
| 2194 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2195 | /* Compute the xxh64 of the ciphersuite. */ |
| 2196 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 2197 | |
| 2198 | /* Capture the ciphersuite. */ |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 2199 | capture->ciphersuite_len = MIN(global_ssl.capture_buffer_size, rec_len); |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2200 | capture->ciphersuite_offset = 0; |
| 2201 | memcpy(capture->data, msg, capture->ciphersuite_len); |
| 2202 | msg += rec_len; |
| 2203 | offset += capture->ciphersuite_len; |
| 2204 | |
| 2205 | /* Initialize other data */ |
| 2206 | capture->protocol_version = protocol_version; |
| 2207 | |
| 2208 | /* Next, compression methods: |
| 2209 | * if present, we have to jump by length + 1 for the size information |
| 2210 | * if not present, we have to jump by 1 only |
| 2211 | */ |
| 2212 | if (msg[0] > 0) |
| 2213 | msg += msg[0]; |
| 2214 | msg += 1; |
| 2215 | if (msg > end) |
| 2216 | goto store_capture; |
| 2217 | |
| 2218 | /* We reached extensions */ |
| 2219 | if (msg + 2 > end) |
| 2220 | goto store_capture; |
| 2221 | rec_len = (msg[0] << 8) + msg[1]; |
| 2222 | msg += 2; |
| 2223 | if (msg + rec_len > end || msg + rec_len < msg) |
| 2224 | goto store_capture; |
| 2225 | extensions_end = msg + rec_len; |
| 2226 | capture->extensions_offset = offset; |
| 2227 | |
| 2228 | /* Parse each extension */ |
| 2229 | while (msg + 4 < extensions_end) { |
| 2230 | /* Add 2 bytes of extension_id */ |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 2231 | if (global_ssl.capture_buffer_size >= offset + 2) { |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2232 | capture->data[offset++] = msg[0]; |
| 2233 | capture->data[offset++] = msg[1]; |
| 2234 | capture->extensions_len += 2; |
| 2235 | } |
| 2236 | else |
| 2237 | break; |
| 2238 | extension_id = (msg[0] << 8) + msg[1]; |
| 2239 | /* Length of the extension */ |
| 2240 | rec_len = (msg[2] << 8) + msg[3]; |
| 2241 | |
| 2242 | /* Expect 2 bytes extension id + 2 bytes extension size */ |
| 2243 | msg += 2 + 2; |
| 2244 | if (msg + rec_len > extensions_end || msg + rec_len < msg) |
| 2245 | goto store_capture; |
| 2246 | /* TLS Extensions |
| 2247 | * https://www.iana.org/assignments/tls-extensiontype-values/tls-extensiontype-values.xhtml */ |
| 2248 | if (extension_id == 0x000a) { |
| 2249 | /* Elliptic Curves: |
| 2250 | * https://www.rfc-editor.org/rfc/rfc8422.html |
| 2251 | * https://www.rfc-editor.org/rfc/rfc7919.html */ |
| 2252 | list_end = msg + rec_len; |
| 2253 | if (msg + 2 > list_end) |
| 2254 | goto store_capture; |
| 2255 | rec_len = (msg[0] << 8) + msg[1]; |
| 2256 | msg += 2; |
| 2257 | |
| 2258 | if (msg + rec_len > list_end || msg + rec_len < msg) |
| 2259 | goto store_capture; |
| 2260 | /* Store location/size of the list */ |
| 2261 | ec_start = msg; |
| 2262 | ec_len = rec_len; |
| 2263 | } |
| 2264 | else if (extension_id == 0x000b) { |
| 2265 | /* Elliptic Curves Point Formats: |
| 2266 | * https://www.rfc-editor.org/rfc/rfc8422.html */ |
| 2267 | list_end = msg + rec_len; |
| 2268 | if (msg + 1 > list_end) |
| 2269 | goto store_capture; |
| 2270 | rec_len = msg[0]; |
| 2271 | msg += 1; |
| 2272 | |
| 2273 | if (msg + rec_len > list_end || msg + rec_len < msg) |
| 2274 | goto store_capture; |
| 2275 | /* Store location/size of the list */ |
| 2276 | ec_formats_start = msg; |
| 2277 | ec_formats_len = rec_len; |
| 2278 | } |
| 2279 | msg += rec_len; |
| 2280 | } |
| 2281 | |
| 2282 | if (ec_start) { |
| 2283 | rec_len = ec_len; |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 2284 | if (offset + rec_len > global_ssl.capture_buffer_size) |
| 2285 | rec_len = global_ssl.capture_buffer_size - offset; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2286 | memcpy(capture->data + offset, ec_start, rec_len); |
| 2287 | capture->ec_offset = offset; |
| 2288 | capture->ec_len = rec_len; |
| 2289 | offset += rec_len; |
| 2290 | } |
| 2291 | if (ec_formats_start) { |
| 2292 | rec_len = ec_formats_len; |
Marcin Deranek | 310a260 | 2021-07-13 19:04:24 +0200 | [diff] [blame] | 2293 | if (offset + rec_len > global_ssl.capture_buffer_size) |
| 2294 | rec_len = global_ssl.capture_buffer_size - offset; |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2295 | memcpy(capture->data + offset, ec_formats_start, rec_len); |
| 2296 | capture->ec_formats_offset = offset; |
| 2297 | capture->ec_formats_len = rec_len; |
| 2298 | offset += rec_len; |
| 2299 | } |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 2300 | |
Marcin Deranek | 769fd2e | 2021-07-12 14:16:55 +0200 | [diff] [blame] | 2301 | store_capture: |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 2302 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2303 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 2304 | |
| 2305 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 2306 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 2307 | static void ssl_init_keylog(struct connection *conn, int write_p, int version, |
| 2308 | int content_type, const void *buf, size_t len, |
| 2309 | SSL *ssl) |
| 2310 | { |
| 2311 | struct ssl_keylog *keylog; |
| 2312 | |
| 2313 | if (SSL_get_ex_data(ssl, ssl_keylog_index)) |
| 2314 | return; |
| 2315 | |
Willy Tarreau | f208ac0 | 2021-03-22 21:10:12 +0100 | [diff] [blame] | 2316 | keylog = pool_zalloc(pool_head_ssl_keylog); |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 2317 | if (!keylog) |
| 2318 | return; |
| 2319 | |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 2320 | if (!SSL_set_ex_data(ssl, ssl_keylog_index, keylog)) { |
| 2321 | pool_free(pool_head_ssl_keylog, keylog); |
| 2322 | return; |
| 2323 | } |
| 2324 | } |
| 2325 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 2326 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 2327 | /* Callback is called for ssl protocol analyse */ |
| 2328 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 2329 | { |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 2330 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 2331 | struct ssl_sock_msg_callback *cbk; |
| 2332 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 2333 | /* Try to call all callback functions that were registered by using |
| 2334 | * ssl_sock_register_msg_callback(). |
| 2335 | */ |
| 2336 | list_for_each_entry(cbk, &ssl_sock_msg_callbacks, list) { |
| 2337 | cbk->func(conn, write_p, version, content_type, buf, len, ssl); |
| 2338 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 2339 | } |
| 2340 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 2341 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 2342 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 2343 | const unsigned char *in, unsigned int inlen, |
| 2344 | void *arg) |
| 2345 | { |
| 2346 | struct server *srv = arg; |
| 2347 | |
| 2348 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 2349 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 2350 | return SSL_TLSEXT_ERR_OK; |
| 2351 | return SSL_TLSEXT_ERR_NOACK; |
| 2352 | } |
| 2353 | #endif |
| 2354 | |
| 2355 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2356 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2357 | * negotiable protocols for NPN. |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2358 | */ |
| 2359 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 2360 | unsigned int *len, void *arg) |
| 2361 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2362 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 2363 | |
| 2364 | *data = (const unsigned char *)conf->npn_str; |
| 2365 | *len = conf->npn_len; |
| 2366 | return SSL_TLSEXT_ERR_OK; |
| 2367 | } |
| 2368 | #endif |
| 2369 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2370 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2371 | /* This callback is used so that the server advertises the list of |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2372 | * negotiable protocols for ALPN. |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2373 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2374 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 2375 | unsigned char *outlen, |
| 2376 | const unsigned char *server, |
| 2377 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2378 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2379 | struct ssl_bind_conf *conf = arg; |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 2380 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2381 | 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] | 2382 | #endif |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2383 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2384 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 2385 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
Frédéric Lécaille | 067a82b | 2021-11-19 17:02:20 +0100 | [diff] [blame] | 2386 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2387 | if (qc) |
| 2388 | 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] | 2389 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 2390 | return SSL_TLSEXT_ERR_NOACK; |
| 2391 | } |
Frédéric Lécaille | 1761fdf | 2021-12-14 19:40:04 +0100 | [diff] [blame] | 2392 | |
| 2393 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2394 | if (qc && !quic_set_app_ops(qc, *out, *outlen)) { |
| 2395 | 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] | 2396 | return SSL_TLSEXT_ERR_NOACK; |
| 2397 | } |
| 2398 | #endif |
| 2399 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 2400 | return SSL_TLSEXT_ERR_OK; |
| 2401 | } |
| 2402 | #endif |
| 2403 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 2404 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2405 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2406 | |
cui fliter | a94bedc | 2022-08-29 14:42:57 +0800 | [diff] [blame] | 2407 | /* Configure a DNS SAN extension on a certificate. */ |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 2408 | int ssl_sock_add_san_ext(X509V3_CTX* ctx, X509* cert, const char *servername) { |
| 2409 | int failure = 0; |
| 2410 | X509_EXTENSION *san_ext = NULL; |
| 2411 | CONF *conf = NULL; |
| 2412 | struct buffer *san_name = get_trash_chunk(); |
| 2413 | |
| 2414 | conf = NCONF_new(NULL); |
| 2415 | if (!conf) { |
| 2416 | failure = 1; |
| 2417 | goto cleanup; |
| 2418 | } |
| 2419 | |
| 2420 | /* Build an extension based on the DNS entry above */ |
| 2421 | chunk_appendf(san_name, "DNS:%s", servername); |
| 2422 | san_ext = X509V3_EXT_nconf_nid(conf, ctx, NID_subject_alt_name, san_name->area); |
| 2423 | if (!san_ext) { |
| 2424 | failure = 1; |
| 2425 | goto cleanup; |
| 2426 | } |
| 2427 | |
| 2428 | /* Add the extension */ |
| 2429 | if (!X509_add_ext(cert, san_ext, -1 /* Add to end */)) { |
| 2430 | failure = 1; |
| 2431 | goto cleanup; |
| 2432 | } |
| 2433 | |
| 2434 | /* Success */ |
| 2435 | failure = 0; |
| 2436 | |
| 2437 | cleanup: |
| 2438 | if (NULL != san_ext) X509_EXTENSION_free(san_ext); |
| 2439 | if (NULL != conf) NCONF_free(conf); |
| 2440 | |
| 2441 | return failure; |
| 2442 | } |
| 2443 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2444 | /* Create a X509 certificate with the specified servername and serial. This |
| 2445 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2446 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2447 | 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] | 2448 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2449 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
| 2450 | EVP_PKEY *capkey = bind_conf->ca_sign_ckch->key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2451 | SSL_CTX *ssl_ctx = NULL; |
| 2452 | X509 *newcrt = NULL; |
| 2453 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2454 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2455 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2456 | X509_NAME *name; |
| 2457 | const EVP_MD *digest; |
| 2458 | X509V3_CTX ctx; |
| 2459 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2460 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2461 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 2462 | /* Get the private key of the default certificate and use it */ |
Ilya Shipitsin | af20488 | 2020-12-19 03:12:12 +0500 | [diff] [blame] | 2463 | #ifdef HAVE_SSL_CTX_get0_privatekey |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2464 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 2465 | #else |
| 2466 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 2467 | if (tmp_ssl) |
| 2468 | pkey = SSL_get_privatekey(tmp_ssl); |
| 2469 | #endif |
| 2470 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2471 | goto mkcert_error; |
| 2472 | |
| 2473 | /* Create the certificate */ |
| 2474 | if (!(newcrt = X509_new())) |
| 2475 | goto mkcert_error; |
| 2476 | |
| 2477 | /* Set version number for the certificate (X509v3) and the serial |
| 2478 | * number */ |
| 2479 | if (X509_set_version(newcrt, 2L) != 1) |
| 2480 | goto mkcert_error; |
Willy Tarreau | 1db4273 | 2021-04-06 11:44:07 +0200 | [diff] [blame] | 2481 | 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] | 2482 | |
| 2483 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 2484 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 2485 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2486 | goto mkcert_error; |
| 2487 | |
| 2488 | /* set public key in the certificate */ |
| 2489 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 2490 | goto mkcert_error; |
| 2491 | |
| 2492 | /* Set issuer name from the CA */ |
| 2493 | if (!(name = X509_get_subject_name(cacert))) |
| 2494 | goto mkcert_error; |
| 2495 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 2496 | goto mkcert_error; |
| 2497 | |
| 2498 | /* Set the subject name using the same, but the CN */ |
| 2499 | name = X509_NAME_dup(name); |
| 2500 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 2501 | (const unsigned char *)servername, |
| 2502 | -1, -1, 0) != 1) { |
| 2503 | X509_NAME_free(name); |
| 2504 | goto mkcert_error; |
| 2505 | } |
| 2506 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 2507 | X509_NAME_free(name); |
| 2508 | goto mkcert_error; |
| 2509 | } |
| 2510 | X509_NAME_free(name); |
| 2511 | |
| 2512 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2513 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2514 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 2515 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 2516 | X509_EXTENSION *ext; |
| 2517 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2518 | 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] | 2519 | goto mkcert_error; |
| 2520 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 2521 | X509_EXTENSION_free(ext); |
| 2522 | goto mkcert_error; |
| 2523 | } |
| 2524 | X509_EXTENSION_free(ext); |
| 2525 | } |
| 2526 | |
Shimi Gersner | adabbfe | 2020-08-23 13:58:13 +0300 | [diff] [blame] | 2527 | /* Add SAN extension */ |
| 2528 | if (ssl_sock_add_san_ext(&ctx, newcrt, servername)) { |
| 2529 | goto mkcert_error; |
| 2530 | } |
| 2531 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2532 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2533 | |
| 2534 | key_type = EVP_PKEY_base_id(capkey); |
| 2535 | |
| 2536 | if (key_type == EVP_PKEY_DSA) |
| 2537 | digest = EVP_sha1(); |
| 2538 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2539 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2540 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2541 | digest = EVP_sha256(); |
| 2542 | else { |
Ilya Shipitsin | ec36c91 | 2021-01-07 11:57:42 +0500 | [diff] [blame] | 2543 | #ifdef ASN1_PKEY_CTRL_DEFAULT_MD_NID |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2544 | int nid; |
| 2545 | |
| 2546 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 2547 | goto mkcert_error; |
| 2548 | if (!(digest = EVP_get_digestbynid(nid))) |
| 2549 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 2550 | #else |
| 2551 | goto mkcert_error; |
| 2552 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2553 | } |
| 2554 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2555 | if (!(X509_sign(newcrt, capkey, digest))) |
| 2556 | goto mkcert_error; |
| 2557 | |
| 2558 | /* Create and set the new SSL_CTX */ |
| 2559 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 2560 | goto mkcert_error; |
| 2561 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 2562 | goto mkcert_error; |
| 2563 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 2564 | goto mkcert_error; |
| 2565 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 2566 | goto mkcert_error; |
| 2567 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2568 | /* Build chaining the CA cert and the rest of the chain, keep these order */ |
| 2569 | #if defined(SSL_CTX_add1_chain_cert) |
| 2570 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, bind_conf->ca_sign_ckch->cert)) { |
| 2571 | goto mkcert_error; |
| 2572 | } |
| 2573 | |
| 2574 | if (bind_conf->ca_sign_ckch->chain) { |
| 2575 | for (i = 0; i < sk_X509_num(bind_conf->ca_sign_ckch->chain); i++) { |
| 2576 | X509 *chain_cert = sk_X509_value(bind_conf->ca_sign_ckch->chain, i); |
| 2577 | if (!SSL_CTX_add1_chain_cert(ssl_ctx, chain_cert)) { |
| 2578 | goto mkcert_error; |
| 2579 | } |
| 2580 | } |
| 2581 | } |
| 2582 | #endif |
| 2583 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2584 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2585 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2586 | #ifndef OPENSSL_NO_DH |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 2587 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 2588 | 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] | 2589 | #else |
| 2590 | ssl_sock_set_tmp_dh_from_pkey(ssl_ctx, pkey); |
| 2591 | #endif |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 2592 | #endif |
Remi Tricot-Le Breton | c11e7e1 | 2022-02-08 17:45:56 +0100 | [diff] [blame] | 2593 | |
| 2594 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
| 2595 | #if defined(SSL_CTX_set1_curves_list) |
| 2596 | { |
| 2597 | const char *ecdhe = (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : ECDHE_DEFAULT_CURVE); |
| 2598 | if (!SSL_CTX_set1_curves_list(ssl_ctx, ecdhe)) |
| 2599 | goto end; |
| 2600 | } |
| 2601 | #endif |
| 2602 | #else |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2603 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 2604 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2605 | 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] | 2606 | EC_KEY *ecc; |
| 2607 | int nid; |
| 2608 | |
| 2609 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 2610 | goto end; |
| 2611 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 2612 | goto end; |
| 2613 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 2614 | EC_KEY_free(ecc); |
| 2615 | } |
Remi Tricot-Le Breton | c11e7e1 | 2022-02-08 17:45:56 +0100 | [diff] [blame] | 2616 | #endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */ |
| 2617 | #endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */ |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 2618 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2619 | return ssl_ctx; |
| 2620 | |
| 2621 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 2622 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 2623 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2624 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 2625 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2626 | return NULL; |
| 2627 | } |
| 2628 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2629 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2630 | /* 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] | 2631 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2632 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2633 | 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] | 2634 | { |
| 2635 | struct lru64 *lru = NULL; |
| 2636 | |
| 2637 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2638 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2639 | 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] | 2640 | if (lru && lru->domain) { |
| 2641 | if (ssl) |
| 2642 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2643 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2644 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2645 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2646 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2647 | } |
| 2648 | return NULL; |
| 2649 | } |
| 2650 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2651 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2652 | * function is not thread-safe, it should only be used to check if a certificate |
| 2653 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2654 | * thread). It is kept for backward compatibility. */ |
| 2655 | SSL_CTX * |
| 2656 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2657 | { |
| 2658 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2659 | } |
| 2660 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2661 | /* Set a certificate int the LRU cache used to store generated |
| 2662 | * certificate. Return 0 on success, otherwise -1 */ |
| 2663 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2664 | 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] | 2665 | { |
| 2666 | struct lru64 *lru = NULL; |
| 2667 | |
| 2668 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2669 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2670 | 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] | 2671 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2672 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2673 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2674 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2675 | if (lru->domain && lru->data) |
| 2676 | lru->free((SSL_CTX *)lru->data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2677 | 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] | 2678 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2679 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2680 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2681 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2682 | } |
| 2683 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2684 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2685 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2686 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2687 | { |
| 2688 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2689 | } |
| 2690 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2691 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2692 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2693 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2694 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2695 | 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] | 2696 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 2697 | X509 *cacert = bind_conf->ca_sign_ckch->cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2698 | SSL_CTX *ssl_ctx = NULL; |
| 2699 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2700 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2701 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2702 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2703 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2704 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2705 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2706 | if (lru && lru->domain) |
| 2707 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2708 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2709 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2710 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2711 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2712 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2713 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2714 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2715 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2716 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2717 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2718 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2719 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2720 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2721 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2722 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2723 | return 0; |
| 2724 | } |
| 2725 | static int |
| 2726 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2727 | { |
| 2728 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2729 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2730 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2731 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2732 | 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] | 2733 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2734 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2735 | } |
| 2736 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2737 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2738 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2739 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2740 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2741 | |
| 2742 | 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] | 2743 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2744 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2745 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2746 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2747 | #endif |
| 2748 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2749 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2750 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2751 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2752 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2753 | 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] | 2754 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2755 | 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] | 2756 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2757 | #endif |
| 2758 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2759 | 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] | 2760 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2761 | 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] | 2762 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2763 | #endif |
| 2764 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2765 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2766 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2767 | /* Unusable in this context. */ |
| 2768 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2769 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2770 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2771 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2772 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2773 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2774 | |
| 2775 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2776 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2777 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2778 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2779 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2780 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2781 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2782 | } |
| 2783 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2784 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2785 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2786 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2787 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2788 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2789 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2790 | } |
| 2791 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2792 | 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] | 2793 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2794 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2795 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2796 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2797 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2798 | } |
| 2799 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2800 | 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] | 2801 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2802 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2803 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2804 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2805 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2806 | } |
| 2807 | 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] | 2808 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2809 | 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] | 2810 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2811 | #endif |
| 2812 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2813 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
William Lallemand | f22b032 | 2021-06-02 16:09:11 +0200 | [diff] [blame] | 2814 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2815 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2816 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2817 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2818 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2819 | #endif |
| 2820 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2821 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2822 | |
William Lallemand | 7fd8b45 | 2020-05-07 15:20:43 +0200 | [diff] [blame] | 2823 | struct methodVersions methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2824 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2825 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2826 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2827 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2828 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2829 | {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] | 2830 | }; |
| 2831 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2832 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2833 | { |
| 2834 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2835 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2836 | SSL_set_SSL_CTX(ssl, ctx); |
| 2837 | } |
| 2838 | |
Ilya Shipitsin | 1fc44d4 | 2021-01-23 00:09:14 +0500 | [diff] [blame] | 2839 | #ifdef HAVE_SSL_CLIENT_HELLO_CB |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2840 | |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2841 | int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2842 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2843 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2844 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2845 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2846 | 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] | 2847 | return SSL_TLSEXT_ERR_OK; |
| 2848 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2849 | } |
| 2850 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2851 | #ifdef OPENSSL_IS_BORINGSSL |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2852 | int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2853 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2854 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2855 | #else |
Frédéric Lécaille | 901ee2f | 2020-11-23 11:19:04 +0100 | [diff] [blame] | 2856 | int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2857 | { |
| 2858 | #endif |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2859 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
| 2860 | #ifdef USE_QUIC |
| 2861 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 2862 | #endif /* USE_QUIC */ |
| 2863 | struct bind_conf *s = NULL; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2864 | const uint8_t *extension_data; |
| 2865 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2866 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2867 | |
| 2868 | char *wildp = NULL; |
| 2869 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2870 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2871 | 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] | 2872 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2873 | int i; |
| 2874 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2875 | if (conn) |
| 2876 | s = __objt_listener(conn->target)->bind_conf; |
| 2877 | #ifdef USE_QUIC |
| 2878 | else if (qc) |
| 2879 | s = qc->li->bind_conf; |
| 2880 | #endif /* USE_QUIC */ |
Amaury Denoyelle | 7c564bf | 2022-01-24 11:04:05 +0100 | [diff] [blame] | 2881 | |
| 2882 | if (!s) { |
| 2883 | /* must never happen */ |
| 2884 | ABORT_NOW(); |
| 2885 | return 0; |
| 2886 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2887 | |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2888 | #ifdef USE_QUIC |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2889 | if (qc) { |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2890 | /* Look for the QUIC transport parameters. */ |
| 2891 | #ifdef OPENSSL_IS_BORINGSSL |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2892 | 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] | 2893 | &extension_data, &extension_len)) |
| 2894 | #else |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2895 | 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] | 2896 | &extension_data, &extension_len)) |
| 2897 | #endif |
Frédéric Lécaille | b5b5247 | 2021-11-22 15:55:16 +0100 | [diff] [blame] | 2898 | { |
| 2899 | /* This is not redundant. It we only return 0 without setting |
| 2900 | * <*al>, this has as side effect to generate another TLS alert |
| 2901 | * which would be set after calling quic_set_tls_alert(). |
| 2902 | */ |
| 2903 | *al = SSL_AD_MISSING_EXTENSION; |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2904 | quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION); |
Frédéric Lécaille | b5b5247 | 2021-11-22 15:55:16 +0100 | [diff] [blame] | 2905 | return 0; |
| 2906 | } |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2907 | |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2908 | if (!quic_transport_params_store(qc, 0, extension_data, |
Frédéric Lécaille | 301425b | 2022-06-14 17:40:39 +0200 | [diff] [blame] | 2909 | extension_data + extension_len) || |
| 2910 | !qc_conn_finalize(qc, 0)) |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2911 | goto abort; |
| 2912 | } |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 2913 | #endif /* USE_QUIC */ |
Frédéric Lécaille | e9473c7 | 2020-11-23 15:37:11 +0100 | [diff] [blame] | 2914 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2915 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2916 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2917 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2918 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2919 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2920 | #else |
| 2921 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2922 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2923 | /* |
| 2924 | * The server_name extension was given too much extensibility when it |
| 2925 | * was written, so parsing the normal case is a bit complex. |
| 2926 | */ |
| 2927 | size_t len; |
| 2928 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2929 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2930 | /* Extract the length of the supplied list of names. */ |
| 2931 | len = (*extension_data++) << 8; |
| 2932 | len |= *extension_data++; |
| 2933 | if (len + 2 != extension_len) |
| 2934 | goto abort; |
| 2935 | /* |
| 2936 | * The list in practice only has a single element, so we only consider |
| 2937 | * the first one. |
| 2938 | */ |
| 2939 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2940 | goto abort; |
| 2941 | extension_len = len - 1; |
| 2942 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2943 | if (extension_len <= 2) |
| 2944 | goto abort; |
| 2945 | len = (*extension_data++) << 8; |
| 2946 | len |= *extension_data++; |
| 2947 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2948 | || memchr(extension_data, 0, len) != NULL) |
| 2949 | goto abort; |
| 2950 | servername = extension_data; |
| 2951 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2952 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2953 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 2954 | 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] | 2955 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2956 | } |
| 2957 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2958 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2959 | if (!s->strict_sni) { |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2960 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2961 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 2962 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2963 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2964 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2965 | goto abort; |
| 2966 | } |
| 2967 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 2968 | /* extract/check clientHello information */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2969 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2970 | 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] | 2971 | #else |
| 2972 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2973 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2974 | uint8_t sign; |
| 2975 | size_t len; |
| 2976 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2977 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2978 | len = (*extension_data++) << 8; |
| 2979 | len |= *extension_data++; |
| 2980 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2981 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2982 | if (len % 2 != 0) |
| 2983 | goto abort; |
| 2984 | for (; len > 0; len -= 2) { |
| 2985 | extension_data++; /* hash */ |
| 2986 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2987 | switch (sign) { |
| 2988 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2989 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2990 | break; |
| 2991 | case TLSEXT_signature_ecdsa: |
| 2992 | has_ecdsa_sig = 1; |
| 2993 | break; |
| 2994 | default: |
| 2995 | continue; |
| 2996 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2997 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2998 | break; |
| 2999 | } |
| 3000 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 3001 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 3002 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3003 | } |
| 3004 | 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] | 3005 | const SSL_CIPHER *cipher; |
| 3006 | size_t len; |
| 3007 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 3008 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3009 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 3010 | len = ctx->cipher_suites_len; |
| 3011 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3012 | #else |
| 3013 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 3014 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 3015 | if (len % 2 != 0) |
| 3016 | goto abort; |
| 3017 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3018 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 3019 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3020 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3021 | #else |
| 3022 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 3023 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 3024 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 3025 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3026 | break; |
| 3027 | } |
| 3028 | } |
| 3029 | } |
| 3030 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 3031 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3032 | trash.area[i] = tolower(servername[i]); |
| 3033 | if (!wildp && (trash.area[i] == '.')) |
| 3034 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3035 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3036 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3037 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3038 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3039 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 3040 | /* Look for an ECDSA, RSA and DSA certificate, first in the single |
| 3041 | * name and if not found in the wildcard */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 3042 | for (i = 0; i < 2; i++) { |
| 3043 | if (i == 0) /* lookup in full qualified names */ |
| 3044 | node = ebst_lookup(&s->sni_ctx, trash.area); |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 3045 | else if (i == 1 && wildp) /* lookup in wildcards names */ |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 3046 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 3047 | else |
| 3048 | break; |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 3049 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3050 | for (n = node; n; n = ebmb_next_dup(n)) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 3051 | |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 3052 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3053 | if (!container_of(n, struct sni_ctx, name)->neg) { |
William Lallemand | 30f9e09 | 2020-08-17 14:31:19 +0200 | [diff] [blame] | 3054 | struct sni_ctx *sni, *sni_tmp; |
| 3055 | int skip = 0; |
| 3056 | |
| 3057 | if (i == 1 && wildp) { /* wildcard */ |
| 3058 | /* If this is a wildcard, look for an exclusion on the same crt-list line */ |
| 3059 | sni = container_of(n, struct sni_ctx, name); |
| 3060 | 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] | 3061 | 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] | 3062 | skip = 1; |
| 3063 | break; |
| 3064 | } |
| 3065 | } |
| 3066 | if (skip) |
| 3067 | continue; |
| 3068 | } |
| 3069 | |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3070 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3071 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 3072 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3073 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3074 | break; |
| 3075 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 3076 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3077 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3078 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3079 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3080 | if (!node_anonymous) |
| 3081 | node_anonymous = n; |
| 3082 | break; |
| 3083 | } |
| 3084 | } |
| 3085 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 3086 | } |
| 3087 | /* Once the certificates are found, select them depending on what is |
| 3088 | * supported in the client and by key_signature priority order: EDSA > |
| 3089 | * RSA > DSA */ |
William Lallemand | 5b1d1f6 | 2020-08-14 15:30:13 +0200 | [diff] [blame] | 3090 | if (has_ecdsa_sig && node_ecdsa) |
| 3091 | node = node_ecdsa; |
| 3092 | else if (has_rsa_sig && node_rsa) |
| 3093 | node = node_rsa; |
| 3094 | else if (node_anonymous) |
| 3095 | node = node_anonymous; |
| 3096 | else if (node_ecdsa) |
| 3097 | node = node_ecdsa; /* no ecdsa signature case (< TLSv1.2) */ |
| 3098 | else |
| 3099 | node = node_rsa; /* no rsa signature case (far far away) */ |
| 3100 | |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 3101 | if (node) { |
| 3102 | /* switch ctx */ |
| 3103 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
| 3104 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
| 3105 | if (conf) { |
| 3106 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 3107 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 3108 | if (conf->early_data) |
| 3109 | allow_early = 1; |
Emmanuel Hocdet | 3777e3a | 2019-11-06 16:05:34 +0100 | [diff] [blame] | 3110 | } |
William Lallemand | 94bd319 | 2020-08-14 14:43:35 +0200 | [diff] [blame] | 3111 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
| 3112 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3113 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3114 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 3115 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3116 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 3117 | 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] | 3118 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 3119 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3120 | } |
| 3121 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3122 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3123 | /* no certificate match, is the default_ctx */ |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3124 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 3125 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3126 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 3127 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3128 | } |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 3129 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 3130 | /* We are about to raise an handshake error so the servername extension |
| 3131 | * callback will never be called and the SNI will never be stored in the |
| 3132 | * SSL context. In order for the ssl_fc_sni sample fetch to still work |
| 3133 | * in such a case, we store the SNI ourselves as an ex_data information |
| 3134 | * in the SSL context. |
| 3135 | */ |
| 3136 | { |
| 3137 | char *client_sni = pool_alloc(ssl_sock_client_sni_pool); |
| 3138 | if (client_sni) { |
| 3139 | strncpy(client_sni, trash.area, TLSEXT_MAXLEN_host_name); |
| 3140 | client_sni[TLSEXT_MAXLEN_host_name] = '\0'; |
| 3141 | SSL_set_ex_data(ssl, ssl_client_sni_index, client_sni); |
| 3142 | } |
| 3143 | } |
| 3144 | |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 3145 | /* other cases fallback on abort, if strict-sni is set but no node was found */ |
| 3146 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3147 | abort: |
| 3148 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
Amaury Denoyelle | 9320dd5 | 2022-01-19 10:03:30 +0100 | [diff] [blame] | 3149 | if (conn) |
| 3150 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3151 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 3152 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3153 | #else |
| 3154 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 3155 | return 0; |
| 3156 | #endif |
William Lallemand | 7980dff | 2021-11-18 17:46:26 +0100 | [diff] [blame] | 3157 | |
| 3158 | allow_early: |
| 3159 | #ifdef OPENSSL_IS_BORINGSSL |
| 3160 | if (allow_early) |
| 3161 | SSL_set_early_data_enabled(ssl, 1); |
| 3162 | #else |
| 3163 | if (!allow_early) |
| 3164 | SSL_set_max_early_data(ssl, 0); |
| 3165 | #endif |
| 3166 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3167 | } |
| 3168 | |
William Lallemand | 002e206 | 2021-11-18 15:25:16 +0100 | [diff] [blame] | 3169 | #else /* ! HAVE_SSL_CLIENT_HELLO_CB */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3170 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3171 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 3172 | * warning when no match is found, which implies the default (first) cert |
| 3173 | * will keep being used. |
| 3174 | */ |
William Lallemand | 844009d | 2022-09-02 15:27:32 +0200 | [diff] [blame] | 3175 | int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3176 | { |
| 3177 | const char *servername; |
| 3178 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3179 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3180 | struct bind_conf *s = priv; |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 3181 | #ifdef USE_QUIC |
| 3182 | const uint8_t *extension_data; |
| 3183 | size_t extension_len; |
| 3184 | struct quic_conn *qc = SSL_get_ex_data(ssl, ssl_qc_app_data_index); |
| 3185 | #endif /* USE_QUIC */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3186 | int i; |
| 3187 | (void)al; /* shut gcc stupid warning */ |
| 3188 | |
William Lallemand | 70a6e63 | 2022-09-07 11:21:34 +0200 | [diff] [blame] | 3189 | #ifdef USE_QUIC |
| 3190 | if (qc) { |
| 3191 | |
| 3192 | /* Look for the QUIC transport parameters. */ |
| 3193 | SSL_get_peer_quic_transport_params(ssl, &extension_data, &extension_len); |
| 3194 | if (extension_len == 0) { |
| 3195 | /* This is not redundant. It we only return 0 without setting |
| 3196 | * <*al>, this has as side effect to generate another TLS alert |
| 3197 | * which would be set after calling quic_set_tls_alert(). |
| 3198 | */ |
| 3199 | *al = SSL_AD_MISSING_EXTENSION; |
| 3200 | quic_set_tls_alert(qc, SSL_AD_MISSING_EXTENSION); |
| 3201 | return SSL_TLSEXT_ERR_NOACK; |
| 3202 | } |
| 3203 | |
| 3204 | if (!quic_transport_params_store(qc, 0, extension_data, |
| 3205 | extension_data + extension_len) || |
| 3206 | !qc_conn_finalize(qc, 0)) { |
| 3207 | return SSL_TLSEXT_ERR_NOACK; |
| 3208 | } |
| 3209 | } |
| 3210 | #endif /* USE_QUIC */ |
| 3211 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3212 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 3213 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3214 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 3215 | 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] | 3216 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3217 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3218 | if (s->strict_sni) |
| 3219 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3220 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3221 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3222 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3223 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 3224 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3225 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 3226 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3227 | if (!servername[i]) |
| 3228 | break; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 3229 | trash.area[i] = tolower((unsigned char)servername[i]); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3230 | if (!wildp && (trash.area[i] == '.')) |
| 3231 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3232 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3233 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3234 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3235 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 3236 | node = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3237 | /* lookup in full qualified names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 3238 | for (n = ebst_lookup(&s->sni_ctx, trash.area); n; n = ebmb_next_dup(n)) { |
| 3239 | /* lookup a not neg filter */ |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3240 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 3241 | node = n; |
| 3242 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 3243 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3244 | } |
| 3245 | if (!node && wildp) { |
| 3246 | /* lookup in wildcards names */ |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 3247 | for (n = ebst_lookup(&s->sni_w_ctx, wildp); n; n = ebmb_next_dup(n)) { |
| 3248 | /* lookup a not neg filter */ |
| 3249 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 3250 | node = n; |
| 3251 | break; |
| 3252 | } |
| 3253 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3254 | } |
Emmanuel Hocdet | c5fdf0f | 2019-11-04 15:49:46 +0100 | [diff] [blame] | 3255 | if (!node) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3256 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 3257 | 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] | 3258 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3259 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 3260 | return SSL_TLSEXT_ERR_OK; |
| 3261 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 3262 | #endif |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3263 | if (s->strict_sni) { |
| 3264 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3265 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3266 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3267 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3268 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3269 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3270 | } |
| 3271 | |
| 3272 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 3273 | 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] | 3274 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3275 | return SSL_TLSEXT_ERR_OK; |
| 3276 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3277 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3278 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3279 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3280 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3281 | |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 3282 | static inline HASSL_DH *ssl_new_dh_fromdata(BIGNUM *p, BIGNUM *g) |
| 3283 | { |
| 3284 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3285 | OSSL_PARAM_BLD *tmpl = NULL; |
| 3286 | OSSL_PARAM *params = NULL; |
| 3287 | EVP_PKEY_CTX *ctx = NULL; |
| 3288 | EVP_PKEY *pkey = NULL; |
| 3289 | |
| 3290 | if ((tmpl = OSSL_PARAM_BLD_new()) == NULL |
| 3291 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_P, p) |
| 3292 | || !OSSL_PARAM_BLD_push_BN(tmpl, OSSL_PKEY_PARAM_FFC_G, g) |
| 3293 | || (params = OSSL_PARAM_BLD_to_param(tmpl)) == NULL) { |
| 3294 | goto end; |
| 3295 | } |
| 3296 | ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 3297 | if (ctx == NULL |
| 3298 | || !EVP_PKEY_fromdata_init(ctx) |
| 3299 | || !EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params)) { |
| 3300 | goto end; |
| 3301 | } |
| 3302 | |
| 3303 | end: |
| 3304 | EVP_PKEY_CTX_free(ctx); |
| 3305 | OSSL_PARAM_free(params); |
| 3306 | OSSL_PARAM_BLD_free(tmpl); |
Remi Tricot-Le Breton | a2c21db | 2022-11-03 15:16:47 +0100 | [diff] [blame] | 3307 | BN_free(p); |
| 3308 | BN_free(g); |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 3309 | return pkey; |
| 3310 | #else |
| 3311 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3312 | HASSL_DH *dh = DH_new(); |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 3313 | |
| 3314 | if (!dh) |
| 3315 | return NULL; |
| 3316 | |
| 3317 | DH_set0_pqg(dh, p, NULL, g); |
| 3318 | |
| 3319 | return dh; |
| 3320 | #endif |
| 3321 | } |
| 3322 | |
Remi Tricot-Le Breton | c69be7c | 2022-04-20 18:30:17 +0200 | [diff] [blame] | 3323 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 3324 | static inline HASSL_DH *ssl_get_dh_by_nid(int nid) |
| 3325 | { |
| 3326 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3327 | OSSL_PARAM params[2]; |
| 3328 | EVP_PKEY *pkey = NULL; |
| 3329 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL); |
| 3330 | const char *named_group = NULL; |
| 3331 | |
| 3332 | if (!pctx) |
| 3333 | goto end; |
| 3334 | |
| 3335 | named_group = OBJ_nid2ln(nid); |
| 3336 | |
| 3337 | if (!named_group) |
| 3338 | goto end; |
| 3339 | |
| 3340 | params[0] = OSSL_PARAM_construct_utf8_string("group", (char*)named_group, 0); |
| 3341 | params[1] = OSSL_PARAM_construct_end(); |
| 3342 | |
| 3343 | if (EVP_PKEY_keygen_init(pctx) && EVP_PKEY_CTX_set_params(pctx, params)) |
| 3344 | EVP_PKEY_generate(pctx, &pkey); |
| 3345 | |
| 3346 | end: |
| 3347 | EVP_PKEY_CTX_free(pctx); |
| 3348 | return pkey; |
| 3349 | #else |
| 3350 | |
| 3351 | HASSL_DH *dh = NULL; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 3352 | dh = DH_new_by_nid(nid); |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 3353 | return dh; |
| 3354 | #endif |
| 3355 | } |
Remi Tricot-Le Breton | c69be7c | 2022-04-20 18:30:17 +0200 | [diff] [blame] | 3356 | #endif |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 3357 | |
Remi Tricot-Le Breton | 7f6425a | 2022-02-11 12:04:52 +0100 | [diff] [blame] | 3358 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3359 | static HASSL_DH * ssl_get_dh_1024(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3360 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3361 | static unsigned char dh1024_p[]={ |
| 3362 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 3363 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 3364 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 3365 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 3366 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 3367 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 3368 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 3369 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 3370 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 3371 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 3372 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 3373 | }; |
| 3374 | static unsigned char dh1024_g[]={ |
| 3375 | 0x02, |
| 3376 | }; |
| 3377 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3378 | BIGNUM *p; |
| 3379 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3380 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3381 | HASSL_DH *dh = NULL; |
| 3382 | |
| 3383 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 3384 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
| 3385 | |
| 3386 | if (p && g) |
| 3387 | dh = ssl_new_dh_fromdata(p, g); |
| 3388 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3389 | return dh; |
| 3390 | } |
| 3391 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3392 | static HASSL_DH *ssl_get_dh_2048(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3393 | { |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 3394 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3395 | static unsigned char dh2048_p[]={ |
| 3396 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 3397 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 3398 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 3399 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 3400 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 3401 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 3402 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 3403 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 3404 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 3405 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 3406 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 3407 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 3408 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 3409 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 3410 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 3411 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 3412 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 3413 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 3414 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 3415 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 3416 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 3417 | 0xB7,0x1F,0x77,0xF3, |
| 3418 | }; |
| 3419 | static unsigned char dh2048_g[]={ |
| 3420 | 0x02, |
| 3421 | }; |
| 3422 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3423 | BIGNUM *p; |
| 3424 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3425 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3426 | HASSL_DH *dh = NULL; |
| 3427 | |
| 3428 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 3429 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
| 3430 | |
| 3431 | if (p && g) |
| 3432 | dh = ssl_new_dh_fromdata(p, g); |
| 3433 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3434 | return dh; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 3435 | #else |
| 3436 | return ssl_get_dh_by_nid(NID_ffdhe2048); |
| 3437 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3438 | } |
| 3439 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3440 | static HASSL_DH *ssl_get_dh_4096(void) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3441 | { |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 3442 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3443 | static unsigned char dh4096_p[]={ |
| 3444 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 3445 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 3446 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 3447 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 3448 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 3449 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 3450 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 3451 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 3452 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 3453 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 3454 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 3455 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 3456 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 3457 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 3458 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 3459 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 3460 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 3461 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 3462 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 3463 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 3464 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 3465 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 3466 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 3467 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 3468 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 3469 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 3470 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 3471 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 3472 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 3473 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 3474 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 3475 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 3476 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 3477 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 3478 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 3479 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 3480 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 3481 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 3482 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 3483 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 3484 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 3485 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 3486 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3487 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3488 | static unsigned char dh4096_g[]={ |
| 3489 | 0x02, |
| 3490 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3491 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3492 | BIGNUM *p; |
| 3493 | BIGNUM *g; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3494 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3495 | HASSL_DH *dh = NULL; |
| 3496 | |
| 3497 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 3498 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
| 3499 | |
| 3500 | if (p && g) |
| 3501 | dh = ssl_new_dh_fromdata(p, g); |
| 3502 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3503 | return dh; |
Remi Tricot-Le Breton | 528b3fd | 2022-04-12 11:31:54 +0200 | [diff] [blame] | 3504 | #else |
| 3505 | return ssl_get_dh_by_nid(NID_ffdhe4096); |
| 3506 | #endif |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3507 | } |
| 3508 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3509 | static HASSL_DH *ssl_get_tmp_dh(EVP_PKEY *pkey) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3510 | { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3511 | HASSL_DH *dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3512 | int type; |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3513 | int keylen = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3514 | |
| 3515 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3516 | |
Ilya Shipitsin | 52f2ff5 | 2022-07-23 23:55:19 +0500 | [diff] [blame] | 3517 | if (type == EVP_PKEY_EC) { |
| 3518 | keylen = global_ssl.default_dh_param; |
| 3519 | } |
| 3520 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3521 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 3522 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 3523 | */ |
| 3524 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 3525 | keylen = EVP_PKEY_bits(pkey); |
| 3526 | } |
| 3527 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 3528 | if (keylen > global_ssl.default_dh_param) { |
| 3529 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3530 | } |
| 3531 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 3532 | if (keylen >= 4096) { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3533 | if (!local_dh_4096) |
| 3534 | local_dh_4096 = ssl_get_dh_4096(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3535 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3536 | } |
| 3537 | else if (keylen >= 2048) { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3538 | if (!local_dh_2048) |
| 3539 | local_dh_2048 = ssl_get_dh_2048(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3540 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3541 | } |
| 3542 | else { |
Remi Tricot-Le Breton | bed7263 | 2022-02-11 12:04:53 +0100 | [diff] [blame] | 3543 | if (!local_dh_1024) |
| 3544 | local_dh_1024 = ssl_get_dh_1024(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 3545 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 3546 | } |
| 3547 | |
| 3548 | return dh; |
| 3549 | } |
| 3550 | |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3551 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3552 | /* Returns Diffie-Hellman parameters matching the private key length |
| 3553 | but not exceeding global_ssl.default_dh_param */ |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3554 | 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] | 3555 | { |
| 3556 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
| 3557 | |
| 3558 | return ssl_get_tmp_dh(pkey); |
| 3559 | } |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3560 | #endif |
Remi Tricot-Le Breton | 292a88c | 2022-02-11 12:04:49 +0100 | [diff] [blame] | 3561 | |
Remi Tricot-Le Breton | 846eda9 | 2022-02-11 12:04:50 +0100 | [diff] [blame] | 3562 | static int ssl_sock_set_tmp_dh(SSL_CTX *ctx, HASSL_DH *dh) |
| 3563 | { |
| 3564 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
| 3565 | return SSL_CTX_set_tmp_dh(ctx, dh); |
| 3566 | #else |
| 3567 | int retval = 0; |
| 3568 | HASSL_DH_up_ref(dh); |
| 3569 | |
| 3570 | retval = SSL_CTX_set0_tmp_dh_pkey(ctx, dh); |
| 3571 | |
| 3572 | if (!retval) |
| 3573 | HASSL_DH_free(dh); |
| 3574 | |
| 3575 | return retval; |
| 3576 | #endif |
| 3577 | } |
| 3578 | |
Remi Tricot-Le Breton | 5f17930 | 2022-02-11 12:04:51 +0100 | [diff] [blame] | 3579 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3580 | static void ssl_sock_set_tmp_dh_from_pkey(SSL_CTX *ctx, EVP_PKEY *pkey) |
| 3581 | { |
| 3582 | HASSL_DH *dh = NULL; |
| 3583 | if (pkey && (dh = ssl_get_tmp_dh(pkey))) { |
| 3584 | HASSL_DH_up_ref(dh); |
| 3585 | if (!SSL_CTX_set0_tmp_dh_pkey(ctx, dh)) |
| 3586 | HASSL_DH_free(dh); |
| 3587 | } |
Remi Tricot-Le Breton | 5f17930 | 2022-02-11 12:04:51 +0100 | [diff] [blame] | 3588 | } |
| 3589 | #endif |
| 3590 | |
Remi Tricot-Le Breton | 09ebb33 | 2022-02-11 12:04:48 +0100 | [diff] [blame] | 3591 | HASSL_DH *ssl_sock_get_dh_from_bio(BIO *bio) |
| 3592 | { |
| 3593 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x3000000fL) |
| 3594 | HASSL_DH *dh = NULL; |
| 3595 | OSSL_DECODER_CTX *dctx = NULL; |
| 3596 | const char *format = "PEM"; |
| 3597 | const char *keytype = "DH"; |
| 3598 | |
| 3599 | dctx = OSSL_DECODER_CTX_new_for_pkey(&dh, format, NULL, keytype, |
| 3600 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
| 3601 | NULL, NULL); |
| 3602 | |
| 3603 | if (dctx == NULL || OSSL_DECODER_CTX_get_num_decoders(dctx) == 0) |
| 3604 | goto end; |
| 3605 | |
| 3606 | /* The DH parameters might not be the first section found in the PEM |
| 3607 | * file so we need to iterate over all of them until we find the right |
| 3608 | * one. |
| 3609 | */ |
| 3610 | while (!BIO_eof(bio) && !dh) |
| 3611 | OSSL_DECODER_from_bio(dctx, bio); |
| 3612 | |
| 3613 | end: |
| 3614 | OSSL_DECODER_CTX_free(dctx); |
| 3615 | return dh; |
| 3616 | #else |
| 3617 | HASSL_DH *dh = NULL; |
| 3618 | |
| 3619 | dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); |
| 3620 | |
| 3621 | return dh; |
| 3622 | #endif |
| 3623 | } |
| 3624 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3625 | static HASSL_DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3626 | { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3627 | HASSL_DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3628 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3629 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3630 | if (in == NULL) |
| 3631 | goto end; |
| 3632 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3633 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3634 | goto end; |
| 3635 | |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3636 | dh = ssl_sock_get_dh_from_bio(in); |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3637 | |
| 3638 | end: |
| 3639 | if (in) |
| 3640 | BIO_free(in); |
| 3641 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 3642 | ERR_clear_error(); |
| 3643 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 3644 | return dh; |
| 3645 | } |
| 3646 | |
| 3647 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 3648 | { |
| 3649 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 3650 | |
| 3651 | if (global_dh) { |
| 3652 | return 0; |
| 3653 | } |
| 3654 | |
| 3655 | return -1; |
| 3656 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3657 | #endif |
| 3658 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3659 | /* 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] | 3660 | 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] | 3661 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 3662 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3663 | { |
| 3664 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3665 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3666 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3667 | if (*name == '!') { |
| 3668 | neg = 1; |
| 3669 | name++; |
| 3670 | } |
| 3671 | if (*name == '*') { |
| 3672 | wild = 1; |
| 3673 | name++; |
| 3674 | } |
| 3675 | /* !* filter is a nop */ |
| 3676 | if (neg && wild) |
| 3677 | return order; |
| 3678 | if (*name) { |
| 3679 | int j, len; |
| 3680 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3681 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 3682 | trash.area[j] = tolower((unsigned char)name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3683 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3684 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3685 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 3686 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3687 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 3688 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3689 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3690 | memcpy(sc->name.key, trash.area, len + 1); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3691 | SSL_CTX_up_ref(ctx); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3692 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3693 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3694 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3695 | sc->order = order++; |
| 3696 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3697 | sc->wild = wild; |
| 3698 | sc->name.node.leaf_p = NULL; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 3699 | sc->ckch_inst = ckch_inst; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3700 | LIST_APPEND(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3701 | } |
| 3702 | return order; |
| 3703 | } |
| 3704 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3705 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3706 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 3707 | * This function can't return an error. |
| 3708 | * |
| 3709 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 3710 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3711 | 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] | 3712 | { |
| 3713 | |
| 3714 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 3715 | struct ebmb_node *node; |
| 3716 | |
| 3717 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 3718 | |
| 3719 | /* ignore if sc0 was already inserted in a tree */ |
| 3720 | if (sc0->name.node.leaf_p) |
| 3721 | continue; |
| 3722 | |
| 3723 | /* Check for duplicates. */ |
| 3724 | if (sc0->wild) |
| 3725 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 3726 | else |
| 3727 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 3728 | |
| 3729 | for (; node; node = ebmb_next_dup(node)) { |
| 3730 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 3731 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 3732 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 3733 | /* it's a duplicate, we should remove and free it */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 3734 | LIST_DELETE(&sc0->by_ckch_inst); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 3735 | SSL_CTX_free(sc0->ctx); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 3736 | ha_free(&sc0); |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 3737 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | /* if duplicate, ignore the insertion */ |
| 3742 | if (!sc0) |
| 3743 | continue; |
| 3744 | |
| 3745 | if (sc0->wild) |
| 3746 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 3747 | else |
| 3748 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3749 | } |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 3750 | |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 3751 | /* replace the default_ctx if required with the instance's ctx. */ |
| 3752 | if (ckch_inst->is_default) { |
| 3753 | SSL_CTX_free(bind_conf->default_ctx); |
| 3754 | SSL_CTX_up_ref(ckch_inst->ctx); |
| 3755 | bind_conf->default_ctx = ckch_inst->ctx; |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 3756 | bind_conf->default_inst = ckch_inst; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3757 | } |
| 3758 | } |
| 3759 | |
| 3760 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3761 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3762 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3763 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3764 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 3765 | /* tree of crtlist (crt-list/directory) */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 3766 | struct eb_root crtlists_tree = EB_ROOT_UNIQUE; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3767 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3768 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 3769 | * If there is no DH parameter available in the ckchs, the global |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3770 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 3771 | * DH parameter available in ckchs nor in global, the default |
| 3772 | * DH parameters are applied on the SSL_CTX. |
| 3773 | * Returns a bitfield containing the flags: |
| 3774 | * ERR_FATAL in any fatal error case |
| 3775 | * ERR_ALERT if a reason of the error is availabine in err |
| 3776 | * ERR_WARN if a warning is available into err |
| 3777 | * The value 0 means there is no error nor warning and |
| 3778 | * the operation succeed. |
| 3779 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3780 | #ifndef OPENSSL_NO_DH |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3781 | 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] | 3782 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3783 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3784 | int ret = 0; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3785 | HASSL_DH *dh = NULL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3786 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3787 | if (data && data->dh) { |
| 3788 | dh = data->dh; |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3789 | if (!ssl_sock_set_tmp_dh(ctx, dh)) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3790 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 3791 | err && *err ? *err : "", path); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3792 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3793 | err && *err ? *err : ""); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3794 | ret |= ERR_WARN; |
| 3795 | goto end; |
| 3796 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3797 | |
| 3798 | if (ssl_dh_ptr_index >= 0) { |
| 3799 | /* store a pointer to the DH params to avoid complaining about |
| 3800 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 3801 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 3802 | } |
| 3803 | } |
| 3804 | else if (global_dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 3805 | if (!ssl_sock_set_tmp_dh(ctx, global_dh)) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3806 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 3807 | err && *err ? *err : "", path); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3808 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3809 | err && *err ? *err : ""); |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3810 | ret |= ERR_WARN; |
| 3811 | goto end; |
| 3812 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3813 | } |
| 3814 | else { |
| 3815 | /* Clear openssl global errors stack */ |
| 3816 | ERR_clear_error(); |
| 3817 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3818 | /* We do not want DHE ciphers to be added to the cipher list |
| 3819 | * unless there is an explicit global dh option in the conf. |
| 3820 | */ |
| 3821 | if (global_ssl.default_dh_param) { |
| 3822 | if (global_ssl.default_dh_param <= 1024) { |
| 3823 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 3824 | if (local_dh_1024 == NULL) |
| 3825 | local_dh_1024 = ssl_get_dh_1024(); |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3826 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3827 | if (local_dh_1024 == NULL) { |
| 3828 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3829 | err && *err ? *err : "", path); |
| 3830 | ret |= ERR_ALERT | ERR_FATAL; |
| 3831 | goto end; |
| 3832 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3833 | |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3834 | if (!ssl_sock_set_tmp_dh(ctx, local_dh_1024)) { |
| 3835 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 3836 | err && *err ? *err : "", path); |
| 3837 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 3838 | err && *err ? *err : ""); |
| 3839 | ret |= ERR_WARN; |
| 3840 | goto end; |
| 3841 | } |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 3842 | } |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3843 | else { |
Remi Tricot-Le Breton | 88c5695 | 2022-02-11 12:04:56 +0100 | [diff] [blame] | 3844 | #if (HA_OPENSSL_VERSION_NUMBER < 0x3000000fL) |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3845 | 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] | 3846 | #else |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3847 | 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] | 3848 | #endif |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 3849 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3850 | } |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 3851 | } |
| 3852 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3853 | end: |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3854 | ERR_clear_error(); |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3855 | return ret; |
| 3856 | } |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3857 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3858 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3859 | |
| 3860 | /* Load a certificate chain into an SSL context. |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3861 | * Returns a bitfield containing the flags: |
| 3862 | * ERR_FATAL in any fatal error case |
| 3863 | * ERR_ALERT if the reason of the error is available in err |
| 3864 | * ERR_WARN if a warning is available into err |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3865 | * The caller is responsible of freeing the newly built or newly refcounted |
| 3866 | * find_chain element. |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3867 | * The value 0 means there is no error nor warning and |
| 3868 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3869 | */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3870 | 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] | 3871 | SSL_CTX *ctx, STACK_OF(X509) **find_chain, char **err) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3872 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3873 | int errcode = 0; |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3874 | int ret; |
| 3875 | |
| 3876 | ERR_clear_error(); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3877 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3878 | if (find_chain == NULL) { |
| 3879 | errcode |= ERR_FATAL; |
| 3880 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3881 | } |
| 3882 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3883 | if (!SSL_CTX_use_certificate(ctx, data->cert)) { |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3884 | ret = ERR_get_error(); |
| 3885 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s': %s.\n", |
| 3886 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3887 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3888 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3889 | } |
| 3890 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3891 | if (data->chain) { |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3892 | *find_chain = X509_chain_up_ref(data->chain); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3893 | } else { |
| 3894 | /* Find Certificate Chain in global */ |
| 3895 | struct issuer_chain *issuer; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3896 | issuer = ssl_get0_issuer_chain(data->cert); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3897 | if (issuer) |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3898 | *find_chain = X509_chain_up_ref(issuer->chain); |
Emmanuel Hocdet | b90d2cb | 2020-02-18 15:27:32 +0100 | [diff] [blame] | 3899 | } |
William Lallemand | 8588857 | 2020-02-27 14:48:35 +0100 | [diff] [blame] | 3900 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3901 | if (!*find_chain) { |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3902 | /* always put a null chain stack in the SSL_CTX so it does not |
| 3903 | * try to build the chain from the verify store */ |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3904 | *find_chain = sk_X509_new_null(); |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3905 | } |
| 3906 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3907 | /* 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] | 3908 | #ifdef SSL_CTX_set1_chain |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3909 | if (!SSL_CTX_set1_chain(ctx, *find_chain)) { |
William Lallemand | 45fed2c | 2022-11-15 16:56:03 +0100 | [diff] [blame] | 3910 | ret = ERR_get_error(); |
| 3911 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s': %s. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3912 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
William Lallemand | 935d829 | 2020-08-12 20:02:10 +0200 | [diff] [blame] | 3913 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3914 | goto end; |
| 3915 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3916 | #else |
| 3917 | { /* legacy compat (< openssl 1.0.2) */ |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3918 | X509 *ca; |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 3919 | while ((ca = sk_X509_shift(*find_chain))) |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3920 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3921 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3922 | err && *err ? *err : "", path); |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3923 | X509_free(ca); |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3924 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3925 | goto end; |
| 3926 | } |
Emmanuel Hocdet | 4fed93e | 2020-02-28 16:00:34 +0100 | [diff] [blame] | 3927 | } |
William Lallemand | f187ce6 | 2020-06-02 18:27:20 +0200 | [diff] [blame] | 3928 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3929 | |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3930 | #ifdef SSL_CTX_build_cert_chain |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3931 | /* remove the Root CA from the SSL_CTX if the option is activated */ |
| 3932 | if (global_ssl.skip_self_issued_ca) { |
| 3933 | if (!SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT|SSL_BUILD_CHAIN_FLAG_UNTRUSTED|SSL_BUILD_CHAIN_FLAG_IGNORE_ERROR)) { |
| 3934 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3935 | err && *err ? *err : "", path); |
| 3936 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3937 | goto end; |
| 3938 | } |
| 3939 | } |
William Lallemand | 9a1d839 | 2020-08-10 17:28:23 +0200 | [diff] [blame] | 3940 | #endif |
William Lallemand | bf298af | 2020-08-10 16:18:45 +0200 | [diff] [blame] | 3941 | |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3942 | end: |
| 3943 | return errcode; |
| 3944 | } |
| 3945 | |
| 3946 | |
| 3947 | /* Loads the info in ckch into ctx |
| 3948 | * Returns a bitfield containing the flags: |
| 3949 | * ERR_FATAL in any fatal error case |
| 3950 | * ERR_ALERT if the reason of the error is available in err |
| 3951 | * ERR_WARN if a warning is available into err |
| 3952 | * The value 0 means there is no error nor warning and |
| 3953 | * the operation succeed. |
| 3954 | */ |
Remi Tricot-Le Breton | cc34667 | 2022-12-20 11:11:08 +0100 | [diff] [blame] | 3955 | 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] | 3956 | { |
| 3957 | int errcode = 0; |
| 3958 | STACK_OF(X509) *find_chain = NULL; |
| 3959 | |
William Lallemand | 5de4951 | 2022-10-27 14:41:07 +0200 | [diff] [blame] | 3960 | ERR_clear_error(); |
| 3961 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3962 | if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) { |
William Lallemand | 5de4951 | 2022-10-27 14:41:07 +0200 | [diff] [blame] | 3963 | int ret; |
| 3964 | |
| 3965 | ret = ERR_get_error(); |
| 3966 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s': %s.\n", |
| 3967 | err && *err ? *err : "", path, ERR_reason_error_string(ret)); |
Remi Tricot-Le Breton | ec805a3 | 2021-01-25 17:19:42 +0100 | [diff] [blame] | 3968 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3969 | return errcode; |
| 3970 | } |
| 3971 | |
| 3972 | /* Load certificate chain */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3973 | 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] | 3974 | if (errcode & ERR_CODE) |
| 3975 | goto end; |
| 3976 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3977 | #ifndef OPENSSL_NO_DH |
| 3978 | /* store a NULL pointer to indicate we have not yet loaded |
| 3979 | a custom DH param file */ |
| 3980 | if (ssl_dh_ptr_index >= 0) { |
| 3981 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3982 | } |
| 3983 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3984 | errcode |= ssl_sock_load_dh_params(ctx, data, path, err); |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3985 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3986 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3987 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3988 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3989 | } |
| 3990 | #endif |
| 3991 | |
Ilya Shipitsin | 7bbf586 | 2021-02-06 18:55:27 +0500 | [diff] [blame] | 3992 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 3993 | if (sctl_ex_index >= 0 && data->sctl) { |
| 3994 | if (ssl_sock_load_sctl(ctx, data->sctl) < 0) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3995 | 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] | 3996 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3997 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3998 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3999 | } |
| 4000 | } |
| 4001 | #endif |
| 4002 | |
Emmanuel Hocdet | a73a222 | 2020-10-26 13:55:30 +0100 | [diff] [blame] | 4003 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 4004 | /* Load OCSP Info into context */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4005 | if (data->ocsp_response) { |
| 4006 | if (ssl_sock_load_ocsp(ctx, data, find_chain) < 0) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 4007 | 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", |
| 4008 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4009 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4010 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 4011 | } |
| 4012 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 4013 | #endif |
| 4014 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4015 | end: |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 4016 | sk_X509_pop_free(find_chain, X509_free); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4017 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 4018 | } |
| 4019 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4020 | |
| 4021 | /* Loads the info of a ckch built out of a backend certificate into an SSL ctx |
| 4022 | * Returns a bitfield containing the flags: |
| 4023 | * ERR_FATAL in any fatal error case |
| 4024 | * ERR_ALERT if the reason of the error is available in err |
| 4025 | * ERR_WARN if a warning is available into err |
| 4026 | * The value 0 means there is no error nor warning and |
| 4027 | * the operation succeed. |
| 4028 | */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4029 | 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] | 4030 | SSL_CTX *ctx, char **err) |
| 4031 | { |
| 4032 | int errcode = 0; |
| 4033 | STACK_OF(X509) *find_chain = NULL; |
| 4034 | |
| 4035 | /* Load the private key */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4036 | if (SSL_CTX_use_PrivateKey(ctx, data->key) <= 0) { |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4037 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 4038 | err && *err ? *err : "", path); |
| 4039 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4040 | } |
| 4041 | |
| 4042 | /* Load certificate chain */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4043 | 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] | 4044 | if (errcode & ERR_CODE) |
| 4045 | goto end; |
| 4046 | |
| 4047 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 4048 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4049 | err && *err ? *err : "", path); |
| 4050 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4051 | } |
| 4052 | |
| 4053 | end: |
Remi Tricot-Le Breton | 4cf0d3f | 2022-12-15 15:44:37 +0100 | [diff] [blame] | 4054 | sk_X509_pop_free(find_chain, X509_free); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4055 | return errcode; |
| 4056 | } |
| 4057 | |
| 4058 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4059 | /* |
| 4060 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4061 | * |
| 4062 | * Returns a bitfield containing the flags: |
| 4063 | * ERR_FATAL in any fatal error case |
| 4064 | * ERR_ALERT if the reason of the error is available in err |
| 4065 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4066 | */ |
William Lallemand | c756bbd | 2020-05-13 17:23:59 +0200 | [diff] [blame] | 4067 | 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] | 4068 | 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] | 4069 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4070 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4071 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4072 | int order = 0; |
| 4073 | X509_NAME *xname; |
| 4074 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4075 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4076 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4077 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4078 | STACK_OF(GENERAL_NAME) *names; |
| 4079 | #endif |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4080 | struct ckch_data *data; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4081 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4082 | int errcode = 0; |
| 4083 | |
| 4084 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 4085 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4086 | if (!ckchs || !ckchs->data) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4087 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4088 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4089 | data = ckchs->data; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4090 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4091 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 4092 | if (!ctx) { |
| 4093 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4094 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4095 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4096 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4097 | } |
| 4098 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4099 | errcode |= ssl_sock_put_ckch_into_ctx(path, data, ctx, err); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 4100 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4101 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4102 | |
| 4103 | ckch_inst = ckch_inst_new(); |
| 4104 | if (!ckch_inst) { |
| 4105 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4106 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4107 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4108 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 4109 | } |
| 4110 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4111 | pkey = X509_get_pubkey(data->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4112 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4113 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4114 | switch(EVP_PKEY_base_id(pkey)) { |
| 4115 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4116 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4117 | break; |
| 4118 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 4119 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 4120 | break; |
| 4121 | case EVP_PKEY_DSA: |
| 4122 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 4123 | break; |
| 4124 | } |
| 4125 | EVP_PKEY_free(pkey); |
| 4126 | } |
| 4127 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4128 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4129 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4130 | 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] | 4131 | if (order < 0) { |
| 4132 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4133 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4134 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4135 | } |
| 4136 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4137 | } |
| 4138 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4139 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4140 | 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] | 4141 | if (names) { |
| 4142 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 4143 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 4144 | if (name->type == GEN_DNS) { |
| 4145 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4146 | 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] | 4147 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4148 | if (order < 0) { |
| 4149 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4150 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4151 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4152 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4153 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4154 | } |
| 4155 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4156 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4157 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4158 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4159 | xname = X509_get_subject_name(data->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4160 | i = -1; |
| 4161 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 4162 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4163 | ASN1_STRING *value; |
| 4164 | |
| 4165 | value = X509_NAME_ENTRY_get_data(entry); |
| 4166 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4167 | 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] | 4168 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4169 | if (order < 0) { |
| 4170 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4171 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4172 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4173 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4174 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4175 | } |
| 4176 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4177 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 4178 | * the tree, so it will be discovered and cleaned in time. |
| 4179 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4180 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4181 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4182 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4183 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 4184 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4185 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4186 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4187 | } |
| 4188 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4189 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4190 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4191 | bind_conf->default_ssl_conf = ssl_conf; |
William Lallemand | 21724f0 | 2019-11-04 17:56:13 +0100 | [diff] [blame] | 4192 | ckch_inst->is_default = 1; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4193 | SSL_CTX_up_ref(ctx); |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 4194 | bind_conf->default_inst = ckch_inst; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4195 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4196 | |
Remi Tricot-Le Breton | 8218aed | 2021-03-17 14:56:54 +0100 | [diff] [blame] | 4197 | /* Always keep a reference to the newly constructed SSL_CTX in the |
| 4198 | * instance. This way if the instance has no SNIs, the SSL_CTX will |
| 4199 | * still be linked. */ |
| 4200 | SSL_CTX_up_ref(ctx); |
| 4201 | ckch_inst->ctx = ctx; |
| 4202 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4203 | /* everything succeed, the ckch instance can be used */ |
| 4204 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4205 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | cfca142 | 2020-03-05 10:17:47 +0100 | [diff] [blame] | 4206 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4207 | |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4208 | SSL_CTX_free(ctx); /* we need to free the ctx since we incremented the refcount where it's used */ |
| 4209 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4210 | *ckchi = ckch_inst; |
| 4211 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4212 | |
| 4213 | error: |
| 4214 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4215 | if (ckch_inst) { |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 4216 | if (ckch_inst->is_default) |
| 4217 | SSL_CTX_free(ctx); |
| 4218 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 4219 | ckch_inst_free(ckch_inst); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4220 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4221 | } |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4222 | SSL_CTX_free(ctx); |
| 4223 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4224 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4225 | } |
| 4226 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4227 | |
| 4228 | /* |
| 4229 | * This function allocate a ckch_inst that will be used on the backend side |
| 4230 | * (server line) |
| 4231 | * |
| 4232 | * Returns a bitfield containing the flags: |
| 4233 | * ERR_FATAL in any fatal error case |
| 4234 | * ERR_ALERT if the reason of the error is available in err |
| 4235 | * ERR_WARN if a warning is available into err |
| 4236 | */ |
| 4237 | 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] | 4238 | struct ckch_inst **ckchi, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4239 | { |
| 4240 | SSL_CTX *ctx; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4241 | struct ckch_data *data; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4242 | struct ckch_inst *ckch_inst = NULL; |
| 4243 | int errcode = 0; |
| 4244 | |
| 4245 | *ckchi = NULL; |
| 4246 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4247 | if (!ckchs || !ckchs->data) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4248 | return ERR_FATAL; |
| 4249 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4250 | data = ckchs->data; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4251 | |
| 4252 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 4253 | if (!ctx) { |
| 4254 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4255 | err && *err ? *err : "", path); |
| 4256 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4257 | goto error; |
| 4258 | } |
| 4259 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 4260 | 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] | 4261 | if (errcode & ERR_CODE) |
| 4262 | goto error; |
| 4263 | |
| 4264 | ckch_inst = ckch_inst_new(); |
| 4265 | if (!ckch_inst) { |
| 4266 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 4267 | err && *err ? *err : "", path); |
| 4268 | errcode |= ERR_ALERT | ERR_FATAL; |
| 4269 | goto error; |
| 4270 | } |
| 4271 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4272 | /* everything succeed, the ckch instance can be used */ |
| 4273 | ckch_inst->bind_conf = NULL; |
| 4274 | ckch_inst->ssl_conf = NULL; |
| 4275 | ckch_inst->ckch_store = ckchs; |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 4276 | ckch_inst->ctx = ctx; |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 4277 | ckch_inst->is_server_instance = 1; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4278 | |
| 4279 | *ckchi = ckch_inst; |
| 4280 | return errcode; |
| 4281 | |
| 4282 | error: |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4283 | SSL_CTX_free(ctx); |
| 4284 | |
| 4285 | return errcode; |
| 4286 | } |
| 4287 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4288 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4289 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 4290 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4291 | char **sni_filter, int fcount, struct ckch_inst **ckch_inst, char **err) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4292 | { |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4293 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4294 | |
| 4295 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | e7eb1fe | 2020-09-16 16:17:51 +0200 | [diff] [blame] | 4296 | 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] | 4297 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4298 | if (errcode & ERR_CODE) |
| 4299 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4300 | |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4301 | ssl_sock_load_cert_sni(*ckch_inst, bind_conf); |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4302 | |
| 4303 | /* succeed, add the instance to the ckch_store's list of instance */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4304 | LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4305 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4306 | } |
| 4307 | |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 4308 | /* This function generates a <struct ckch_inst *> for a <struct server *>, and |
| 4309 | * fill the SSL_CTX of the server. |
| 4310 | * |
| 4311 | * 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] | 4312 | 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] | 4313 | struct server *server, struct ckch_inst **ckch_inst, char **err) |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4314 | { |
| 4315 | int errcode = 0; |
| 4316 | |
| 4317 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 795bd9b | 2021-01-26 11:27:42 +0100 | [diff] [blame] | 4318 | 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] | 4319 | |
| 4320 | if (errcode & ERR_CODE) |
| 4321 | return errcode; |
| 4322 | |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 4323 | (*ckch_inst)->server = server; |
| 4324 | /* Keep the reference to the SSL_CTX in the server. */ |
| 4325 | SSL_CTX_up_ref((*ckch_inst)->ctx); |
| 4326 | server->ssl_ctx.ctx = (*ckch_inst)->ctx; |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4327 | /* succeed, add the instance to the ckch_store's list of instance */ |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4328 | LIST_APPEND(&ckchs->ckch_inst, &((*ckch_inst)->by_ckchs)); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4329 | return errcode; |
| 4330 | } |
| 4331 | |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4332 | |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 4333 | |
| 4334 | |
| 4335 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 4336 | * done once. Zero is returned if the operation fails. No error is returned |
| 4337 | * if the random is said as not implemented, because we expect that openssl |
| 4338 | * will use another method once needed. |
| 4339 | */ |
Amaury Denoyelle | c593bcd | 2021-05-19 15:35:29 +0200 | [diff] [blame] | 4340 | int ssl_initialize_random(void) |
William Lallemand | 4c68bba | 2020-03-30 18:45:10 +0200 | [diff] [blame] | 4341 | { |
| 4342 | unsigned char random; |
| 4343 | static int random_initialized = 0; |
| 4344 | |
| 4345 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 4346 | random_initialized = 1; |
| 4347 | |
| 4348 | return random_initialized; |
| 4349 | } |
| 4350 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4351 | /* Load a crt-list file, this is done in 2 parts: |
| 4352 | * - store the content of the file in a crtlist structure with crtlist_entry structures |
| 4353 | * - generate the instances by iterating on entries in the crtlist struct |
| 4354 | * |
| 4355 | * Nothing is locked there, this function is used in the configuration parser. |
| 4356 | * |
| 4357 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 4358 | */ |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4359 | 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] | 4360 | { |
| 4361 | struct crtlist *crtlist = NULL; |
| 4362 | struct ebmb_node *eb; |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 4363 | struct crtlist_entry *entry = NULL; |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 4364 | struct bind_conf_list *bind_conf_node = NULL; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4365 | int cfgerr = 0; |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 4366 | char *end; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4367 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 4368 | bind_conf_node = malloc(sizeof(*bind_conf_node)); |
| 4369 | if (!bind_conf_node) { |
| 4370 | memprintf(err, "%sCan't alloc memory!\n", err && *err ? *err : ""); |
| 4371 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 4372 | goto error; |
| 4373 | } |
| 4374 | bind_conf_node->next = NULL; |
| 4375 | bind_conf_node->bind_conf = bind_conf; |
| 4376 | |
William Lallemand | 41ca930 | 2020-04-08 13:15:18 +0200 | [diff] [blame] | 4377 | /* strip trailing slashes, including first one */ |
| 4378 | for (end = file + strlen(file) - 1; end >= file && *end == '/'; end--) |
| 4379 | *end = 0; |
| 4380 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4381 | /* look for an existing crtlist or create one */ |
| 4382 | eb = ebst_lookup(&crtlists_tree, file); |
| 4383 | if (eb) { |
| 4384 | crtlist = ebmb_entry(eb, struct crtlist, node); |
| 4385 | } else { |
William Lallemand | 6be66ec | 2020-03-06 22:26:32 +0100 | [diff] [blame] | 4386 | /* load a crt-list OR a directory */ |
| 4387 | if (dir) |
| 4388 | cfgerr |= crtlist_load_cert_dir(file, bind_conf, &crtlist, err); |
| 4389 | else |
| 4390 | cfgerr |= crtlist_parse_file(file, bind_conf, curproxy, &crtlist, err); |
| 4391 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4392 | if (!(cfgerr & ERR_CODE)) |
| 4393 | ebst_insert(&crtlists_tree, &crtlist->node); |
| 4394 | } |
| 4395 | |
| 4396 | if (cfgerr & ERR_CODE) { |
| 4397 | cfgerr |= ERR_FATAL | ERR_ALERT; |
| 4398 | goto error; |
| 4399 | } |
| 4400 | |
| 4401 | /* generates ckch instance from the crtlist_entry */ |
| 4402 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 4403 | struct ckch_store *store; |
| 4404 | struct ckch_inst *ckch_inst = NULL; |
| 4405 | |
| 4406 | store = entry->node.key; |
| 4407 | cfgerr |= ssl_sock_load_ckchs(store->path, store, bind_conf, entry->ssl_conf, entry->filters, entry->fcount, &ckch_inst, err); |
| 4408 | if (cfgerr & ERR_CODE) { |
| 4409 | memprintf(err, "error processing line %d in file '%s' : %s", entry->linenum, file, *err); |
| 4410 | goto error; |
| 4411 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4412 | LIST_APPEND(&entry->ckch_inst, &ckch_inst->by_crtlist_entry); |
William Lallemand | caa1619 | 2020-04-08 16:29:15 +0200 | [diff] [blame] | 4413 | ckch_inst->crtlist_entry = entry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4414 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4415 | |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 4416 | /* add the bind_conf to the list */ |
| 4417 | bind_conf_node->next = crtlist->bind_conf; |
| 4418 | crtlist->bind_conf = bind_conf_node; |
| 4419 | |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4420 | return cfgerr; |
| 4421 | error: |
| 4422 | { |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 4423 | struct crtlist_entry *lastentry; |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4424 | struct ckch_inst *inst, *s_inst; |
| 4425 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 4426 | lastentry = entry; /* which entry we tried to generate last */ |
| 4427 | if (lastentry) { |
| 4428 | list_for_each_entry(entry, &crtlist->ord_entries, by_crtlist) { |
| 4429 | if (entry == lastentry) /* last entry we tried to generate, no need to go further */ |
| 4430 | break; |
| 4431 | |
| 4432 | 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] | 4433 | |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 4434 | /* this was not generated for this bind_conf, skip */ |
| 4435 | if (inst->bind_conf != bind_conf) |
| 4436 | continue; |
| 4437 | |
William Lallemand | d9d5d1b | 2020-04-09 16:31:05 +0200 | [diff] [blame] | 4438 | /* free the sni_ctx and instance */ |
| 4439 | ckch_inst_free(inst); |
William Lallemand | 4939831 | 2020-03-30 17:01:33 +0200 | [diff] [blame] | 4440 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4441 | } |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4442 | } |
William Lallemand | 79d31ec | 2020-03-25 15:10:49 +0100 | [diff] [blame] | 4443 | free(bind_conf_node); |
William Lallemand | 2954c47 | 2020-03-06 21:54:13 +0100 | [diff] [blame] | 4444 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4445 | return cfgerr; |
| 4446 | } |
| 4447 | |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4448 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
| 4449 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
| 4450 | { |
| 4451 | struct stat buf; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4452 | int cfgerr = 0; |
| 4453 | struct ckch_store *ckchs; |
William Lallemand | 24bde43 | 2020-03-09 16:48:43 +0100 | [diff] [blame] | 4454 | struct ckch_inst *ckch_inst = NULL; |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 4455 | int found = 0; /* did we found a file to load ? */ |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4456 | |
| 4457 | if ((ckchs = ckchs_lookup(path))) { |
| 4458 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 4459 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 4460 | found++; |
| 4461 | } else if (stat(path, &buf) == 0) { |
| 4462 | found++; |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4463 | if (S_ISDIR(buf.st_mode) == 0) { |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 4464 | ckchs = ckchs_load_cert_file(path, err); |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4465 | if (!ckchs) |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 4466 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4467 | 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] | 4468 | } else { |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 4469 | 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] | 4470 | } |
| 4471 | } else { |
| 4472 | /* stat failed, could be a bundle */ |
| 4473 | if (global_ssl.extra_files & SSL_GF_BUNDLE) { |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 4474 | char fp[MAXPATHLEN+1] = {0}; |
| 4475 | int n = 0; |
| 4476 | |
| 4477 | /* Load all possible certs and keys in separate ckch_store */ |
| 4478 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 4479 | struct stat buf; |
| 4480 | int ret; |
| 4481 | |
| 4482 | ret = snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 4483 | if (ret > sizeof(fp)) |
| 4484 | continue; |
| 4485 | |
| 4486 | if ((ckchs = ckchs_lookup(fp))) { |
| 4487 | 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] | 4488 | found++; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 4489 | } else { |
| 4490 | if (stat(fp, &buf) == 0) { |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 4491 | found++; |
William Lallemand | bd8e6ed | 2020-09-16 16:08:08 +0200 | [diff] [blame] | 4492 | ckchs = ckchs_load_cert_file(fp, err); |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 4493 | if (!ckchs) |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 4494 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | dfa93be | 2020-09-16 14:48:52 +0200 | [diff] [blame] | 4495 | cfgerr |= ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, &ckch_inst, err); |
| 4496 | } |
| 4497 | } |
| 4498 | } |
William Lallemand | b7fdfdf | 2020-12-04 15:45:02 +0100 | [diff] [blame] | 4499 | #if HA_OPENSSL_VERSION_NUMBER < 0x10101000L |
| 4500 | if (found) { |
| 4501 | memprintf(err, "%sCan't load '%s'. Loading a multi certificates bundle requires OpenSSL >= 1.1.1\n", |
| 4502 | err && *err ? *err : "", path); |
| 4503 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4504 | } |
| 4505 | #endif |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4506 | } |
| 4507 | } |
William Lallemand | 06ce84a | 2020-11-20 15:36:13 +0100 | [diff] [blame] | 4508 | if (!found) { |
| 4509 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4510 | err && *err ? *err : "", path, strerror(errno)); |
| 4511 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4512 | } |
William Lallemand | 06b22a8 | 2020-03-16 14:45:55 +0100 | [diff] [blame] | 4513 | |
| 4514 | return cfgerr; |
| 4515 | } |
| 4516 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4517 | |
| 4518 | /* Create a full ssl context and ckch instance that will be used for a specific |
| 4519 | * backend server (server configuration line). |
| 4520 | * Returns a set of ERR_* flags possibly with an error in <err>. |
| 4521 | */ |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4522 | 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] | 4523 | { |
| 4524 | struct stat buf; |
| 4525 | int cfgerr = 0; |
| 4526 | struct ckch_store *ckchs; |
| 4527 | int found = 0; /* did we found a file to load ? */ |
| 4528 | |
| 4529 | if ((ckchs = ckchs_lookup(path))) { |
| 4530 | /* we found the ckchs in the tree, we can use it directly */ |
William Lallemand | db26e2b | 2021-01-26 12:01:46 +0100 | [diff] [blame] | 4531 | 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] | 4532 | found++; |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4533 | } else { |
| 4534 | if (!create_if_none) { |
| 4535 | memprintf(err, "%sunable to stat SSL certificate '%s'.\n", |
| 4536 | err && *err ? *err : "", path); |
| 4537 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4538 | goto out; |
| 4539 | } |
| 4540 | |
| 4541 | if (stat(path, &buf) == 0) { |
| 4542 | /* We do not manage directories on backend side. */ |
| 4543 | if (S_ISDIR(buf.st_mode) == 0) { |
| 4544 | ++found; |
| 4545 | ckchs = ckchs_load_cert_file(path, err); |
| 4546 | if (!ckchs) |
| 4547 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4548 | cfgerr |= ssl_sock_load_srv_ckchs(path, ckchs, server, &server->ssl_ctx.inst, err); |
| 4549 | } |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4550 | } |
| 4551 | } |
| 4552 | if (!found) { |
| 4553 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4554 | err && *err ? *err : "", path, strerror(errno)); |
| 4555 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 4556 | } |
| 4557 | |
Amaury Denoyelle | 36aa451 | 2021-05-21 16:22:11 +0200 | [diff] [blame] | 4558 | out: |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 4559 | return cfgerr; |
| 4560 | } |
| 4561 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4562 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4563 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4564 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4565 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4566 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4567 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4568 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4569 | SSL_OP_NO_SSLv2 | |
| 4570 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4571 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4572 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4573 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 4574 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4575 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4576 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4577 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4578 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4579 | SSL_MODE_RELEASE_BUFFERS | |
| 4580 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 4581 | 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] | 4582 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4583 | int flags = MC_SSL_O_ALL; |
| 4584 | int cfgerr = 0; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 4585 | const int default_min_ver = CONF_TLSV12; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4586 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4587 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4588 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4589 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4590 | 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] | 4591 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 4592 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4593 | 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] | 4594 | else |
| 4595 | flags = conf_ssl_methods->flags; |
| 4596 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 4597 | min = conf_ssl_methods->min; |
| 4598 | max = conf_ssl_methods->max; |
William Lallemand | 50df1cb | 2020-06-02 10:52:24 +0200 | [diff] [blame] | 4599 | |
| 4600 | /* default minimum is TLSV12, */ |
| 4601 | if (!min) { |
| 4602 | if (!max || (max >= default_min_ver)) { |
| 4603 | min = default_min_ver; |
| 4604 | } else { |
| 4605 | 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). " |
| 4606 | "Setting the ssl-min-ver to %s. Use 'ssl-min-ver' to fix this.\n", |
| 4607 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, methodVersions[default_min_ver].name, methodVersions[max].name); |
| 4608 | min = max; |
| 4609 | } |
| 4610 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4611 | /* 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] | 4612 | if (min) |
| 4613 | flags |= (methodVersions[min].flag - 1); |
| 4614 | if (max) |
| 4615 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4616 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4617 | min = max = CONF_TLSV_NONE; |
| 4618 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4619 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4620 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4621 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4622 | if (min) { |
| 4623 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4624 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 4625 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4626 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 4627 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4628 | hole = 0; |
| 4629 | } |
| 4630 | max = i; |
| 4631 | } |
| 4632 | else { |
| 4633 | min = max = i; |
| 4634 | } |
| 4635 | } |
| 4636 | else { |
| 4637 | if (min) |
| 4638 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4639 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4640 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4641 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4642 | 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] | 4643 | cfgerr += 1; |
| 4644 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4645 | /* save real min/max in bind_conf */ |
| 4646 | conf_ssl_methods->min = min; |
| 4647 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4648 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4649 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4650 | /* 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] | 4651 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4652 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4653 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4654 | else |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4655 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) { |
| 4656 | /* clear every version flags in case SSL_CTX_new() |
| 4657 | * returns an SSL_CTX with disabled versions */ |
| 4658 | SSL_CTX_clear_options(ctx, methodVersions[i].option); |
| 4659 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4660 | if (flags & methodVersions[i].flag) |
| 4661 | options |= methodVersions[i].option; |
William Lallemand | d0712f3 | 2020-06-11 17:34:00 +0200 | [diff] [blame] | 4662 | |
| 4663 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4664 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4665 | /* 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] | 4666 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4667 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 4668 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4669 | |
| 4670 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 4671 | options |= SSL_OP_NO_TICKET; |
| 4672 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 4673 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 4674 | |
| 4675 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 4676 | options |= SSL_OP_NO_RENEGOTIATION; |
| 4677 | #endif |
| 4678 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4679 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4680 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 4681 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4682 | if (global_ssl.async) |
| 4683 | mode |= SSL_MODE_ASYNC; |
| 4684 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4685 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4686 | if (global_ssl.life_time) |
| 4687 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4688 | |
| 4689 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4690 | # ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4691 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 4692 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4693 | # elif defined(HAVE_SSL_CLIENT_HELLO_CB) |
| 4694 | # if defined(SSL_OP_NO_ANTI_REPLAY) |
Olivier Houchard | 545989f | 2019-12-17 15:39:54 +0100 | [diff] [blame] | 4695 | if (bind_conf->ssl_conf.early_data) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 4696 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4697 | # endif /* ! SSL_OP_NO_ANTI_REPLAY */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 4698 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 4699 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4700 | # else /* ! OPENSSL_IS_BORINGSSL && ! HAVE_SSL_CLIENT_HELLO_CB */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4701 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4702 | # endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 4703 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
William Lallemand | 4b7938d | 2022-09-07 10:54:17 +0200 | [diff] [blame] | 4704 | #endif /* ! SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4705 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4706 | } |
| 4707 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4708 | |
| 4709 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 4710 | { |
| 4711 | if (first == block) { |
| 4712 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4713 | if (first->len > 0) |
| 4714 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 4715 | } |
| 4716 | } |
| 4717 | |
| 4718 | /* return first block from sh_ssl_sess */ |
| 4719 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 4720 | { |
| 4721 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 4722 | |
| 4723 | } |
| 4724 | |
| 4725 | /* store a session into the cache |
| 4726 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 4727 | * data: asn1 encoded session |
| 4728 | * data_len: asn1 encoded session length |
| 4729 | * Returns 1 id session was stored (else 0) |
| 4730 | */ |
| 4731 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 4732 | { |
| 4733 | struct shared_block *first; |
| 4734 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 4735 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4736 | 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] | 4737 | if (!first) { |
| 4738 | /* Could not retrieve enough free blocks to store that session */ |
| 4739 | return 0; |
| 4740 | } |
| 4741 | |
| 4742 | /* STORE the key in the first elem */ |
| 4743 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4744 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 4745 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4746 | |
| 4747 | /* it returns the already existing node |
| 4748 | or current node if none, never returns null */ |
| 4749 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4750 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4751 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4752 | /* release the reserved row */ |
| 4753 | shctx_row_dec_hot(ssl_shctx, first); |
| 4754 | /* replace the previous session already in the tree */ |
| 4755 | sh_ssl_sess = oldsh_ssl_sess; |
| 4756 | /* ignore the previous session data, only use the header */ |
| 4757 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4758 | shctx_row_inc_hot(ssl_shctx, first); |
| 4759 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4760 | } |
| 4761 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4762 | 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] | 4763 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4764 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4765 | } |
| 4766 | |
| 4767 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4768 | |
| 4769 | return 1; |
| 4770 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4771 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4772 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4773 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4774 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4775 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4776 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4777 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4778 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4779 | |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4780 | /* RWLOCK: only read lock the SSL cache even when writing in it because there is |
| 4781 | * one cache per thread, it only prevents to flush it from the CLI in |
| 4782 | * another thread */ |
| 4783 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4784 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4785 | int len; |
| 4786 | unsigned char *ptr; |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4787 | const char *sni; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4788 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4789 | len = i2d_SSL_SESSION(sess, NULL); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4790 | sni = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4791 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4792 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4793 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4794 | } else { |
Willy Tarreau | 3bda3f4 | 2021-02-26 21:05:08 +0100 | [diff] [blame] | 4795 | ptr = realloc(s->ssl_ctx.reused_sess[tid].ptr, len); |
| 4796 | s->ssl_ctx.reused_sess[tid].ptr = ptr; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4797 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4798 | } |
| 4799 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4800 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4801 | &ptr); |
| 4802 | } |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 4803 | |
| 4804 | if (s->ssl_ctx.reused_sess[tid].sni) { |
| 4805 | /* if the new sni is empty or isn' t the same as the old one */ |
| 4806 | if ((!sni) || strcmp(s->ssl_ctx.reused_sess[tid].sni, sni) != 0) { |
| 4807 | ha_free(&s->ssl_ctx.reused_sess[tid].sni); |
| 4808 | if (sni) |
| 4809 | s->ssl_ctx.reused_sess[tid].sni = strdup(sni); |
| 4810 | } |
| 4811 | } else if (sni) { |
| 4812 | /* if there wasn't an old sni but there is a new one */ |
| 4813 | s->ssl_ctx.reused_sess[tid].sni = strdup(sni); |
| 4814 | } |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4815 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4816 | } else { |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4817 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4818 | ha_free(&s->ssl_ctx.reused_sess[tid].ptr); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 4819 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4823 | } |
| 4824 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4825 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4826 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4827 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4828 | { |
| 4829 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4830 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4831 | unsigned char *p; |
| 4832 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4833 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4834 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4835 | |
| 4836 | /* 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] | 4837 | * so we don't store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4838 | * note: SSL_SESSION_set1_id is using |
| 4839 | * a memcpy so we need to use a different pointer |
| 4840 | * than sid_data or sid_ctx_data to avoid valgrind |
| 4841 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4842 | */ |
| 4843 | |
| 4844 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4845 | |
| 4846 | /* copy value in an other buffer */ |
| 4847 | memcpy(encid, sid_data, sid_length); |
| 4848 | |
| 4849 | /* pad with 0 */ |
| 4850 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4851 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4852 | |
| 4853 | /* force length to zero to avoid ASN1 encoding */ |
| 4854 | SSL_SESSION_set1_id(sess, encid, 0); |
| 4855 | |
| 4856 | /* force length to zero to avoid ASN1 encoding */ |
| 4857 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4858 | |
| 4859 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4860 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4861 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4862 | goto err; |
| 4863 | |
| 4864 | p = encsess; |
| 4865 | |
| 4866 | /* process ASN1 session encoding before the lock */ |
| 4867 | i2d_SSL_SESSION(sess, &p); |
| 4868 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4869 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4870 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4871 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4872 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4873 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4874 | err: |
| 4875 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4876 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 4877 | 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] | 4878 | |
| 4879 | return 0; /* do not increment session reference count */ |
| 4880 | } |
| 4881 | |
| 4882 | /* 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] | 4883 | 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] | 4884 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4885 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4886 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4887 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4888 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4889 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4890 | |
Willy Tarreau | 4c19e99 | 2021-06-15 16:39:22 +0200 | [diff] [blame] | 4891 | _HA_ATOMIC_INC(&global.shctx_lookups); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4892 | |
| 4893 | /* allow the session to be freed automatically by openssl */ |
| 4894 | *do_copy = 0; |
| 4895 | |
| 4896 | /* tree key is zeros padded sessionid */ |
| 4897 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4898 | memcpy(tmpkey, key, key_len); |
| 4899 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4900 | key = tmpkey; |
| 4901 | } |
| 4902 | |
| 4903 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4904 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4905 | |
| 4906 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4907 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4908 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4909 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4910 | shctx_unlock(ssl_shctx); |
Willy Tarreau | 4c19e99 | 2021-06-15 16:39:22 +0200 | [diff] [blame] | 4911 | _HA_ATOMIC_INC(&global.shctx_misses); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4912 | return NULL; |
| 4913 | } |
| 4914 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4915 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4916 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4917 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4918 | 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] | 4919 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4920 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4921 | |
| 4922 | /* decode ASN1 session */ |
| 4923 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4924 | 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] | 4925 | /* Reset session id and session id contenxt */ |
| 4926 | if (sess) { |
| 4927 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4928 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4929 | } |
| 4930 | |
| 4931 | return sess; |
| 4932 | } |
| 4933 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4934 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4935 | /* 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] | 4936 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4937 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4938 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4939 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4940 | unsigned int sid_length; |
| 4941 | const unsigned char *sid_data; |
| 4942 | (void)ctx; |
| 4943 | |
| 4944 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4945 | /* tree key is zeros padded sessionid */ |
| 4946 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4947 | memcpy(tmpkey, sid_data, sid_length); |
| 4948 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4949 | sid_data = tmpkey; |
| 4950 | } |
| 4951 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4952 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4953 | |
| 4954 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4955 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4956 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4957 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4958 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4959 | } |
| 4960 | |
| 4961 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4962 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | /* Set session cache mode to server and disable openssl internal cache. |
| 4966 | * Set shared cache callbacks on an ssl context. |
| 4967 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4968 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4969 | { |
| 4970 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4971 | |
| 4972 | if (!ssl_shctx) { |
| 4973 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4974 | return; |
| 4975 | } |
| 4976 | |
| 4977 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4978 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4979 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4980 | |
| 4981 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4982 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4983 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4984 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4985 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4986 | |
| 4987 | /* |
| 4988 | * https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format |
| 4989 | * |
| 4990 | * The format is: |
| 4991 | * * <Label> <space> <ClientRandom> <space> <Secret> |
| 4992 | * We only need to copy the secret as there is a sample fetch for the ClientRandom |
| 4993 | */ |
| 4994 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 4995 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 4996 | void SSL_CTX_keylog(const SSL *ssl, const char *line) |
| 4997 | { |
| 4998 | struct ssl_keylog *keylog; |
| 4999 | char *lastarg = NULL; |
| 5000 | char *dst = NULL; |
| 5001 | |
| 5002 | keylog = SSL_get_ex_data(ssl, ssl_keylog_index); |
| 5003 | if (!keylog) |
| 5004 | return; |
| 5005 | |
| 5006 | lastarg = strrchr(line, ' '); |
| 5007 | if (lastarg == NULL || ++lastarg == NULL) |
| 5008 | return; |
| 5009 | |
| 5010 | dst = pool_alloc(pool_head_ssl_keylog_str); |
| 5011 | if (!dst) |
| 5012 | return; |
| 5013 | |
| 5014 | strncpy(dst, lastarg, SSL_KEYLOG_MAX_SECRET_SIZE-1); |
| 5015 | dst[SSL_KEYLOG_MAX_SECRET_SIZE-1] = '\0'; |
| 5016 | |
| 5017 | if (strncmp(line, "CLIENT_RANDOM ", strlen("CLIENT RANDOM ")) == 0) { |
| 5018 | if (keylog->client_random) |
| 5019 | goto error; |
| 5020 | keylog->client_random = dst; |
| 5021 | |
| 5022 | } else if (strncmp(line, "CLIENT_EARLY_TRAFFIC_SECRET ", strlen("CLIENT_EARLY_TRAFFIC_SECRET ")) == 0) { |
| 5023 | if (keylog->client_early_traffic_secret) |
| 5024 | goto error; |
| 5025 | keylog->client_early_traffic_secret = dst; |
| 5026 | |
| 5027 | } else if (strncmp(line, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ", strlen("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 5028 | if(keylog->client_handshake_traffic_secret) |
| 5029 | goto error; |
| 5030 | keylog->client_handshake_traffic_secret = dst; |
| 5031 | |
| 5032 | } else if (strncmp(line, "SERVER_HANDSHAKE_TRAFFIC_SECRET ", strlen("SERVER_HANDSHAKE_TRAFFIC_SECRET ")) == 0) { |
| 5033 | if (keylog->server_handshake_traffic_secret) |
| 5034 | goto error; |
| 5035 | keylog->server_handshake_traffic_secret = dst; |
| 5036 | |
| 5037 | } else if (strncmp(line, "CLIENT_TRAFFIC_SECRET_0 ", strlen("CLIENT_TRAFFIC_SECRET_0 ")) == 0) { |
| 5038 | if (keylog->client_traffic_secret_0) |
| 5039 | goto error; |
| 5040 | keylog->client_traffic_secret_0 = dst; |
| 5041 | |
| 5042 | } else if (strncmp(line, "SERVER_TRAFFIC_SECRET_0 ", strlen("SERVER_TRAFFIC_SECRET_0 ")) == 0) { |
| 5043 | if (keylog->server_traffic_secret_0) |
| 5044 | goto error; |
| 5045 | keylog->server_traffic_secret_0 = dst; |
| 5046 | |
| 5047 | } else if (strncmp(line, "EARLY_EXPORTER_SECRET ", strlen("EARLY_EXPORTER_SECRET ")) == 0) { |
| 5048 | if (keylog->early_exporter_secret) |
| 5049 | goto error; |
| 5050 | keylog->early_exporter_secret = dst; |
| 5051 | |
| 5052 | } else if (strncmp(line, "EXPORTER_SECRET ", strlen("EXPORTER_SECRET ")) == 0) { |
| 5053 | if (keylog->exporter_secret) |
| 5054 | goto error; |
| 5055 | keylog->exporter_secret = dst; |
| 5056 | } else { |
| 5057 | goto error; |
| 5058 | } |
| 5059 | |
| 5060 | return; |
| 5061 | |
| 5062 | error: |
| 5063 | pool_free(pool_head_ssl_keylog_str, dst); |
| 5064 | |
| 5065 | return; |
| 5066 | } |
| 5067 | #endif |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 5068 | |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5069 | /* |
| 5070 | * This function applies the SSL configuration on a SSL_CTX |
| 5071 | * It returns an error code and fills the <err> buffer |
| 5072 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5073 | 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] | 5074 | { |
| 5075 | struct proxy *curproxy = bind_conf->frontend; |
| 5076 | int cfgerr = 0; |
| 5077 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 5078 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5079 | const char *conf_ciphers; |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 5080 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5081 | const char *conf_ciphersuites; |
| 5082 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5083 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5084 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5085 | if (ssl_conf) { |
| 5086 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 5087 | int i, min, max; |
| 5088 | int flags = MC_SSL_O_ALL; |
| 5089 | |
| 5090 | /* 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] | 5091 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 5092 | 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] | 5093 | if (min) |
| 5094 | flags |= (methodVersions[min].flag - 1); |
| 5095 | if (max) |
| 5096 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 5097 | min = max = CONF_TLSV_NONE; |
| 5098 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5099 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 5100 | if (min) |
| 5101 | max = i; |
| 5102 | else |
| 5103 | min = max = i; |
| 5104 | } |
| 5105 | /* save real min/max */ |
| 5106 | conf_ssl_methods->min = min; |
| 5107 | conf_ssl_methods->max = max; |
| 5108 | if (!min) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5109 | memprintf(err, "%sProxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 5110 | 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] | 5111 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 5112 | } |
| 5113 | } |
| 5114 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5115 | 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] | 5116 | case SSL_SOCK_VERIFY_NONE: |
| 5117 | verify = SSL_VERIFY_NONE; |
| 5118 | break; |
| 5119 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 5120 | verify = SSL_VERIFY_PEER; |
| 5121 | break; |
| 5122 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5123 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 5124 | break; |
| 5125 | } |
| 5126 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 5127 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5128 | 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] | 5129 | 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] | 5130 | 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] | 5131 | if (ca_file || ca_verify_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5132 | /* set CAfile to verify */ |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5133 | if (ca_file && !ssl_set_verify_locations_file(ctx, ca_file)) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5134 | 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] | 5135 | 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] | 5136 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5137 | } |
Emmanuel Hocdet | 842e94e | 2019-12-16 16:39:17 +0100 | [diff] [blame] | 5138 | if (ca_verify_file && !ssl_set_verify_locations_file(ctx, ca_verify_file)) { |
| 5139 | memprintf(err, "%sProxy '%s': unable to set CA-no-names file '%s' for bind '%s' at [%s:%d].\n", |
| 5140 | err && *err ? *err : "", curproxy->id, ca_verify_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 5141 | cfgerr |= ERR_ALERT | ERR_FATAL; |
| 5142 | } |
| 5143 | 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] | 5144 | /* set CA names for client cert request, function returns void */ |
Emmanuel Hocdet | 129d328 | 2019-10-24 18:08:51 +0200 | [diff] [blame] | 5145 | 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] | 5146 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5147 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5148 | else { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5149 | memprintf(err, "%sProxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 5150 | 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] | 5151 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5152 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5153 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5154 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5155 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 5156 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5157 | if (!ssl_set_cert_crl_file(store, crl_file)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5158 | memprintf(err, "%sProxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 5159 | 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] | 5160 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5161 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 5162 | else { |
| 5163 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5164 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5165 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 5166 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5167 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 5168 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5169 | #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] | 5170 | if(bind_conf->keys_ref) { |
Remi Tricot-Le Breton | 8ea1f5f | 2022-02-08 17:45:58 +0100 | [diff] [blame] | 5171 | 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] | 5172 | memprintf(err, "%sProxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 5173 | 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] | 5174 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 5175 | } |
| 5176 | } |
| 5177 | #endif |
| 5178 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 5179 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5180 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 5181 | if (conf_ciphers && |
| 5182 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5183 | memprintf(err, "%sProxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 5184 | 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] | 5185 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5186 | } |
| 5187 | |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 5188 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5189 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 5190 | if (conf_ciphersuites && |
| 5191 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5192 | memprintf(err, "%sProxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 5193 | 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] | 5194 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5195 | } |
| 5196 | #endif |
| 5197 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 5198 | #ifndef OPENSSL_NO_DH |
Remi Tricot-Le Breton | 1d6338e | 2022-04-12 11:31:55 +0200 | [diff] [blame] | 5199 | if (!local_dh_1024) |
| 5200 | local_dh_1024 = ssl_get_dh_1024(); |
| 5201 | if (!local_dh_2048) |
| 5202 | local_dh_2048 = ssl_get_dh_2048(); |
| 5203 | if (!local_dh_4096) |
| 5204 | local_dh_4096 = ssl_get_dh_4096(); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 5205 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 5206 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5207 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Ilya Shipitsin | 7ff7747 | 2021-02-08 16:55:06 +0500 | [diff] [blame] | 5208 | #ifdef SSL_CTRL_SET_MSG_CALLBACK |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5209 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 5210 | #endif |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 5211 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | b60a77b | 2022-11-18 15:00:15 +0100 | [diff] [blame] | 5212 | /* only activate the keylog callback if it was required to prevent performance loss */ |
| 5213 | if (global_ssl.keylog > 0) |
| 5214 | SSL_CTX_set_keylog_callback(ctx, SSL_CTX_keylog); |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 5215 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5216 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 5217 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5218 | ssl_conf_cur = NULL; |
| 5219 | if (ssl_conf && ssl_conf->npn_str) |
| 5220 | ssl_conf_cur = ssl_conf; |
| 5221 | else if (bind_conf->ssl_conf.npn_str) |
| 5222 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5223 | if (ssl_conf_cur) |
| 5224 | 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] | 5225 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 5226 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5227 | ssl_conf_cur = NULL; |
| 5228 | if (ssl_conf && ssl_conf->alpn_str) |
| 5229 | ssl_conf_cur = ssl_conf; |
| 5230 | else if (bind_conf->ssl_conf.alpn_str) |
| 5231 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5232 | if (ssl_conf_cur) |
| 5233 | 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] | 5234 | #endif |
Ilya Shipitsin | 0aa8c29 | 2020-11-04 00:39:07 +0500 | [diff] [blame] | 5235 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5236 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 5237 | if (conf_curves) { |
| 5238 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5239 | memprintf(err, "%sProxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 5240 | 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] | 5241 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5242 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 5243 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5244 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 5245 | #endif /* defined(SSL_CTX_set1_curves_list) */ |
| 5246 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5247 | if (!conf_curves) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5248 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 5249 | #if defined(SSL_CTX_set1_curves_list) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5250 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5251 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5252 | NULL); |
| 5253 | |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 5254 | if (ecdhe && SSL_CTX_set1_curves_list(ctx, ecdhe) == 0) { |
| 5255 | memprintf(err, "%sProxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 5256 | err && *err ? *err : "", curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 5257 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5258 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 5259 | #endif /* defined(SSL_CTX_set1_curves_list) */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5260 | #else |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 5261 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 5262 | int i; |
| 5263 | EC_KEY *ecdh; |
| 5264 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5265 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 5266 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5267 | ECDHE_DEFAULT_CURVE); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5268 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5269 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5270 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Tim Duesterhus | 9312853 | 2019-11-23 23:45:10 +0100 | [diff] [blame] | 5271 | 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] | 5272 | 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] | 5273 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5274 | } |
| 5275 | else { |
| 5276 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 5277 | EC_KEY_free(ecdh); |
| 5278 | } |
Remi Tricot-Le Breton | ff4c3c4 | 2022-02-08 17:45:54 +0100 | [diff] [blame] | 5279 | #endif /* defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) */ |
| 5280 | #endif /* HA_OPENSSL_VERSION_NUMBER >= 0x10101000L */ |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5281 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5282 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5283 | return cfgerr; |
| 5284 | } |
| 5285 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5286 | |
| 5287 | /* |
| 5288 | * Prepare the SSL_CTX based on the bind line configuration. |
| 5289 | * Since the CA file loading is made depending on the verify option of the bind |
| 5290 | * line, the link between the SSL_CTX and the CA file tree entry is made here. |
| 5291 | * If we want to create a link between the CA file entry and the corresponding |
| 5292 | * ckch instance (for CA file hot update), it needs to be done after |
| 5293 | * ssl_sock_prepare_ctx. |
| 5294 | * Returns 0 in case of success. |
| 5295 | */ |
| 5296 | int ssl_sock_prep_ctx_and_inst(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 5297 | SSL_CTX *ctx, struct ckch_inst *ckch_inst, char **err) |
| 5298 | { |
| 5299 | int errcode = 0; |
| 5300 | |
| 5301 | errcode |= ssl_sock_prepare_ctx(bind_conf, ssl_conf, ctx, err); |
| 5302 | if (!errcode && ckch_inst) |
| 5303 | ckch_inst_add_cafile_link(ckch_inst, bind_conf, ssl_conf, NULL); |
| 5304 | |
| 5305 | return errcode; |
| 5306 | } |
| 5307 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5308 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 5309 | { |
| 5310 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 5311 | size_t prefixlen, suffixlen; |
| 5312 | |
| 5313 | /* Trivial case */ |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 5314 | if (strcasecmp(pattern, hostname) == 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5315 | return 1; |
| 5316 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5317 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 5318 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 5319 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5320 | pattern_wildcard = NULL; |
| 5321 | pattern_left_label_end = pattern; |
| 5322 | while (*pattern_left_label_end != '.') { |
| 5323 | switch (*pattern_left_label_end) { |
| 5324 | case 0: |
| 5325 | /* End of label not found */ |
| 5326 | return 0; |
| 5327 | case '*': |
| 5328 | /* If there is more than one wildcards */ |
| 5329 | if (pattern_wildcard) |
| 5330 | return 0; |
| 5331 | pattern_wildcard = pattern_left_label_end; |
| 5332 | break; |
| 5333 | } |
| 5334 | pattern_left_label_end++; |
| 5335 | } |
| 5336 | |
| 5337 | /* If it's not trivial and there is no wildcard, it can't |
| 5338 | * match */ |
| 5339 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5340 | return 0; |
| 5341 | |
| 5342 | /* Make sure all labels match except the leftmost */ |
| 5343 | hostname_left_label_end = strchr(hostname, '.'); |
| 5344 | if (!hostname_left_label_end |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 5345 | || strcasecmp(pattern_left_label_end, hostname_left_label_end) != 0) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5346 | return 0; |
| 5347 | |
| 5348 | /* Make sure the leftmost label of the hostname is long enough |
| 5349 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 5350 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5351 | return 0; |
| 5352 | |
| 5353 | /* Finally compare the string on either side of the |
| 5354 | * wildcard */ |
| 5355 | prefixlen = pattern_wildcard - pattern; |
| 5356 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
William Lallemand | 2d6fd0a | 2020-09-14 15:20:10 +0200 | [diff] [blame] | 5357 | if ((prefixlen && (strncasecmp(pattern, hostname, prefixlen) != 0)) |
| 5358 | || (suffixlen && (strncasecmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5359 | return 0; |
| 5360 | |
| 5361 | return 1; |
| 5362 | } |
| 5363 | |
| 5364 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 5365 | { |
| 5366 | SSL *ssl; |
| 5367 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5368 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5369 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5370 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5371 | |
| 5372 | int depth; |
| 5373 | X509 *cert; |
| 5374 | STACK_OF(GENERAL_NAME) *alt_names; |
| 5375 | int i; |
| 5376 | X509_NAME *cert_subject; |
| 5377 | char *str; |
| 5378 | |
| 5379 | if (ok == 0) |
| 5380 | return ok; |
| 5381 | |
| 5382 | 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] | 5383 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 5384 | ssl_ctx = __conn_get_ssl_sock_ctx(conn); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5385 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 5386 | /* We're checking if the provided hostnames match the desired one. The |
| 5387 | * desired hostname comes from the SNI we presented if any, or if not |
| 5388 | * provided then it may have been explicitly stated using a "verifyhost" |
| 5389 | * directive. If neither is set, we don't care about the name so the |
| 5390 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5391 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5392 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5393 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5394 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5395 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5396 | if (!servername) |
| 5397 | return ok; |
| 5398 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5399 | |
| 5400 | /* We only need to verify the CN on the actual server cert, |
| 5401 | * not the indirect CAs */ |
| 5402 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 5403 | if (depth != 0) |
| 5404 | return ok; |
| 5405 | |
| 5406 | /* At this point, the cert is *not* OK unless we can find a |
| 5407 | * hostname match */ |
| 5408 | ok = 0; |
| 5409 | |
| 5410 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 5411 | /* It seems like this might happen if verify peer isn't set */ |
| 5412 | if (!cert) |
| 5413 | return ok; |
| 5414 | |
| 5415 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 5416 | if (alt_names) { |
| 5417 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 5418 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 5419 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5420 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5421 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 5422 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5423 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5424 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5425 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5426 | OPENSSL_free(str); |
| 5427 | } |
| 5428 | } |
| 5429 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 5430 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | cert_subject = X509_get_subject_name(cert); |
| 5434 | i = -1; |
| 5435 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 5436 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5437 | ASN1_STRING *value; |
| 5438 | value = X509_NAME_ENTRY_get_data(entry); |
| 5439 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5440 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5441 | OPENSSL_free(str); |
| 5442 | } |
| 5443 | } |
| 5444 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5445 | /* report the mismatch and indicate if SNI was used or not */ |
| 5446 | if (!ok && !conn->err_code) |
| 5447 | 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] | 5448 | return ok; |
| 5449 | } |
| 5450 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5451 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5452 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5453 | { |
| 5454 | int cfgerr = 0; |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 5455 | SSL_CTX *ctx; |
William Lallemand | 0adafb3 | 2022-12-13 18:17:44 +0100 | [diff] [blame] | 5456 | /* Automatic memory computations need to know we use SSL there |
| 5457 | * 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] | 5458 | if (!(srv->proxy->cap & PR_CAP_INT)) |
William Lallemand | 0adafb3 | 2022-12-13 18:17:44 +0100 | [diff] [blame] | 5459 | global.ssl_used_backend = 1; |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5460 | |
| 5461 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5462 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5463 | 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] | 5464 | ha_alert("out of memory.\n"); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5465 | cfgerr++; |
| 5466 | return cfgerr; |
| 5467 | } |
| 5468 | } |
Christopher Faulet | f61f33a | 2020-03-27 18:55:49 +0100 | [diff] [blame] | 5469 | if (srv->use_ssl == 1) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5470 | srv->xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5471 | |
William Lallemand | 2c776f1 | 2021-12-28 18:47:17 +0100 | [diff] [blame] | 5472 | if (srv->ssl_ctx.client_crt) { |
| 5473 | const int create_if_none = srv->flags & SRV_F_DYNAMIC ? 0 : 1; |
| 5474 | char *err = NULL; |
| 5475 | int err_code = 0; |
| 5476 | |
| 5477 | /* If there is a crt keyword there, the SSL_CTX will be created here. */ |
| 5478 | err_code = ssl_sock_load_srv_cert(srv->ssl_ctx.client_crt, srv, create_if_none, &err); |
| 5479 | if (err_code != ERR_NONE) { |
| 5480 | if ((err_code & ERR_WARN) && !(err_code & ERR_ALERT)) |
| 5481 | ha_warning("%s", err); |
| 5482 | else |
| 5483 | ha_alert("%s", err); |
| 5484 | |
| 5485 | if (err_code & (ERR_FATAL|ERR_ABORT)) |
| 5486 | cfgerr++; |
| 5487 | } |
| 5488 | ha_free(&err); |
| 5489 | } |
| 5490 | |
| 5491 | ctx = srv->ssl_ctx.ctx; |
| 5492 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 5493 | /* The context will be uninitialized if there wasn't any "cert" option |
| 5494 | * in the server line. */ |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5495 | if (!ctx) { |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 5496 | ctx = SSL_CTX_new(SSLv23_client_method()); |
| 5497 | if (!ctx) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5498 | ha_alert("unable to allocate ssl context.\n"); |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 5499 | cfgerr++; |
| 5500 | return cfgerr; |
| 5501 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5502 | |
Remi Tricot-Le Breton | d817dc7 | 2021-01-25 17:19:43 +0100 | [diff] [blame] | 5503 | srv->ssl_ctx.ctx = ctx; |
| 5504 | } |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5505 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5506 | 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] | 5507 | |
| 5508 | return cfgerr; |
| 5509 | } |
| 5510 | |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5511 | /* Initialize an SSL context that will be used on the backend side. |
| 5512 | * Returns an error count. |
| 5513 | */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5514 | 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] | 5515 | { |
| 5516 | struct proxy *curproxy = srv->proxy; |
| 5517 | int cfgerr = 0; |
| 5518 | long options = |
| 5519 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5520 | SSL_OP_NO_SSLv2 | |
| 5521 | SSL_OP_NO_COMPRESSION; |
| 5522 | long mode = |
| 5523 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5524 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
| 5525 | SSL_MODE_RELEASE_BUFFERS | |
| 5526 | SSL_MODE_SMALL_BUFFERS; |
| 5527 | int verify = SSL_VERIFY_NONE; |
| 5528 | const struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
| 5529 | int i, min, max, hole; |
| 5530 | int flags = MC_SSL_O_ALL; |
| 5531 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5532 | 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] | 5533 | ha_warning("no-sslv3/no-tlsv1x are ignored for this server. " |
| 5534 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n"); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5535 | else |
| 5536 | flags = conf_ssl_methods->flags; |
| 5537 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5538 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5539 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5540 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5541 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5542 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5543 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5544 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5545 | min = max = CONF_TLSV_NONE; |
| 5546 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5547 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5548 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5549 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5550 | if (min) { |
| 5551 | if (hole) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 5552 | 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] | 5553 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5554 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5555 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5556 | hole = 0; |
| 5557 | } |
| 5558 | max = i; |
| 5559 | } |
| 5560 | else { |
| 5561 | min = max = i; |
| 5562 | } |
| 5563 | } |
| 5564 | else { |
| 5565 | if (min) |
| 5566 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5567 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5568 | if (!min) { |
Amaury Denoyelle | 1112430 | 2021-06-04 18:22:08 +0200 | [diff] [blame] | 5569 | 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] | 5570 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5571 | cfgerr += 1; |
| 5572 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5573 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5574 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5575 | /* 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] | 5576 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5577 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5578 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5579 | else |
| 5580 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5581 | if (flags & methodVersions[i].flag) |
| 5582 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5583 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5584 | /* 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] | 5585 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5586 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5587 | #endif |
| 5588 | |
| 5589 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5590 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5591 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5592 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5593 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5594 | if (global_ssl.async) |
| 5595 | mode |= SSL_MODE_ASYNC; |
| 5596 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5597 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5598 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5599 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 5600 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5601 | switch (srv->ssl_ctx.verify) { |
| 5602 | case SSL_SOCK_VERIFY_NONE: |
| 5603 | verify = SSL_VERIFY_NONE; |
| 5604 | break; |
| 5605 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5606 | verify = SSL_VERIFY_PEER; |
| 5607 | break; |
| 5608 | } |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5609 | SSL_CTX_set_verify(ctx, verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5610 | (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] | 5611 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5612 | if (srv->ssl_ctx.ca_file) { |
Emmanuel Hocdet | d4f9a60 | 2019-10-24 11:32:47 +0200 | [diff] [blame] | 5613 | /* set CAfile to verify */ |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5614 | if (!ssl_set_verify_locations_file(ctx, srv->ssl_ctx.ca_file)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5615 | ha_alert("unable to set CA file '%s'.\n", |
| 5616 | srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5617 | cfgerr++; |
| 5618 | } |
| 5619 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5620 | else { |
| 5621 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5622 | 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] | 5623 | else |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5624 | ha_alert("verify is enabled but no CA file specified.\n"); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5625 | cfgerr++; |
| 5626 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5627 | #ifdef X509_V_FLAG_CRL_CHECK |
| 5628 | if (srv->ssl_ctx.crl_file) { |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5629 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5630 | |
Emmanuel Hocdet | b270e81 | 2019-11-21 19:09:31 +0100 | [diff] [blame] | 5631 | if (!ssl_set_cert_crl_file(store, srv->ssl_ctx.crl_file)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5632 | ha_alert("unable to configure CRL file '%s'.\n", |
| 5633 | srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5634 | cfgerr++; |
| 5635 | } |
| 5636 | else { |
| 5637 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5638 | } |
| 5639 | } |
| 5640 | #endif |
| 5641 | } |
| 5642 | |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5643 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_CLIENT | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 5644 | SSL_CTX_sess_set_new_cb(ctx, ssl_sess_new_srv_cb); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5645 | if (srv->ssl_ctx.ciphers && |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5646 | !SSL_CTX_set_cipher_list(ctx, srv->ssl_ctx.ciphers)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5647 | ha_alert("unable to set SSL cipher list to '%s'.\n", |
| 5648 | srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5649 | cfgerr++; |
| 5650 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5651 | |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 5652 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5653 | if (srv->ssl_ctx.ciphersuites && |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5654 | !SSL_CTX_set_ciphersuites(ctx, srv->ssl_ctx.ciphersuites)) { |
Amaury Denoyelle | e74cbc3 | 2021-05-28 10:34:01 +0200 | [diff] [blame] | 5655 | ha_alert("unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 5656 | srv->ssl_ctx.ciphersuites); |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5657 | cfgerr++; |
| 5658 | } |
| 5659 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5660 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 5661 | if (srv->ssl_ctx.npn_str) |
Remi Tricot-Le Breton | 442b7f2 | 2021-01-25 17:19:41 +0100 | [diff] [blame] | 5662 | 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] | 5663 | #endif |
| 5664 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5665 | if (srv->ssl_ctx.alpn_str) |
| 5666 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 5667 | #endif |
| 5668 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5669 | |
| 5670 | return cfgerr; |
| 5671 | } |
| 5672 | |
| 5673 | /* |
| 5674 | * Prepare the frontend's SSL_CTX based on the server line configuration. |
| 5675 | * Since the CA file loading is made depending on the verify option of the |
| 5676 | * server line, the link between the SSL_CTX and the CA file tree entry is |
| 5677 | * made here. |
| 5678 | * If we want to create a link between the CA file entry and the corresponding |
| 5679 | * ckch instance (for CA file hot update), it needs to be done after |
| 5680 | * ssl_sock_prepare_srv_ssl_ctx. |
| 5681 | * Returns an error count. |
| 5682 | */ |
| 5683 | int ssl_sock_prep_srv_ctx_and_inst(const struct server *srv, SSL_CTX *ctx, |
| 5684 | struct ckch_inst *ckch_inst) |
| 5685 | { |
| 5686 | int cfgerr = 0; |
| 5687 | |
| 5688 | cfgerr += ssl_sock_prepare_srv_ssl_ctx(srv, ctx); |
| 5689 | if (!cfgerr && ckch_inst) |
| 5690 | ckch_inst_add_cafile_link(ckch_inst, NULL, NULL, srv); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5691 | |
| 5692 | return cfgerr; |
| 5693 | } |
| 5694 | |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5695 | |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5696 | /* |
| 5697 | * Create an initial CTX used to start the SSL connections. |
| 5698 | * May be used by QUIC xprt which makes usage of SSL sessions initialized from SSL_CTXs. |
| 5699 | * Returns 0 if succeeded, or something >0 if not. |
| 5700 | */ |
| 5701 | #ifdef USE_QUIC |
| 5702 | static int ssl_initial_ctx(struct bind_conf *bind_conf) |
| 5703 | { |
| 5704 | if (bind_conf->xprt == xprt_get(XPRT_QUIC)) |
| 5705 | return ssl_quic_initial_ctx(bind_conf); |
| 5706 | else |
| 5707 | return ssl_sock_initial_ctx(bind_conf); |
| 5708 | } |
| 5709 | #else |
| 5710 | static int ssl_initial_ctx(struct bind_conf *bind_conf) |
| 5711 | { |
| 5712 | return ssl_sock_initial_ctx(bind_conf); |
| 5713 | } |
| 5714 | #endif |
| 5715 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5716 | /* 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] | 5717 | * be NULL, in which case nothing is done. Returns the number of errors |
| 5718 | * encountered. |
| 5719 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5720 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5721 | { |
| 5722 | struct ebmb_node *node; |
| 5723 | struct sni_ctx *sni; |
| 5724 | int err = 0; |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5725 | int errcode = 0; |
| 5726 | char *errmsg = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5727 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5728 | /* Automatic memory computations need to know we use SSL there */ |
| 5729 | global.ssl_used_frontend = 1; |
| 5730 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5731 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 5732 | if (!bind_conf->initial_ctx) { |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5733 | err += ssl_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5734 | /* It should not be necessary to call this function, but it's |
| 5735 | necessary first to check and move all initialisation related |
Frédéric Lécaille | ec21652 | 2020-11-23 14:33:30 +0100 | [diff] [blame] | 5736 | to initial_ctx in ssl_initial_ctx. */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5737 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, NULL, bind_conf->initial_ctx, NULL, &errmsg); |
| 5738 | } |
| 5739 | if (bind_conf->default_ctx) { |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 5740 | 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] | 5741 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5742 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5743 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5744 | while (node) { |
| 5745 | sni = ebmb_entry(node, struct sni_ctx, name); |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5746 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5747 | /* only initialize the CTX on its first occurrence and |
| 5748 | if it is not the default_ctx */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5749 | errcode |= ssl_sock_prep_ctx_and_inst(bind_conf, sni->conf, sni->ctx, sni->ckch_inst, &errmsg); |
| 5750 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5751 | node = ebmb_next(node); |
| 5752 | } |
| 5753 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5754 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5755 | while (node) { |
| 5756 | sni = ebmb_entry(node, struct sni_ctx, name); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5757 | if (!sni->order && sni->ctx != bind_conf->default_ctx) { |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5758 | /* only initialize the CTX on its first occurrence and |
| 5759 | if it is not the default_ctx */ |
Remi Tricot-Le Breton | 4458b97 | 2021-02-19 17:41:55 +0100 | [diff] [blame] | 5760 | 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] | 5761 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5762 | node = ebmb_next(node); |
| 5763 | } |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5764 | |
| 5765 | if (errcode & ERR_WARN) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5766 | ha_warning("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5767 | } else if (errcode & ERR_CODE) { |
Tim Duesterhus | c0e820c | 2019-11-23 23:52:30 +0100 | [diff] [blame] | 5768 | ha_alert("%s", errmsg); |
William Lallemand | 8b45391 | 2019-11-21 15:48:10 +0100 | [diff] [blame] | 5769 | err++; |
| 5770 | } |
| 5771 | |
| 5772 | free(errmsg); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5773 | return err; |
| 5774 | } |
| 5775 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5776 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 5777 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 5778 | * alerts are directly emitted since the rest of the stack does it below. |
| 5779 | */ |
| 5780 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 5781 | { |
| 5782 | struct proxy *px = bind_conf->frontend; |
| 5783 | int alloc_ctx; |
| 5784 | int err; |
| 5785 | |
Willy Tarreau | 11ba404 | 2022-05-20 15:56:32 +0200 | [diff] [blame] | 5786 | if (!(bind_conf->options & BC_O_USE_SSL)) { |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5787 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5788 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 5789 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5790 | } |
| 5791 | return 0; |
| 5792 | } |
| 5793 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5794 | if (bind_conf->strict_sni && !(bind_conf->options & BC_O_GENERATE_CERTS)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5795 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 5796 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5797 | } |
| 5798 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5799 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 5800 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5801 | return -1; |
| 5802 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5803 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 5804 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5805 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 5806 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
Willy Tarreau | 9135859 | 2021-06-15 08:08:04 +0200 | [diff] [blame] | 5807 | sizeof(*sh_ssl_sess_tree), (global.nbthread > 1)); |
Frédéric Lécaille | 4c8aa11 | 2018-10-25 20:22:46 +0200 | [diff] [blame] | 5808 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5809 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 5810 | 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"); |
| 5811 | else |
| 5812 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 5813 | return -1; |
| 5814 | } |
| 5815 | /* free block callback */ |
| 5816 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 5817 | /* init the root tree within the extra space */ |
| 5818 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 5819 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5820 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5821 | err = 0; |
| 5822 | /* initialize all certificate contexts */ |
| 5823 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 5824 | |
| 5825 | /* initialize CA variables if the certificates generation is enabled */ |
| 5826 | err += ssl_sock_load_ca(bind_conf); |
| 5827 | |
| 5828 | return -err; |
| 5829 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5830 | |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5831 | /* release ssl context allocated for servers. Most of the field free here |
| 5832 | * must also be allocated in srv_ssl_settings_cpy() */ |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5833 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 5834 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5835 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5836 | ha_free(&srv->ssl_ctx.alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5837 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 5838 | #ifdef OPENSSL_NPN_NEGOTIATED |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5839 | ha_free(&srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 5840 | #endif |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 5841 | if (srv->ssl_ctx.reused_sess) { |
| 5842 | int i; |
| 5843 | |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 5844 | for (i = 0; i < global.nbthread; i++) { |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5845 | ha_free(&srv->ssl_ctx.reused_sess[i].ptr); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 5846 | ha_free(&srv->ssl_ctx.reused_sess[i].sni); |
| 5847 | } |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5848 | ha_free(&srv->ssl_ctx.reused_sess); |
Christopher Faulet | 58feb49 | 2020-10-07 13:20:23 +0200 | [diff] [blame] | 5849 | } |
| 5850 | |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5851 | if (srv->ssl_ctx.ctx) { |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5852 | SSL_CTX_free(srv->ssl_ctx.ctx); |
Willy Tarreau | e709e82 | 2021-02-26 21:06:32 +0100 | [diff] [blame] | 5853 | srv->ssl_ctx.ctx = NULL; |
| 5854 | } |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5855 | |
| 5856 | ha_free(&srv->ssl_ctx.ca_file); |
| 5857 | ha_free(&srv->ssl_ctx.crl_file); |
| 5858 | ha_free(&srv->ssl_ctx.client_crt); |
| 5859 | ha_free(&srv->ssl_ctx.verify_host); |
| 5860 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 5861 | ha_free(&srv->sni_expr); |
William Lallemand | 43c2ce4 | 2022-03-16 17:48:19 +0100 | [diff] [blame] | 5862 | release_sample_expr(srv->ssl_ctx.sni); |
| 5863 | srv->ssl_ctx.sni = NULL; |
William Lallemand | 231610a | 2021-12-30 11:25:43 +0100 | [diff] [blame] | 5864 | #endif |
| 5865 | ha_free(&srv->ssl_ctx.ciphers); |
| 5866 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
| 5867 | ha_free(&srv->ssl_ctx.ciphersuites); |
| 5868 | #endif |
William Lallemand | e69563f | 2021-12-30 14:45:19 +0100 | [diff] [blame] | 5869 | /* If there is a certificate we must unlink the ckch instance */ |
| 5870 | ckch_inst_free(srv->ssl_ctx.inst); |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5871 | } |
| 5872 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5873 | /* 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] | 5874 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 5875 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5876 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5877 | { |
| 5878 | struct ebmb_node *node, *back; |
| 5879 | struct sni_ctx *sni; |
| 5880 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5881 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5882 | while (node) { |
| 5883 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5884 | back = ebmb_next(node); |
| 5885 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5886 | SSL_CTX_free(sni->ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5887 | LIST_DELETE(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5888 | free(sni); |
| 5889 | node = back; |
| 5890 | } |
| 5891 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5892 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5893 | while (node) { |
| 5894 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5895 | back = ebmb_next(node); |
| 5896 | ebmb_delete(node); |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5897 | SSL_CTX_free(sni->ctx); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5898 | LIST_DELETE(&sni->by_ckch_inst); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5899 | free(sni); |
| 5900 | node = back; |
| 5901 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 5902 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5903 | SSL_CTX_free(bind_conf->initial_ctx); |
| 5904 | bind_conf->initial_ctx = NULL; |
William Lallemand | 02e19a5 | 2020-04-08 16:11:26 +0200 | [diff] [blame] | 5905 | SSL_CTX_free(bind_conf->default_ctx); |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5906 | bind_conf->default_ctx = NULL; |
Remi Tricot-Le Breton | 40ddea8 | 2021-04-13 16:07:29 +0200 | [diff] [blame] | 5907 | bind_conf->default_inst = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5908 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5909 | } |
William Lallemand | b240869 | 2020-06-24 09:54:29 +0200 | [diff] [blame] | 5910 | |
| 5911 | |
| 5912 | void ssl_sock_deinit() |
| 5913 | { |
| 5914 | crtlist_deinit(); /* must be free'd before the ckchs */ |
| 5915 | ckch_deinit(); |
| 5916 | } |
| 5917 | REGISTER_POST_DEINIT(ssl_sock_deinit); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5918 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5919 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5920 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5921 | { |
| 5922 | ssl_sock_free_ca(bind_conf); |
| 5923 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5924 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5925 | free(bind_conf->ca_sign_file); |
| 5926 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5927 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5928 | free(bind_conf->keys_ref->filename); |
| 5929 | free(bind_conf->keys_ref->tlskeys); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 5930 | LIST_DELETE(&bind_conf->keys_ref->list); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5931 | free(bind_conf->keys_ref); |
| 5932 | } |
| 5933 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5934 | bind_conf->ca_sign_pass = NULL; |
| 5935 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5936 | } |
| 5937 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5938 | /* Load CA cert file and private key used to generate certificates */ |
| 5939 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5940 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5941 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5942 | struct proxy *px = bind_conf->frontend; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5943 | struct ckch_data *data = NULL; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5944 | int ret = 0; |
| 5945 | char *err = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5946 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5947 | if (!(bind_conf->options & BC_O_GENERATE_CERTS)) |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5948 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5949 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5950 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5951 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5952 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5953 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5954 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5955 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5956 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5957 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5958 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5959 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5960 | "no CA certificate File configured at [%s:%d].\n", |
| 5961 | px->id, bind_conf->file, bind_conf->line); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5962 | goto failed; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5963 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5964 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5965 | /* Allocate cert structure */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5966 | data = calloc(1, sizeof(*data)); |
| 5967 | if (!data) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5968 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain allocation failure\n", |
| 5969 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 5970 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5971 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5972 | |
| 5973 | /* Try to parse file */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5974 | 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] | 5975 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain loading failed: %s\n", |
| 5976 | 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] | 5977 | free(err); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5978 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5979 | } |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5980 | |
| 5981 | /* Fail if missing cert or pkey */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5982 | if ((!data->cert) || (!data->key)) { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5983 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d]. Chain missing certificate or private key\n", |
| 5984 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
| 5985 | goto failed; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5986 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5987 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5988 | /* Final assignment to bind */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5989 | bind_conf->ca_sign_ckch = data; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5990 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5991 | |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5992 | failed: |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 5993 | if (data) { |
| 5994 | ssl_sock_free_cert_key_and_chain_contents(data); |
| 5995 | free(data); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5996 | } |
| 5997 | |
Willy Tarreau | 1ea6e6a | 2022-05-20 16:03:18 +0200 | [diff] [blame] | 5998 | bind_conf->options &= ~BC_O_GENERATE_CERTS; |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 5999 | ret++; |
| 6000 | return ret; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | /* Release CA cert and private key used to generate certificated */ |
| 6004 | void |
| 6005 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 6006 | { |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 6007 | if (bind_conf->ca_sign_ckch) { |
| 6008 | 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] | 6009 | ha_free(&bind_conf->ca_sign_ckch); |
Shimi Gersner | 5846c49 | 2020-08-23 13:58:12 +0300 | [diff] [blame] | 6010 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 6011 | } |
| 6012 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6013 | /* |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 6014 | * Try to allocate the BIO and SSL session objects of <conn> connection with <bio> and |
| 6015 | * <ssl> as addresses, <bio_meth> as BIO method and <ssl_ctx> as SSL context inherited settings. |
| 6016 | * Connect the allocated BIO to the allocated SSL session. Also set <ctx> as address of custom |
| 6017 | * 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] | 6018 | * 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] | 6019 | * as parameters to this function. |
| 6020 | * Return 0 if succeeded, -1 if not. If failed, sets the ->err_code member of <conn> to |
| 6021 | * CO_ER_SSL_NO_MEM. |
| 6022 | */ |
| 6023 | int ssl_bio_and_sess_init(struct connection *conn, SSL_CTX *ssl_ctx, |
| 6024 | SSL **ssl, BIO **bio, BIO_METHOD *bio_meth, void *ctx) |
| 6025 | { |
| 6026 | int retry = 1; |
| 6027 | |
| 6028 | retry: |
| 6029 | /* Alloc a new SSL session. */ |
| 6030 | *ssl = SSL_new(ssl_ctx); |
| 6031 | if (!*ssl) { |
| 6032 | if (!retry--) |
| 6033 | goto err; |
| 6034 | |
| 6035 | pool_gc(NULL); |
| 6036 | goto retry; |
| 6037 | } |
| 6038 | |
| 6039 | *bio = BIO_new(bio_meth); |
| 6040 | if (!*bio) { |
| 6041 | SSL_free(*ssl); |
| 6042 | *ssl = NULL; |
| 6043 | if (!retry--) |
| 6044 | goto err; |
| 6045 | |
| 6046 | pool_gc(NULL); |
| 6047 | goto retry; |
| 6048 | } |
| 6049 | |
| 6050 | BIO_set_data(*bio, ctx); |
| 6051 | SSL_set_bio(*ssl, *bio, *bio); |
| 6052 | |
| 6053 | /* set connection pointer. */ |
| 6054 | if (!SSL_set_ex_data(*ssl, ssl_app_data_index, conn)) { |
| 6055 | SSL_free(*ssl); |
| 6056 | *ssl = NULL; |
| 6057 | if (!retry--) |
| 6058 | goto err; |
| 6059 | |
| 6060 | pool_gc(NULL); |
| 6061 | goto retry; |
| 6062 | } |
| 6063 | |
| 6064 | return 0; |
| 6065 | |
| 6066 | err: |
| 6067 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6068 | return -1; |
| 6069 | } |
| 6070 | |
Olivier Houchard | bc5ce92 | 2021-03-05 23:47:00 +0100 | [diff] [blame] | 6071 | /* This function is called when all the XPRT have been initialized. We can |
| 6072 | * now attempt to start the SSL handshake. |
| 6073 | */ |
| 6074 | static int ssl_sock_start(struct connection *conn, void *xprt_ctx) |
| 6075 | { |
| 6076 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6077 | |
| 6078 | if (ctx->xprt->start) { |
| 6079 | int ret; |
| 6080 | |
| 6081 | ret = ctx->xprt->start(conn, ctx->xprt_ctx); |
| 6082 | if (ret < 0) |
| 6083 | return ret; |
| 6084 | } |
| 6085 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 6086 | |
| 6087 | return 0; |
| 6088 | } |
| 6089 | |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 6090 | /* |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6091 | * This function is called if SSL * context is not yet allocated. The function |
| 6092 | * is designed to be called before any other data-layer operation and sets the |
| 6093 | * handshake flag on the connection. It is safe to call it multiple times. |
| 6094 | * It returns 0 on success and -1 in error case. |
| 6095 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6096 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6097 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6098 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6099 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6100 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6101 | return 0; |
| 6102 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6103 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 6104 | if (!ctx) { |
| 6105 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6106 | return -1; |
| 6107 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6108 | ctx->wait_event.tasklet = tasklet_new(); |
| 6109 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6110 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 6111 | pool_free(ssl_sock_ctx_pool, ctx); |
| 6112 | return -1; |
| 6113 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6114 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6115 | ctx->wait_event.tasklet->context = ctx; |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6116 | 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] | 6117 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6118 | ctx->sent_early_data = 0; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6119 | ctx->early_buf = BUF_NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6120 | ctx->conn = conn; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6121 | ctx->subs = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 6122 | ctx->xprt_st = 0; |
| 6123 | ctx->xprt_ctx = NULL; |
Remi Tricot-Le Breton | 1fe0fad | 2021-09-29 18:56:52 +0200 | [diff] [blame] | 6124 | ctx->error_code = 0; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 6125 | |
| 6126 | /* Only work with sockets for now, this should be adapted when we'll |
| 6127 | * add QUIC support. |
| 6128 | */ |
| 6129 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6130 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6131 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 6132 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 6133 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6134 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 6135 | if (global.maxsslconn && global.sslconns >= global.maxsslconn) { |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6136 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6137 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6138 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6139 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6140 | /* If it is in client mode initiate SSL session |
| 6141 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6142 | if (objt_server(conn->target)) { |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 6143 | if (ssl_bio_and_sess_init(conn, __objt_server(conn->target)->ssl_ctx.ctx, |
| 6144 | &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6145 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6146 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6147 | SSL_set_connect_state(ctx->ssl); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6148 | 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] | 6149 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6150 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 6151 | 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] | 6152 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6153 | SSL_SESSION_free(sess); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 6154 | ha_free(&__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 6155 | } else if (sess) { |
| 6156 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6157 | } |
| 6158 | } |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6159 | 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] | 6160 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6161 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6162 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6163 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 6164 | _HA_ATOMIC_INC(&global.sslconns); |
| 6165 | _HA_ATOMIC_INC(&global.totalsslconns); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6166 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6167 | return 0; |
| 6168 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 6169 | else if (objt_listener(conn->target)) { |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 6170 | struct bind_conf *bc = __objt_listener(conn->target)->bind_conf; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 6171 | |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 6172 | if (ssl_bio_and_sess_init(conn, bc->initial_ctx, |
| 6173 | &ctx->ssl, &ctx->bio, ha_meth, ctx) == -1) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6174 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 6175 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6176 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Frédéric Lécaille | 5aa9241 | 2020-11-09 15:59:23 +0100 | [diff] [blame] | 6177 | if (bc->ssl_conf.early_data) { |
Frédéric Lécaille | 3139c1b | 2020-01-24 14:56:18 +0100 | [diff] [blame] | 6178 | b_alloc(&ctx->early_buf); |
| 6179 | SSL_set_max_early_data(ctx->ssl, |
| 6180 | /* Only allow early data if we managed to allocate |
| 6181 | * a buffer. |
| 6182 | */ |
| 6183 | (!b_is_null(&ctx->early_buf)) ? |
| 6184 | global.tune.bufsize - global.tune.maxrewrite : 0); |
| 6185 | } |
| 6186 | #endif |
| 6187 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6188 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6189 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6190 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 6191 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6192 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Willy Tarreau | a84986a | 2021-02-03 11:21:38 +0100 | [diff] [blame] | 6193 | if (bc->ssl_conf.early_data) |
| 6194 | conn->flags |= CO_FL_EARLY_SSL_HS; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6195 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 6196 | |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 6197 | _HA_ATOMIC_INC(&global.sslconns); |
| 6198 | _HA_ATOMIC_INC(&global.totalsslconns); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6199 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6200 | return 0; |
| 6201 | } |
| 6202 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6203 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6204 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6205 | if (ctx && ctx->wait_event.tasklet) |
| 6206 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6207 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6208 | return -1; |
| 6209 | } |
| 6210 | |
| 6211 | |
| 6212 | /* This is the callback which is used when an SSL handshake is pending. It |
| 6213 | * updates the FD status if it wants some polling before being called again. |
| 6214 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 6215 | * otherwise it returns non-zero and removes itself from the connection's |
| 6216 | * flags (the bit is provided in <flag> by the caller). |
| 6217 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 6218 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6219 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6220 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6221 | int ret; |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6222 | struct ssl_counters *counters = NULL; |
| 6223 | struct ssl_counters *counters_px = NULL; |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 6224 | struct listener *li; |
| 6225 | struct server *srv; |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 6226 | socklen_t lskerr; |
| 6227 | int skerr; |
| 6228 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6229 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 6230 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 6231 | return 0; |
| 6232 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6233 | /* get counters */ |
| 6234 | switch (obj_type(conn->target)) { |
| 6235 | case OBJ_TYPE_LISTENER: |
Amaury Denoyelle | 2bf5d41 | 2021-07-26 09:59:06 +0200 | [diff] [blame] | 6236 | li = __objt_listener(conn->target); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6237 | counters = EXTRA_COUNTERS_GET(li->extra_counters, &ssl_stats_module); |
| 6238 | counters_px = EXTRA_COUNTERS_GET(li->bind_conf->frontend->extra_counters_fe, |
| 6239 | &ssl_stats_module); |
| 6240 | break; |
| 6241 | |
| 6242 | case OBJ_TYPE_SERVER: |
Amaury Denoyelle | 2bf5d41 | 2021-07-26 09:59:06 +0200 | [diff] [blame] | 6243 | srv = __objt_server(conn->target); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6244 | counters = EXTRA_COUNTERS_GET(srv->extra_counters, &ssl_stats_module); |
| 6245 | counters_px = EXTRA_COUNTERS_GET(srv->proxy->extra_counters_be, |
| 6246 | &ssl_stats_module); |
| 6247 | break; |
| 6248 | |
| 6249 | default: |
| 6250 | break; |
| 6251 | } |
| 6252 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6253 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6254 | goto out_error; |
| 6255 | |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 6256 | /* don't start calculating a handshake on a dead connection */ |
| 6257 | if (conn->flags & (CO_FL_ERROR | CO_FL_SOCK_RD_SH | CO_FL_SOCK_WR_SH)) |
| 6258 | goto out_error; |
| 6259 | |
| 6260 | /* FIXME/WT: for now we don't have a clear way to inspect the connection |
| 6261 | * status from the lower layers, so let's check the FD directly. Ideally |
| 6262 | * the xprt layers should provide some status indicating their knowledge |
| 6263 | * of shutdowns or error. |
| 6264 | */ |
Willy Tarreau | 07ecfc5 | 2022-04-11 18:07:03 +0200 | [diff] [blame] | 6265 | BUG_ON(conn->flags & CO_FL_FDLESS); |
| 6266 | |
Willy Tarreau | 0630038 | 2021-02-02 15:42:25 +0100 | [diff] [blame] | 6267 | skerr = 0; |
| 6268 | lskerr = sizeof(skerr); |
| 6269 | if ((getsockopt(conn->handle.fd, SOL_SOCKET, SO_ERROR, &skerr, &lskerr) < 0) || |
| 6270 | skerr != 0) |
| 6271 | goto out_error; |
| 6272 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6273 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6274 | /* |
| 6275 | * Check if we have early data. If we do, we have to read them |
| 6276 | * before SSL_do_handshake() is called, And there's no way to |
| 6277 | * detect early data, except to try to read them |
| 6278 | */ |
| 6279 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6280 | size_t read_data = 0; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6281 | |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6282 | while (1) { |
| 6283 | ret = SSL_read_early_data(ctx->ssl, |
| 6284 | b_tail(&ctx->early_buf), b_room(&ctx->early_buf), |
| 6285 | &read_data); |
| 6286 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 6287 | goto check_error; |
| 6288 | if (read_data > 0) { |
| 6289 | conn->flags |= CO_FL_EARLY_DATA; |
| 6290 | b_add(&ctx->early_buf, read_data); |
| 6291 | } |
| 6292 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 6293 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 6294 | if (!b_data(&ctx->early_buf)) |
| 6295 | b_free(&ctx->early_buf); |
| 6296 | break; |
| 6297 | } |
| 6298 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6299 | } |
| 6300 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6301 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 6302 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 6303 | * Usually SSL_write and SSL_read are used and process implicitly |
| 6304 | * the reneg handshake. |
| 6305 | * Here we use SSL_peek as a workaround for reneg. |
| 6306 | */ |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 6307 | 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] | 6308 | char c; |
| 6309 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6310 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6311 | if (ret <= 0) { |
| 6312 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6313 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6314 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6315 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6316 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6317 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6318 | 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] | 6319 | return 0; |
| 6320 | } |
| 6321 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6322 | /* handshake may have been completed but we have |
| 6323 | * no more data to read. |
| 6324 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6325 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6326 | ret = 1; |
| 6327 | goto reneg_ok; |
| 6328 | } |
| 6329 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6330 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6331 | 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] | 6332 | return 0; |
| 6333 | } |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6334 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6335 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6336 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6337 | return 0; |
| 6338 | } |
| 6339 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6340 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6341 | /* if errno is null, then connection was successfully established */ |
| 6342 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6343 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6344 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6345 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6346 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6347 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6348 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6349 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6350 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6351 | /* 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] | 6352 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6353 | empty_handshake = state == TLS_ST_BEFORE; |
| 6354 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6355 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6356 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6357 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6358 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6359 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6360 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6361 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6362 | else |
| 6363 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6364 | } |
| 6365 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6366 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6367 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6368 | else |
| 6369 | conn->err_code = CO_ER_SSL_ABORT; |
| 6370 | } |
| 6371 | } |
| 6372 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6373 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6374 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6375 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6376 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6377 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6378 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6379 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6380 | goto out_error; |
| 6381 | } |
| 6382 | else { |
| 6383 | /* Fail on all other handshake errors */ |
| 6384 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6385 | * buffer, causing an RST to be emitted upon close() on |
| 6386 | * TCP sockets. We first try to drain possibly pending |
| 6387 | * data to avoid this as much as possible. |
| 6388 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 6389 | conn_ctrl_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6390 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6391 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6392 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6393 | goto out_error; |
| 6394 | } |
| 6395 | } |
| 6396 | /* read some data: consider handshake completed */ |
| 6397 | goto reneg_ok; |
| 6398 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6399 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6400 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6401 | if (ret != 1) { |
| 6402 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6403 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6404 | |
Remi Tricot-Le Breton | 1fe0fad | 2021-09-29 18:56:52 +0200 | [diff] [blame] | 6405 | if (!ctx->error_code) |
| 6406 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 7c6898e | 2021-07-29 09:45:51 +0200 | [diff] [blame] | 6407 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6408 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6409 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6410 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6411 | 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] | 6412 | return 0; |
| 6413 | } |
| 6414 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6415 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6416 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6417 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6418 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6419 | return 0; |
| 6420 | } |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6421 | #ifdef SSL_MODE_ASYNC |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6422 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6423 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6424 | return 0; |
| 6425 | } |
| 6426 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6427 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6428 | /* if errno is null, then connection was successfully established */ |
| 6429 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6430 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6431 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6432 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6433 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6434 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6435 | #else |
| 6436 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6437 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6438 | /* 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] | 6439 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6440 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6441 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6442 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6443 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6444 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6445 | if (empty_handshake) { |
| 6446 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6447 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6448 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6449 | else |
| 6450 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6451 | } |
| 6452 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6453 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6454 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6455 | else |
| 6456 | conn->err_code = CO_ER_SSL_ABORT; |
| 6457 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6458 | } |
| 6459 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6460 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6461 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6462 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6463 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6464 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6465 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6466 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6467 | goto out_error; |
| 6468 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6469 | else { |
| 6470 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 6471 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6472 | * buffer, causing an RST to be emitted upon close() on |
| 6473 | * TCP sockets. We first try to drain possibly pending |
| 6474 | * data to avoid this as much as possible. |
| 6475 | */ |
Willy Tarreau | 2ded48d | 2020-12-11 16:20:34 +0100 | [diff] [blame] | 6476 | conn_ctrl_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6477 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6478 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6479 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6480 | goto out_error; |
| 6481 | } |
| 6482 | } |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6483 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6484 | else { |
| 6485 | /* |
| 6486 | * If the server refused the early data, we have to send a |
| 6487 | * 425 to the client, as we no longer have the data to sent |
| 6488 | * them again. |
| 6489 | */ |
| 6490 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6491 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6492 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 6493 | goto out_error; |
| 6494 | } |
| 6495 | } |
| 6496 | } |
| 6497 | #endif |
| 6498 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6499 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6500 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6501 | |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6502 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6503 | /* ASYNC engine API doesn't support moving read/write |
| 6504 | * buffers. So we disable ASYNC mode right after |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 6505 | * the handshake to avoid buffer overflow. |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6506 | */ |
| 6507 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6508 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6509 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6510 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6511 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6512 | if (objt_server(conn->target)) { |
| 6513 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 6514 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 6515 | 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] | 6516 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6517 | else { |
| 6518 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 6519 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 6520 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 6521 | } |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 6522 | |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6523 | if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6524 | HA_ATOMIC_INC(&counters->sess); |
| 6525 | HA_ATOMIC_INC(&counters_px->sess); |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6526 | } |
Amaury Denoyelle | d0447a7 | 2020-11-03 17:10:02 +0100 | [diff] [blame] | 6527 | } |
Willy Tarreau | 4299528 | 2020-11-06 13:19:18 +0100 | [diff] [blame] | 6528 | else if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6529 | HA_ATOMIC_INC(&counters->reused_sess); |
| 6530 | HA_ATOMIC_INC(&counters_px->reused_sess); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | /* The connection is now established at both layers, it's time to leave */ |
| 6534 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6535 | return 1; |
| 6536 | |
| 6537 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6538 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6539 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6540 | ERR_clear_error(); |
| 6541 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6542 | /* free resumed session if exists */ |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6543 | if (objt_server(conn->target)) { |
| 6544 | struct server *s = __objt_server(conn->target); |
| 6545 | /* RWLOCK: only rdlock the SSL cache even when writing in it because there is |
| 6546 | * one cache per thread, it only prevents to flush it from the CLI in |
| 6547 | * another thread */ |
| 6548 | |
| 6549 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 6550 | if (s->ssl_ctx.reused_sess[tid].ptr) |
| 6551 | ha_free(&s->ssl_ctx.reused_sess[tid].ptr); |
William Lallemand | 3ce6eed | 2021-02-08 10:43:44 +0100 | [diff] [blame] | 6552 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6553 | } |
| 6554 | |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6555 | if (counters) { |
Willy Tarreau | c5e7cf9 | 2021-11-22 17:46:13 +0100 | [diff] [blame] | 6556 | HA_ATOMIC_INC(&counters->failed_handshake); |
| 6557 | HA_ATOMIC_INC(&counters_px->failed_handshake); |
Amaury Denoyelle | 034c162 | 2020-11-13 16:05:00 +0100 | [diff] [blame] | 6558 | } |
| 6559 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6560 | /* Fail on all other handshake errors */ |
| 6561 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6562 | if (!conn->err_code) |
| 6563 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6564 | return 0; |
| 6565 | } |
| 6566 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6567 | /* Called from the upper layer, to subscribe <es> to events <event_type>. The |
| 6568 | * event subscriber <es> is not allowed to change from a previous call as long |
| 6569 | * as at least one event is still subscribed. The <event_type> must only be a |
| 6570 | * combination of SUB_RETRY_RECV and SUB_RETRY_SEND. It always returns 0, |
| 6571 | * unless the transport layer was already released. |
| 6572 | */ |
| 6573 | 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] | 6574 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6575 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6576 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6577 | if (!ctx) |
| 6578 | return -1; |
| 6579 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6580 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6581 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6582 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6583 | ctx->subs = es; |
| 6584 | es->events |= event_type; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6585 | |
| 6586 | /* we may have to subscribe to lower layers for new events */ |
| 6587 | event_type &= ~ctx->wait_event.events; |
| 6588 | if (event_type && !(conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6589 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6590 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6591 | } |
| 6592 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6593 | /* Called from the upper layer, to unsubscribe <es> from events <event_type>. |
| 6594 | * The <es> pointer is not allowed to differ from the one passed to the |
| 6595 | * subscribe() call. It always returns zero. |
| 6596 | */ |
| 6597 | 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] | 6598 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6599 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6600 | |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6601 | BUG_ON(event_type & ~(SUB_RETRY_SEND|SUB_RETRY_RECV)); |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6602 | BUG_ON(ctx->subs && ctx->subs != es); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6603 | |
Willy Tarreau | ee1a6fc | 2020-01-17 07:52:13 +0100 | [diff] [blame] | 6604 | es->events &= ~event_type; |
| 6605 | if (!es->events) |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6606 | ctx->subs = NULL; |
| 6607 | |
| 6608 | /* If we subscribed, and we're not doing the handshake, |
| 6609 | * then we subscribed because the upper layer asked for it, |
| 6610 | * as the upper layer is no longer interested, we can |
| 6611 | * unsubscribe too. |
| 6612 | */ |
| 6613 | event_type &= ctx->wait_event.events; |
| 6614 | if (event_type && !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) |
| 6615 | conn_unsubscribe(conn, ctx->xprt_ctx, event_type, &ctx->wait_event); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6616 | |
| 6617 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6618 | } |
| 6619 | |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6620 | /* The connection has been taken over, so destroy the old tasklet and create |
| 6621 | * a new one. The original thread ID must be passed into orig_tid |
| 6622 | * It should be called with the takeover lock for the old thread held. |
| 6623 | * Returns 0 on success, and -1 on failure |
| 6624 | */ |
| 6625 | static int ssl_takeover(struct connection *conn, void *xprt_ctx, int orig_tid) |
| 6626 | { |
| 6627 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6628 | struct tasklet *tl = tasklet_new(); |
| 6629 | |
| 6630 | if (!tl) |
| 6631 | return -1; |
| 6632 | |
| 6633 | ctx->wait_event.tasklet->context = NULL; |
| 6634 | tasklet_wakeup_on(ctx->wait_event.tasklet, orig_tid); |
| 6635 | ctx->wait_event.tasklet = tl; |
| 6636 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 6637 | ctx->wait_event.tasklet->context = ctx; |
| 6638 | return 0; |
| 6639 | } |
| 6640 | |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6641 | /* notify the next xprt that the connection is about to become idle and that it |
| 6642 | * may be stolen at any time after the function returns and that any tasklet in |
| 6643 | * the chain must be careful before dereferencing its context. |
| 6644 | */ |
| 6645 | static void ssl_set_idle(struct connection *conn, void *xprt_ctx) |
| 6646 | { |
| 6647 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6648 | |
| 6649 | if (!ctx || !ctx->wait_event.tasklet) |
| 6650 | return; |
| 6651 | |
| 6652 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 6653 | if (ctx->xprt) |
| 6654 | xprt_set_idle(conn, ctx->xprt, ctx->xprt_ctx); |
| 6655 | } |
| 6656 | |
| 6657 | /* notify the next xprt that the connection is not idle anymore and that it may |
| 6658 | * not be stolen before the next xprt_set_idle(). |
| 6659 | */ |
| 6660 | static void ssl_set_used(struct connection *conn, void *xprt_ctx) |
| 6661 | { |
| 6662 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6663 | |
| 6664 | if (!ctx || !ctx->wait_event.tasklet) |
| 6665 | return; |
| 6666 | |
| 6667 | HA_ATOMIC_OR(&ctx->wait_event.tasklet->state, TASK_F_USR1); |
| 6668 | if (ctx->xprt) |
| 6669 | xprt_set_used(conn, ctx->xprt, ctx->xprt_ctx); |
| 6670 | } |
| 6671 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6672 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6673 | * Returns 0 on success, and non-zero on failure. |
| 6674 | */ |
| 6675 | 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) |
| 6676 | { |
| 6677 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6678 | |
| 6679 | if (oldxprt_ops != NULL) |
| 6680 | *oldxprt_ops = ctx->xprt; |
| 6681 | if (oldxprt_ctx != NULL) |
| 6682 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6683 | ctx->xprt = toadd_ops; |
| 6684 | ctx->xprt_ctx = toadd_ctx; |
| 6685 | return 0; |
| 6686 | } |
| 6687 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6688 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6689 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6690 | * XPRT. |
| 6691 | */ |
| 6692 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6693 | { |
| 6694 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6695 | |
| 6696 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6697 | ctx->xprt_ctx = newctx; |
| 6698 | ctx->xprt = newops; |
| 6699 | return 0; |
| 6700 | } |
| 6701 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6702 | } |
| 6703 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 6704 | 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] | 6705 | { |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6706 | struct tasklet *tl = (struct tasklet *)t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6707 | struct ssl_sock_ctx *ctx = context; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6708 | struct connection *conn; |
| 6709 | int conn_in_list; |
| 6710 | int ret = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6711 | |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6712 | if (state & TASK_F_USR1) { |
| 6713 | /* the tasklet was idling on an idle connection, it might have |
| 6714 | * been stolen, let's be careful! |
| 6715 | */ |
| 6716 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6717 | if (tl->context == NULL) { |
| 6718 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
| 6719 | tasklet_free(tl); |
| 6720 | return NULL; |
| 6721 | } |
| 6722 | conn = ctx->conn; |
| 6723 | conn_in_list = conn->flags & CO_FL_LIST_MASK; |
| 6724 | if (conn_in_list) |
| 6725 | conn_delete_from_tree(&conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6726 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6727 | } else { |
| 6728 | conn = ctx->conn; |
| 6729 | conn_in_list = 0; |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6730 | } |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 6731 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6732 | /* First if we're doing an handshake, try that */ |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6733 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6734 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6735 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6736 | /* handshake completed, leave the bulk queue */ |
Willy Tarreau | 4c48edb | 2021-03-09 17:58:02 +0100 | [diff] [blame] | 6737 | _HA_ATOMIC_AND(&tl->state, ~TASK_HEAVY); |
Willy Tarreau | 9205ab3 | 2021-02-25 15:31:00 +0100 | [diff] [blame] | 6738 | } |
| 6739 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6740 | /* If we had an error, or the handshake is done and I/O is available, |
| 6741 | * let the upper layer know. |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6742 | * If no mux was set up yet, then call conn_create_mux() |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6743 | * we can't be sure conn_fd_handler() will be called again. |
| 6744 | */ |
| 6745 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6746 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6747 | int woke = 0; |
| 6748 | |
| 6749 | /* On error, wake any waiter */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6750 | if (ctx->subs) { |
| 6751 | tasklet_wakeup(ctx->subs->tasklet); |
| 6752 | ctx->subs->events = 0; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6753 | woke = 1; |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6754 | ctx->subs = NULL; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6755 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6756 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6757 | /* If we're the first xprt for the connection, let the |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6758 | * upper layers know. If we have no mux, create it, |
| 6759 | * and once we have a mux, call its wake method if we didn't |
| 6760 | * woke a tasklet already. |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6761 | */ |
| 6762 | if (ctx->conn->xprt_ctx == ctx) { |
Olivier Houchard | 477902b | 2020-01-22 18:08:48 +0100 | [diff] [blame] | 6763 | if (!ctx->conn->mux) |
| 6764 | ret = conn_create_mux(ctx->conn); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6765 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6766 | ret = ctx->conn->mux->wake(ctx->conn); |
| 6767 | goto leave; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6768 | } |
| 6769 | } |
Ilya Shipitsin | 761d64c | 2021-01-07 11:59:58 +0500 | [diff] [blame] | 6770 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6771 | /* If we have early data and somebody wants to receive, let them */ |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 6772 | else if (b_data(&ctx->early_buf) && ctx->subs && |
| 6773 | ctx->subs->events & SUB_RETRY_RECV) { |
| 6774 | tasklet_wakeup(ctx->subs->tasklet); |
| 6775 | ctx->subs->events &= ~SUB_RETRY_RECV; |
| 6776 | if (!ctx->subs->events) |
| 6777 | ctx->subs = NULL; |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6778 | } |
| 6779 | #endif |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6780 | leave: |
| 6781 | if (!ret && conn_in_list) { |
| 6782 | struct server *srv = objt_server(conn->target); |
| 6783 | |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6784 | HA_SPIN_LOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6785 | if (conn_in_list == CO_FL_SAFE_LIST) |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6786 | eb64_insert(&srv->per_thr[tid].safe_conns, &conn->hash_node->node); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6787 | else |
Willy Tarreau | 8522348 | 2022-09-29 20:32:43 +0200 | [diff] [blame] | 6788 | eb64_insert(&srv->per_thr[tid].idle_conns, &conn->hash_node->node); |
Amaury Denoyelle | 5c7086f | 2021-01-11 09:21:52 +0100 | [diff] [blame] | 6789 | HA_SPIN_UNLOCK(IDLE_CONNS_LOCK, &idle_conns[tid].idle_conns_lock); |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 6790 | } |
Willy Tarreau | 7416314 | 2021-03-13 11:30:19 +0100 | [diff] [blame] | 6791 | return t; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6792 | } |
| 6793 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6794 | /* 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] | 6795 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6796 | * buffer wraps, in which case a second call may be performed. The connection's |
| 6797 | * flags are updated with whatever special event is detected (error, read0, |
| 6798 | * empty). The caller is responsible for taking care of those events and |
| 6799 | * avoiding the call if inappropriate. The function does not call the |
| 6800 | * connection's polling update function, so the caller is responsible for this. |
| 6801 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6802 | 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] | 6803 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6804 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 6805 | ssize_t ret; |
| 6806 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6807 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6808 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6809 | goto out_error; |
| 6810 | |
Ilya Shipitsin | 761d64c | 2021-01-07 11:59:58 +0500 | [diff] [blame] | 6811 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 6812 | if (b_data(&ctx->early_buf)) { |
| 6813 | try = b_contig_space(buf); |
| 6814 | if (try > b_data(&ctx->early_buf)) |
| 6815 | try = b_data(&ctx->early_buf); |
| 6816 | memcpy(b_tail(buf), b_head(&ctx->early_buf), try); |
| 6817 | b_add(buf, try); |
| 6818 | b_del(&ctx->early_buf, try); |
| 6819 | if (b_data(&ctx->early_buf) == 0) |
| 6820 | b_free(&ctx->early_buf); |
| 6821 | return try; |
| 6822 | } |
| 6823 | #endif |
| 6824 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6825 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6826 | /* a handshake was requested */ |
| 6827 | return 0; |
| 6828 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6829 | /* read the largest possible block. For this, we perform only one call |
| 6830 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 6831 | * in which case we accept to do it once again. A new attempt is made on |
| 6832 | * EINTR too. |
| 6833 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 6834 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6835 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6836 | try = b_contig_space(buf); |
| 6837 | if (!try) |
| 6838 | break; |
| 6839 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6840 | if (try > count) |
| 6841 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6842 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6843 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6844 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6845 | if (conn->flags & CO_FL_ERROR) { |
| 6846 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6847 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6848 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6849 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6850 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6851 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6852 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6853 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6854 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6855 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6856 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6857 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6858 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6859 | 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] | 6860 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6861 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6862 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6863 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6864 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6865 | break; |
| 6866 | } |
| 6867 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6868 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6869 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6870 | SUB_RETRY_RECV, |
| 6871 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6872 | /* handshake is running, and it may need to re-enable read */ |
| 6873 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 6874 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6875 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6876 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6877 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6878 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6879 | break; |
| 6880 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6881 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6882 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 6883 | goto read0; |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6884 | else if (ret == SSL_ERROR_SSL) { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 6885 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
| 6886 | if (ctx && !ctx->error_code) |
Remi Tricot-Le Breton | 9543d5a | 2021-09-29 18:56:53 +0200 | [diff] [blame] | 6887 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 6888 | conn->err_code = CO_ERR_SSL_FATAL; |
| 6889 | } |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6890 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 6891 | * stack before shutting down the connection for |
| 6892 | * reading. */ |
Willy Tarreau | acef5e2 | 2022-04-25 20:32:15 +0200 | [diff] [blame] | 6893 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN || errno == EWOULDBLOCK)) |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6894 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6895 | /* otherwise it's a real error */ |
| 6896 | goto out_error; |
| 6897 | } |
| 6898 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6899 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6900 | return done; |
| 6901 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6902 | clear_ssl_error: |
| 6903 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6904 | ssl_sock_dump_errors(conn, NULL); |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6905 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6906 | read0: |
| 6907 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6908 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6909 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6910 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6911 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6912 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 6913 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6914 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6915 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6916 | } |
| 6917 | |
| 6918 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6919 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 6920 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 6921 | * 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] | 6922 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 6923 | * a second call may be performed. The connection's flags are updated with |
| 6924 | * whatever special event is detected (error, empty). The caller is responsible |
| 6925 | * for taking care of those events and avoiding the call if inappropriate. The |
| 6926 | * 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] | 6927 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 6928 | * caller to take care of this. It's up to the caller to update the buffer's |
| 6929 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6930 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6931 | 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] | 6932 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6933 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6934 | ssize_t ret; |
| 6935 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6936 | |
| 6937 | done = 0; |
| 6938 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6939 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6940 | goto out_error; |
| 6941 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 6942 | 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] | 6943 | /* a handshake was requested */ |
| 6944 | return 0; |
| 6945 | |
| 6946 | /* send the largest possible block. For this we perform only one call |
| 6947 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 6948 | * in which case we accept to do it once again. |
| 6949 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6950 | while (count) { |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6951 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6952 | size_t written_data; |
| 6953 | #endif |
| 6954 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6955 | try = b_contig_data(buf, done); |
| 6956 | if (try > count) |
| 6957 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6958 | |
Thomas Prückl | 1024393 | 2022-04-27 13:04:54 +0200 | [diff] [blame] | 6959 | if (global_ssl.hard_max_record && try > global_ssl.hard_max_record) |
| 6960 | try = global_ssl.hard_max_record; |
| 6961 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 6962 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6963 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6964 | global_ssl.max_record && try > global_ssl.max_record) { |
| 6965 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6966 | } |
| 6967 | else { |
| 6968 | /* we need to keep the information about the fact that |
| 6969 | * we're not limiting the upcoming send(), because if it |
| 6970 | * fails, we'll have to retry with at least as many data. |
| 6971 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6972 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6973 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6974 | |
Ilya Shipitsin | b9b84a4 | 2020-10-24 23:42:30 +0500 | [diff] [blame] | 6975 | #ifdef SSL_READ_EARLY_DATA_SUCCESS |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6976 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6977 | unsigned int max_early; |
| 6978 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6979 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6980 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6981 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6982 | if (SSL_get0_session(ctx->ssl)) |
| 6983 | 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] | 6984 | else |
| 6985 | max_early = 0; |
| 6986 | } |
| 6987 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6988 | if (try + ctx->sent_early_data > max_early) { |
| 6989 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6990 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6991 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6992 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6993 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6994 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6995 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6996 | 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] | 6997 | if (ret == 1) { |
| 6998 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6999 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 7000 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7001 | 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] | 7002 | /* Initiate the handshake, now */ |
| 7003 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 7004 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 7005 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7006 | } |
| 7007 | |
| 7008 | } else |
| 7009 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7010 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 7011 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7012 | if (conn->flags & CO_FL_ERROR) { |
| 7013 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7014 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 7015 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7016 | if (ret > 0) { |
Willy Tarreau | c192b0a | 2020-01-23 09:11:58 +0100 | [diff] [blame] | 7017 | /* A send succeeded, so we can consider ourself connected */ |
| 7018 | conn->flags &= ~CO_FL_WAIT_L4L6; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7019 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 7020 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7021 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7022 | } |
| 7023 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7024 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7025 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7026 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7027 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7028 | /* handshake is running, and it may need to re-enable write */ |
| 7029 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7030 | 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] | 7031 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7032 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7033 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7034 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7035 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 7036 | break; |
| 7037 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7038 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7039 | break; |
| 7040 | } |
| 7041 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 7042 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7043 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7044 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 7045 | SUB_RETRY_RECV, |
| 7046 | &ctx->wait_event); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 7047 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7048 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 7049 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7050 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 7051 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7052 | break; |
| 7053 | } |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 7054 | else if (ret == SSL_ERROR_SSL || ret == SSL_ERROR_SYSCALL) { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7055 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
| 7056 | |
Willy Tarreau | 3a0a0d6 | 2022-04-12 07:31:06 +0200 | [diff] [blame] | 7057 | if (ctx && !ctx->error_code) |
Remi Tricot-Le Breton | 9543d5a | 2021-09-29 18:56:53 +0200 | [diff] [blame] | 7058 | ctx->error_code = ERR_peek_error(); |
Remi Tricot-Le Breton | 61944f7 | 2021-09-29 18:56:51 +0200 | [diff] [blame] | 7059 | conn->err_code = CO_ERR_SSL_FATAL; |
| 7060 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7061 | goto out_error; |
| 7062 | } |
| 7063 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7064 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7065 | return done; |
| 7066 | |
| 7067 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7068 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 7069 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7070 | ERR_clear_error(); |
| 7071 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7072 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 7073 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7074 | } |
| 7075 | |
Willy Tarreau | 832e242 | 2021-05-13 10:11:03 +0200 | [diff] [blame] | 7076 | void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7077 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7078 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7079 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7080 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7081 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7082 | if (ctx->wait_event.events != 0) |
| 7083 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 7084 | ctx->wait_event.events, |
| 7085 | &ctx->wait_event); |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7086 | if (ctx->subs) { |
| 7087 | ctx->subs->events = 0; |
| 7088 | tasklet_wakeup(ctx->subs->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 7089 | } |
Willy Tarreau | 113d52b | 2020-01-10 09:20:26 +0100 | [diff] [blame] | 7090 | |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 7091 | if (ctx->xprt->close) |
| 7092 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 7093 | #ifdef SSL_MODE_ASYNC |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7094 | if (global_ssl.async) { |
| 7095 | OSSL_ASYNC_FD all_fd[32], afd; |
| 7096 | size_t num_all_fds = 0; |
| 7097 | int i; |
| 7098 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7099 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7100 | if (num_all_fds > 32) { |
| 7101 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 7102 | return; |
| 7103 | } |
| 7104 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7105 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7106 | |
| 7107 | /* If an async job is pending, we must try to |
| 7108 | to catch the end using polling before calling |
| 7109 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7110 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7111 | for (i=0 ; i < num_all_fds ; i++) { |
| 7112 | /* switch on an handler designed to |
| 7113 | * handle the SSL_free |
| 7114 | */ |
| 7115 | afd = all_fd[i]; |
| 7116 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7117 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7118 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 7119 | /* To ensure that the fd cache won't be used |
| 7120 | * and we'll catch a real RD event. |
| 7121 | */ |
| 7122 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7123 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7124 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7125 | pool_free(ssl_sock_ctx_pool, ctx); |
Willy Tarreau | 4781b15 | 2021-04-06 13:53:36 +0200 | [diff] [blame] | 7126 | _HA_ATOMIC_INC(&jobs); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7127 | return; |
| 7128 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7129 | /* Else we can remove the fds from the fdtab |
| 7130 | * and call SSL_free. |
Willy Tarreau | 6767245 | 2020-08-26 11:44:17 +0200 | [diff] [blame] | 7131 | * note: we do a fd_stop_both and not a delete |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7132 | * because the fd is owned by the engine. |
| 7133 | * the engine is responsible to close |
| 7134 | */ |
Emeric Brun | 7d392a5 | 2022-07-01 17:36:50 +0200 | [diff] [blame] | 7135 | for (i=0 ; i < num_all_fds ; i++) { |
| 7136 | /* We want to remove the fd from the fdtab |
| 7137 | * but we flag it to disown because the |
| 7138 | * close is performed by the engine itself |
| 7139 | */ |
| 7140 | fdtab[all_fd[i]].state |= FD_DISOWN; |
| 7141 | fd_delete(all_fd[i]); |
| 7142 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7143 | } |
| 7144 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7145 | SSL_free(ctx->ssl); |
Olivier Houchard | 54907bb | 2019-12-19 15:02:39 +0100 | [diff] [blame] | 7146 | b_free(&ctx->early_buf); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 7147 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7148 | pool_free(ssl_sock_ctx_pool, ctx); |
Willy Tarreau | 82531f6 | 2021-10-06 12:15:18 +0200 | [diff] [blame] | 7149 | _HA_ATOMIC_DEC(&global.sslconns); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7150 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7151 | } |
| 7152 | |
| 7153 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 7154 | * any case, flags the connection as reusable if no handshake was in progress. |
| 7155 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7156 | 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] | 7157 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7158 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7159 | |
Willy Tarreau | 911db9b | 2020-01-23 16:27:54 +0100 | [diff] [blame] | 7160 | if (conn->flags & (CO_FL_WAIT_XPRT | CO_FL_SSL_WAIT_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7161 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 7162 | if (!clean) |
| 7163 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7164 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7165 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7166 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7167 | /* Clear openssl global errors stack */ |
Frédéric Lécaille | 2be0ac5 | 2022-09-06 19:37:08 +0200 | [diff] [blame] | 7168 | ssl_sock_dump_errors(conn, NULL); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 7169 | ERR_clear_error(); |
| 7170 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 7171 | } |
| 7172 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 7173 | |
Ilya Shipitsin | 77e3b4a | 2020-03-10 12:06:11 +0500 | [diff] [blame] | 7174 | /* used for ppv2 pkey algo (can be used for logging) */ |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7175 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
| 7176 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7177 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7178 | X509 *crt; |
| 7179 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7180 | if (!ctx) |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7181 | return 0; |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7182 | crt = SSL_get_certificate(ctx->ssl); |
| 7183 | if (!crt) |
| 7184 | return 0; |
| 7185 | |
| 7186 | return cert_get_pkey_algo(crt, out); |
| 7187 | } |
| 7188 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7189 | /* used for ppv2 cert signature (can be used for logging) */ |
| 7190 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 7191 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7192 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7193 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7194 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 7195 | X509 *crt; |
| 7196 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7197 | if (!ctx) |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7198 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7199 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 7200 | if (!crt) |
| 7201 | return NULL; |
| 7202 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7203 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 7204 | } |
| 7205 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7206 | /* used for ppv2 authority */ |
| 7207 | const char *ssl_sock_get_sni(struct connection *conn) |
| 7208 | { |
| 7209 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7210 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7211 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7212 | if (!ctx) |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7213 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7214 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7215 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7216 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 7217 | #endif |
| 7218 | } |
| 7219 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7220 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7221 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 7222 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7223 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7224 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7225 | if (!ctx) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7226 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7227 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7228 | } |
| 7229 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 7230 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7231 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 7232 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7233 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7234 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7235 | if (!ctx) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7236 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7237 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 7238 | } |
| 7239 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7240 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 7241 | { |
| 7242 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7243 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7244 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7245 | if (!ctx) |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 7246 | return; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7247 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7248 | #endif |
| 7249 | } |
| 7250 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7251 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 7252 | * to disable SNI. |
| 7253 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7254 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 7255 | { |
| 7256 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7257 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 7258 | struct server *s; |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7259 | char *prev_name; |
| 7260 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7261 | if (!ctx) |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7262 | return; |
Willy Tarreau | 77bfa66 | 2021-12-23 11:12:13 +0100 | [diff] [blame] | 7263 | |
| 7264 | BUG_ON(!(conn->flags & CO_FL_WAIT_L6_CONN)); |
| 7265 | BUG_ON(!(conn->flags & CO_FL_SSL_WAIT_HS)); |
| 7266 | |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 7267 | s = __objt_server(conn->target); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7268 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7269 | /* 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] | 7270 | * new connection will present a new SNI. compare with the SNI |
| 7271 | * previously stored in the reused_sess */ |
| 7272 | /* the RWLOCK is used to ensure that we are not trying to flush the |
| 7273 | * cache from the CLI */ |
| 7274 | |
| 7275 | HA_RWLOCK_RDLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
| 7276 | prev_name = s->ssl_ctx.reused_sess[tid].sni; |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7277 | if ((!prev_name && hostname) || |
| 7278 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7279 | SSL_set_session(ctx->ssl, NULL); |
William Lallemand | e18d4e8 | 2021-11-17 02:59:21 +0100 | [diff] [blame] | 7280 | HA_RWLOCK_RDUNLOCK(SSL_SERVER_LOCK, &s->ssl_ctx.lock); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7281 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7282 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7283 | #endif |
| 7284 | } |
| 7285 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7286 | /* Extract peer certificate's common name into the chunk dest |
| 7287 | * Returns |
| 7288 | * the len of the extracted common name |
| 7289 | * or 0 if no CN found in DN |
| 7290 | * or -1 on error case (i.e. no peer certificate) |
| 7291 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7292 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 7293 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7294 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7295 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7296 | X509 *crt = NULL; |
| 7297 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7298 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7299 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7300 | .area = (char *)&find_cn, |
| 7301 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7302 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7303 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7304 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7305 | if (!ctx) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7306 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7307 | |
| 7308 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7309 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7310 | if (!crt) |
| 7311 | goto out; |
| 7312 | |
| 7313 | name = X509_get_subject_name(crt); |
| 7314 | if (!name) |
| 7315 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7316 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7317 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 7318 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7319 | if (crt) |
| 7320 | X509_free(crt); |
| 7321 | |
| 7322 | return result; |
| 7323 | } |
| 7324 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7325 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 7326 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 7327 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7328 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7329 | X509 *crt = NULL; |
| 7330 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7331 | if (!ctx) |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7332 | return 0; |
| 7333 | |
| 7334 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7335 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7336 | if (!crt) |
| 7337 | return 0; |
| 7338 | |
| 7339 | X509_free(crt); |
| 7340 | return 1; |
| 7341 | } |
| 7342 | |
| 7343 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 7344 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7345 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7346 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7347 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7348 | if (!ctx) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7349 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7350 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7351 | } |
| 7352 | |
| 7353 | /* returns result from SSL verify */ |
| 7354 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 7355 | { |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7356 | struct ssl_sock_ctx *ctx = conn_get_ssl_sock_ctx(conn); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7357 | |
Willy Tarreau | 939b0bf | 2022-04-11 11:29:11 +0200 | [diff] [blame] | 7358 | if (!ctx) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7359 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7360 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7361 | } |
| 7362 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7363 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 7364 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 7365 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 7366 | * freed by the caller. NPN is also checked if available since older versions |
| 7367 | * of openssl (1.0.1) which are more common in field only support this one. |
| 7368 | */ |
Amaury Denoyelle | 71e588c | 2021-11-12 11:23:29 +0100 | [diff] [blame] | 7369 | 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] | 7370 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7371 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 7372 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7373 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 7374 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7375 | return 0; |
| 7376 | |
| 7377 | *str = NULL; |
| 7378 | |
| 7379 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7380 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7381 | if (*str) |
| 7382 | return 1; |
| 7383 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7384 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7385 | 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] | 7386 | if (*str) |
| 7387 | return 1; |
| 7388 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7389 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7390 | return 0; |
| 7391 | } |
| 7392 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 7393 | /* "issuers-chain-path" load chain certificate in global */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 7394 | 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] | 7395 | { |
| 7396 | X509 *ca; |
| 7397 | X509_NAME *name = NULL; |
| 7398 | ASN1_OCTET_STRING *skid = NULL; |
| 7399 | STACK_OF(X509) *chain = NULL; |
| 7400 | struct issuer_chain *issuer; |
| 7401 | struct eb64_node *node; |
| 7402 | char *path; |
| 7403 | u64 key; |
| 7404 | int ret = 0; |
| 7405 | |
| 7406 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 7407 | if (chain == NULL) { |
| 7408 | chain = sk_X509_new_null(); |
| 7409 | skid = X509_get_ext_d2i(ca, NID_subject_key_identifier, NULL, NULL); |
| 7410 | name = X509_get_subject_name(ca); |
| 7411 | } |
| 7412 | if (!sk_X509_push(chain, ca)) { |
| 7413 | X509_free(ca); |
| 7414 | goto end; |
| 7415 | } |
| 7416 | } |
| 7417 | if (!chain) { |
| 7418 | memprintf(err, "unable to load issuers-chain %s : pem certificate not found.\n", fp); |
| 7419 | goto end; |
| 7420 | } |
| 7421 | if (!skid) { |
| 7422 | memprintf(err, "unable to load issuers-chain %s : SubjectKeyIdentifier not found.\n", fp); |
| 7423 | goto end; |
| 7424 | } |
| 7425 | if (!name) { |
| 7426 | memprintf(err, "unable to load issuers-chain %s : SubjectName not found.\n", fp); |
| 7427 | goto end; |
| 7428 | } |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 7429 | key = XXH3(ASN1_STRING_get0_data(skid), ASN1_STRING_length(skid), 0); |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 7430 | 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] | 7431 | issuer = container_of(node, typeof(*issuer), node); |
| 7432 | if (!X509_NAME_cmp(name, X509_get_subject_name(sk_X509_value(issuer->chain, 0)))) { |
| 7433 | memprintf(err, "duplicate issuers-chain %s: %s already in store\n", fp, issuer->path); |
| 7434 | goto end; |
| 7435 | } |
| 7436 | } |
| 7437 | issuer = calloc(1, sizeof *issuer); |
| 7438 | path = strdup(fp); |
| 7439 | if (!issuer || !path) { |
| 7440 | free(issuer); |
| 7441 | free(path); |
| 7442 | goto end; |
| 7443 | } |
| 7444 | issuer->node.key = key; |
| 7445 | issuer->path = path; |
| 7446 | issuer->chain = chain; |
| 7447 | chain = NULL; |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 7448 | eb64_insert(&cert_issuer_tree, &issuer->node); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 7449 | ret = 1; |
| 7450 | end: |
| 7451 | if (skid) |
| 7452 | ASN1_OCTET_STRING_free(skid); |
| 7453 | if (chain) |
| 7454 | sk_X509_pop_free(chain, X509_free); |
| 7455 | return ret; |
| 7456 | } |
| 7457 | |
William Lallemand | da8584c | 2020-05-14 10:14:37 +0200 | [diff] [blame] | 7458 | struct issuer_chain* ssl_get0_issuer_chain(X509 *cert) |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 7459 | { |
| 7460 | AUTHORITY_KEYID *akid; |
| 7461 | struct issuer_chain *issuer = NULL; |
| 7462 | |
| 7463 | akid = X509_get_ext_d2i(cert, NID_authority_key_identifier, NULL, NULL); |
William Lallemand | f69cd68 | 2020-11-19 16:24:13 +0100 | [diff] [blame] | 7464 | if (akid && akid->keyid) { |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 7465 | struct eb64_node *node; |
| 7466 | u64 hk; |
Dragan Dosen | 967e7e7 | 2020-12-22 13:22:34 +0100 | [diff] [blame] | 7467 | 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] | 7468 | for (node = eb64_lookup(&cert_issuer_tree, hk); node; node = eb64_next(node)) { |
| 7469 | struct issuer_chain *ti = container_of(node, typeof(*issuer), node); |
| 7470 | if (X509_check_issued(sk_X509_value(ti->chain, 0), cert) == X509_V_OK) { |
| 7471 | issuer = ti; |
| 7472 | break; |
| 7473 | } |
| 7474 | } |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 7475 | } |
Remi Tricot-Le Breton | 1621dc1 | 2022-11-03 15:16:48 +0100 | [diff] [blame] | 7476 | AUTHORITY_KEYID_free(akid); |
Emmanuel Hocdet | 75a7aa1 | 2020-02-18 15:19:24 +0100 | [diff] [blame] | 7477 | return issuer; |
| 7478 | } |
| 7479 | |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 7480 | void ssl_free_global_issuers(void) |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 7481 | { |
| 7482 | struct eb64_node *node, *back; |
| 7483 | struct issuer_chain *issuer; |
| 7484 | |
William Lallemand | e0f3fd5 | 2020-02-25 14:53:06 +0100 | [diff] [blame] | 7485 | node = eb64_first(&cert_issuer_tree); |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 7486 | while (node) { |
| 7487 | issuer = container_of(node, typeof(*issuer), node); |
| 7488 | back = eb64_next(node); |
| 7489 | eb64_delete(node); |
| 7490 | free(issuer->path); |
| 7491 | sk_X509_pop_free(issuer->chain, X509_free); |
| 7492 | free(issuer); |
| 7493 | node = back; |
| 7494 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7495 | } |
| 7496 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 7497 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7498 | static int ssl_check_async_engine_count(void) { |
Christopher Faulet | fc633b6 | 2020-11-06 15:24:23 +0100 | [diff] [blame] | 7499 | int err_code = ERR_NONE; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 7500 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 7501 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7502 | 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] | 7503 | err_code = ERR_ABORT; |
| 7504 | } |
| 7505 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 7506 | } |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 7507 | #endif |
| 7508 | |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7509 | /* "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] | 7510 | * 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] | 7511 | */ |
Willy Tarreau | 8050efe | 2021-01-21 08:26:06 +0100 | [diff] [blame] | 7512 | 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] | 7513 | { |
| 7514 | const struct ssl_sock_ctx *sctx = ctx; |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7515 | int ret = 0; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7516 | |
| 7517 | if (!sctx) |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7518 | return ret; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7519 | |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7520 | if (sctx->conn != conn) { |
| 7521 | chunk_appendf(&trash, " xctx.conn=%p(BOGUS)", sctx->conn); |
| 7522 | ret = 1; |
| 7523 | } |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7524 | chunk_appendf(&trash, " xctx.st=%d", sctx->xprt_st); |
| 7525 | |
| 7526 | if (sctx->xprt) { |
| 7527 | chunk_appendf(&trash, " .xprt=%s", sctx->xprt->name); |
| 7528 | if (sctx->xprt_ctx) |
| 7529 | chunk_appendf(&trash, " .xctx=%p", sctx->xprt_ctx); |
| 7530 | } |
| 7531 | |
| 7532 | chunk_appendf(&trash, " .wait.ev=%d", sctx->wait_event.events); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7533 | |
| 7534 | /* as soon as a shutdown is reported the lower layer unregisters its |
| 7535 | * subscriber, so the situations below are transient and rare enough to |
| 7536 | * be reported as suspicious. In any case they shouldn't last. |
| 7537 | */ |
| 7538 | if ((sctx->wait_event.events & 1) && (conn->flags & (CO_FL_SOCK_RD_SH|CO_FL_ERROR))) |
| 7539 | ret = 1; |
| 7540 | if ((sctx->wait_event.events & 2) && (conn->flags & (CO_FL_SOCK_WR_SH|CO_FL_ERROR))) |
| 7541 | ret = 1; |
| 7542 | |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7543 | chunk_appendf(&trash, " .subs=%p", sctx->subs); |
| 7544 | if (sctx->subs) { |
| 7545 | 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] | 7546 | if (sctx->subs->tasklet->calls >= 1000000) |
| 7547 | ret = 1; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7548 | chunk_appendf(&trash, " tl.calls=%d tl.ctx=%p tl.fct=", |
| 7549 | sctx->subs->tasklet->calls, |
| 7550 | sctx->subs->tasklet->context); |
| 7551 | resolve_sym_name(&trash, NULL, sctx->subs->tasklet->process); |
| 7552 | chunk_appendf(&trash, ")"); |
| 7553 | } |
| 7554 | chunk_appendf(&trash, " .sent_early=%d", sctx->sent_early_data); |
| 7555 | chunk_appendf(&trash, " .early_in=%d", (int)sctx->early_buf.data); |
Willy Tarreau | 4bd5d63 | 2021-01-21 08:53:50 +0100 | [diff] [blame] | 7556 | return ret; |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 7557 | } |
| 7558 | |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7559 | #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] | 7560 | /* 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] | 7561 | * each reference. The variable <ref> must point to the current node's list |
| 7562 | * 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] | 7563 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7564 | static inline |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7565 | 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] | 7566 | { |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7567 | /* Get next list entry. */ |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7568 | ref = ref->n; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7569 | |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7570 | /* If the entry is the last of the list, return NULL. */ |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7571 | if (ref == end) |
Tim Duesterhus | 2c7bb33 | 2021-01-03 01:29:55 +0100 | [diff] [blame] | 7572 | return NULL; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7573 | |
Willy Tarreau | b6fc524 | 2021-01-05 10:44:30 +0100 | [diff] [blame] | 7574 | return LIST_ELEM(ref, struct tls_keys_ref *, list); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7575 | } |
| 7576 | |
| 7577 | static inline |
| 7578 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 7579 | { |
| 7580 | int id; |
| 7581 | char *error; |
| 7582 | |
| 7583 | /* If the reference starts by a '#', this is numeric id. */ |
| 7584 | if (reference[0] == '#') { |
| 7585 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 7586 | id = strtol(reference + 1, &error, 10); |
| 7587 | if (*error != '\0') |
| 7588 | return NULL; |
| 7589 | |
| 7590 | /* Perform the unique id lookup. */ |
| 7591 | return tlskeys_ref_lookupid(id); |
| 7592 | } |
| 7593 | |
| 7594 | /* Perform the string lookup. */ |
| 7595 | return tlskeys_ref_lookup(reference); |
| 7596 | } |
| 7597 | #endif |
| 7598 | |
| 7599 | |
| 7600 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 7601 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7602 | /* 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] | 7603 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) |
| 7604 | { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7605 | struct show_keys_ctx *ctx = appctx->svcctx; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7606 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7607 | switch (ctx->state) { |
| 7608 | case SHOW_KEYS_INIT: |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7609 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 7610 | * quit the function with returning 0. The function is called |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7611 | * later and restart at the state "SHOW_KEYS_INIT". |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7612 | */ |
| 7613 | chunk_reset(&trash); |
| 7614 | |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7615 | if (ctx->dump_entries) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7616 | chunk_appendf(&trash, "# id secret\n"); |
| 7617 | else |
| 7618 | chunk_appendf(&trash, "# id (file)\n"); |
| 7619 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7620 | if (applet_putchk(appctx, &trash) == -1) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7621 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7622 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7623 | /* Now, we start the browsing of the references lists. |
| 7624 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 7625 | * available field of this pointer is <list>. It is used with the function |
| 7626 | * tlskeys_list_get_next() for retruning the first available entry |
| 7627 | */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7628 | if (ctx->next_ref == NULL) |
| 7629 | ctx->next_ref = tlskeys_list_get_next(&tlskeys_reference, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7630 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7631 | ctx->state = SHOW_KEYS_LIST; |
Willy Tarreau | a551f4f | 2022-11-14 07:34:43 +0100 | [diff] [blame] | 7632 | __fallthrough; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7633 | |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7634 | case SHOW_KEYS_LIST: |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7635 | while (ctx->next_ref) { |
| 7636 | struct tls_keys_ref *ref = ctx->next_ref; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7637 | |
| 7638 | chunk_reset(&trash); |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7639 | if (ctx->dump_entries && ctx->next_index == 0) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7640 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7641 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7642 | if (ctx->next_index == 0) |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7643 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 7644 | |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7645 | if (ctx->dump_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7646 | int head; |
| 7647 | |
| 7648 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 7649 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7650 | while (ctx->next_index < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7651 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7652 | |
| 7653 | chunk_reset(t2); |
| 7654 | /* 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] | 7655 | if (ref->key_size_bits == 128) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7656 | 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] | 7657 | sizeof(struct tls_sess_key_128), |
| 7658 | t2->area, t2->size); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7659 | 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] | 7660 | t2->area); |
| 7661 | } |
| 7662 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7663 | 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] | 7664 | sizeof(struct tls_sess_key_256), |
| 7665 | t2->area, t2->size); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7666 | 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] | 7667 | t2->area); |
| 7668 | } |
| 7669 | else { |
| 7670 | /* This case should never happen */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7671 | 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] | 7672 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7673 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7674 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7675 | /* let's try again later from this stream. We add ourselves into |
| 7676 | * this stream's users so that it can remove us upon termination. |
| 7677 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7678 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7679 | return 0; |
| 7680 | } |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7681 | ctx->next_index++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7682 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 7683 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7684 | ctx->next_index = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7685 | } |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7686 | if (applet_putchk(appctx, &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7687 | /* let's try again later from this stream. We add ourselves into |
| 7688 | * this stream's users so that it can remove us upon termination. |
| 7689 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7690 | return 0; |
| 7691 | } |
| 7692 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7693 | if (ctx->names_only == 0) /* don't display everything if not necessary */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7694 | break; |
| 7695 | |
| 7696 | /* get next list entry and check the end of the list */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7697 | ctx->next_ref = tlskeys_list_get_next(&ref->list, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7698 | } |
Willy Tarreau | 9c5a38c | 2022-05-05 09:03:44 +0200 | [diff] [blame] | 7699 | ctx->state = SHOW_KEYS_DONE; |
Willy Tarreau | a551f4f | 2022-11-14 07:34:43 +0100 | [diff] [blame] | 7700 | __fallthrough; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7701 | |
| 7702 | default: |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7703 | return 1; |
| 7704 | } |
| 7705 | return 0; |
| 7706 | } |
| 7707 | |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7708 | /* 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] | 7709 | 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] | 7710 | { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7711 | struct show_keys_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 7712 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7713 | /* no parameter, shows only file list */ |
| 7714 | if (!*args[2]) { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7715 | ctx->names_only = 1; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7716 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7717 | } |
| 7718 | |
| 7719 | if (args[2][0] == '*') { |
| 7720 | /* list every TLS ticket keys */ |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7721 | ctx->names_only = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7722 | } else { |
Willy Tarreau | a938052 | 2022-05-05 08:50:17 +0200 | [diff] [blame] | 7723 | ctx->next_ref = tlskeys_ref_lookup_ref(args[2]); |
| 7724 | if (!ctx->next_ref) |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7725 | return cli_err(appctx, "'show tls-keys' unable to locate referenced filename\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7726 | } |
Willy Tarreau | bd33864 | 2022-05-05 08:59:17 +0200 | [diff] [blame] | 7727 | |
| 7728 | ctx->dump_entries = 1; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 7729 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7730 | } |
| 7731 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7732 | static int cli_parse_set_tlskeys(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7733 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7734 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 7735 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7736 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7737 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7738 | if (!*args[3] || !*args[4]) |
| 7739 | return cli_err(appctx, "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7740 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 7741 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7742 | if (!ref) |
| 7743 | return cli_err(appctx, "'set ssl tls-key' unable to locate referenced filename\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7744 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 7745 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7746 | if (ret < 0) |
| 7747 | return cli_err(appctx, "'set ssl tls-key' received invalid base64 encoded TLS key.\n"); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 7748 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 7749 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7750 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 7751 | return cli_err(appctx, "'set ssl tls-key' received a key of wrong size.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7752 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7753 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7754 | } |
William Lallemand | d4f946c | 2019-12-05 10:26:40 +0100 | [diff] [blame] | 7755 | #endif |
William Lallemand | 419e634 | 2020-04-08 12:05:39 +0200 | [diff] [blame] | 7756 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7757 | static int cli_parse_set_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7758 | { |
| 7759 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 7760 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 7761 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 7762 | |
| 7763 | if (!payload) |
| 7764 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7765 | |
| 7766 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7767 | if (!*payload) |
| 7768 | return cli_err(appctx, "'set ssl ocsp-response' expects response in base64 encoding.\n"); |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 7769 | |
| 7770 | /* remove \r and \n from the payload */ |
| 7771 | for (i = 0, j = 0; payload[i]; i++) { |
| 7772 | if (payload[i] == '\r' || payload[i] == '\n') |
| 7773 | continue; |
| 7774 | payload[j++] = payload[i]; |
| 7775 | } |
| 7776 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7777 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 7778 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7779 | if (ret < 0) |
| 7780 | return cli_err(appctx, "'set ssl ocsp-response' received invalid base64 encoded response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7781 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 7782 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7783 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7784 | if (err) |
| 7785 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 7786 | else |
| 7787 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7788 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7789 | |
| 7790 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7791 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 7792 | return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 7793 | #endif |
| 7794 | |
Elliot Otchet | 71f8297 | 2020-01-15 08:12:14 -0500 | [diff] [blame] | 7795 | } |
| 7796 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7797 | |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 7798 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7799 | static int cli_io_handler_show_ocspresponse_detail(struct appctx *appctx); |
| 7800 | #endif |
| 7801 | |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 7802 | /* parsing function for 'show ssl ocsp-response [id]'. If an entry is forced, |
| 7803 | * it's set into appctx->svcctx. |
| 7804 | */ |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7805 | static int cli_parse_show_ocspresponse(char **args, char *payload, struct appctx *appctx, void *private) |
| 7806 | { |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 7807 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7808 | if (*args[3]) { |
| 7809 | struct certificate_ocsp *ocsp = NULL; |
Remi Tricot-Le Breton | 9334843 | 2022-12-15 15:44:35 +0100 | [diff] [blame] | 7810 | char key[OCSP_MAX_CERTID_ASN1_LENGTH] = {}; |
| 7811 | int key_length = OCSP_MAX_CERTID_ASN1_LENGTH; |
| 7812 | char *key_ptr = key; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7813 | |
| 7814 | if (strlen(args[3]) > OCSP_MAX_CERTID_ASN1_LENGTH*2) { |
| 7815 | return cli_err(appctx, "'show ssl ocsp-response' received a too big key.\n"); |
| 7816 | } |
| 7817 | |
Remi Tricot-Le Breton | 9334843 | 2022-12-15 15:44:35 +0100 | [diff] [blame] | 7818 | if (!parse_binary(args[3], &key_ptr, &key_length, NULL)) { |
| 7819 | return cli_err(appctx, "'show ssl ocsp-response' received an invalid key.\n"); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7820 | } |
Remi Tricot-Le Breton | 9334843 | 2022-12-15 15:44:35 +0100 | [diff] [blame] | 7821 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7822 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
Remi Tricot-Le Breton | 9334843 | 2022-12-15 15:44:35 +0100 | [diff] [blame] | 7823 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7824 | |
| 7825 | if (!ocsp) { |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7826 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7827 | return cli_err(appctx, "Certificate ID does not match any certificate.\n"); |
| 7828 | } |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7829 | ++ocsp->refcount; |
| 7830 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7831 | |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 7832 | appctx->svcctx = ocsp; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7833 | appctx->io_handler = cli_io_handler_show_ocspresponse_detail; |
| 7834 | } |
| 7835 | |
| 7836 | return 0; |
| 7837 | |
| 7838 | #else |
| 7839 | return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"); |
| 7840 | #endif |
| 7841 | } |
| 7842 | |
| 7843 | |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 7844 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7845 | /* |
| 7846 | * This function dumps the details of an OCSP_CERTID. It is based on |
| 7847 | * ocsp_certid_print in OpenSSL. |
| 7848 | */ |
| 7849 | static inline int ocsp_certid_print(BIO *bp, OCSP_CERTID *certid, int indent) |
| 7850 | { |
| 7851 | ASN1_OCTET_STRING *piNameHash = NULL; |
| 7852 | ASN1_OCTET_STRING *piKeyHash = NULL; |
| 7853 | ASN1_INTEGER *pSerial = NULL; |
| 7854 | |
| 7855 | if (OCSP_id_get0_info(&piNameHash, NULL, &piKeyHash, &pSerial, certid)) { |
| 7856 | |
| 7857 | BIO_printf(bp, "%*sCertificate ID:\n", indent, ""); |
| 7858 | indent += 2; |
Remi Tricot-Le Breton | cc750ef | 2021-12-17 18:53:23 +0100 | [diff] [blame] | 7859 | BIO_printf(bp, "%*sIssuer Name Hash: ", indent, ""); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 7860 | #ifndef USE_OPENSSL_WOLFSSL |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7861 | i2a_ASN1_STRING(bp, piNameHash, 0); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 7862 | #else |
| 7863 | wolfSSL_ASN1_STRING_print(bp, piNameHash); |
| 7864 | #endif |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7865 | BIO_printf(bp, "\n%*sIssuer Key Hash: ", indent, ""); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 7866 | #ifndef USE_OPENSSL_WOLFSSL |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7867 | i2a_ASN1_STRING(bp, piKeyHash, 0); |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 7868 | #else |
| 7869 | wolfSSL_ASN1_STRING_print(bp, piNameHash); |
| 7870 | #endif |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7871 | BIO_printf(bp, "\n%*sSerial Number: ", indent, ""); |
| 7872 | i2a_ASN1_INTEGER(bp, pSerial); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7873 | } |
| 7874 | return 1; |
| 7875 | } |
| 7876 | #endif |
| 7877 | |
| 7878 | /* |
| 7879 | * IO handler of "show ssl ocsp-response". The command taking a specific ID |
| 7880 | * is managed in cli_io_handler_show_ocspresponse_detail. |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 7881 | * The current entry is taken from appctx->svcctx. |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7882 | */ |
| 7883 | static int cli_io_handler_show_ocspresponse(struct appctx *appctx) |
| 7884 | { |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 7885 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7886 | struct buffer *trash = alloc_trash_chunk(); |
| 7887 | struct buffer *tmp = NULL; |
| 7888 | struct ebmb_node *node; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7889 | struct certificate_ocsp *ocsp = NULL; |
| 7890 | BIO *bio = NULL; |
| 7891 | int write = -1; |
| 7892 | |
| 7893 | if (trash == NULL) |
| 7894 | return 1; |
| 7895 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7896 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
| 7897 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7898 | tmp = alloc_trash_chunk(); |
| 7899 | if (!tmp) |
| 7900 | goto end; |
| 7901 | |
| 7902 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 7903 | goto end; |
| 7904 | |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 7905 | if (!appctx->svcctx) { |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7906 | chunk_appendf(trash, "# Certificate IDs\n"); |
| 7907 | node = ebmb_first(&cert_ocsp_tree); |
| 7908 | } else { |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 7909 | node = &((struct certificate_ocsp *)appctx->svcctx)->key; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7910 | } |
| 7911 | |
| 7912 | while (node) { |
| 7913 | OCSP_CERTID *certid = NULL; |
| 7914 | const unsigned char *p = NULL; |
| 7915 | int i; |
| 7916 | |
| 7917 | ocsp = ebmb_entry(node, struct certificate_ocsp, key); |
| 7918 | |
| 7919 | /* Dump the key in hexadecimal */ |
| 7920 | chunk_appendf(trash, "Certificate ID key : "); |
| 7921 | for (i = 0; i < ocsp->key_length; ++i) { |
| 7922 | chunk_appendf(trash, "%02x", ocsp->key_data[i]); |
| 7923 | } |
| 7924 | chunk_appendf(trash, "\n"); |
| 7925 | |
| 7926 | p = ocsp->key_data; |
| 7927 | |
| 7928 | /* Decode the certificate ID (serialized into the key). */ |
| 7929 | d2i_OCSP_CERTID(&certid, &p, ocsp->key_length); |
Remi Tricot-Le Breton | 8081b67 | 2022-02-16 15:03:51 +0100 | [diff] [blame] | 7930 | if (!certid) |
| 7931 | goto end; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7932 | |
| 7933 | /* Dump the CERTID info */ |
| 7934 | ocsp_certid_print(bio, certid, 1); |
Remi Tricot-Le Breton | 8081b67 | 2022-02-16 15:03:51 +0100 | [diff] [blame] | 7935 | OCSP_CERTID_free(certid); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7936 | write = BIO_read(bio, tmp->area, tmp->size-1); |
Remi Tricot-Le Breton | cc750ef | 2021-12-17 18:53:23 +0100 | [diff] [blame] | 7937 | /* strip trailing LFs */ |
| 7938 | while (write > 0 && tmp->area[write-1] == '\n') |
| 7939 | write--; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7940 | tmp->area[write] = '\0'; |
| 7941 | |
| 7942 | chunk_appendf(trash, "%s\n", tmp->area); |
| 7943 | |
| 7944 | node = ebmb_next(node); |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 7945 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7946 | goto yield; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7947 | } |
| 7948 | |
| 7949 | end: |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7950 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 7951 | appctx->svcctx = NULL; |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7952 | free_trash_chunk(trash); |
| 7953 | free_trash_chunk(tmp); |
| 7954 | BIO_free(bio); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7955 | return 1; |
| 7956 | |
| 7957 | yield: |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7958 | free_trash_chunk(trash); |
| 7959 | free_trash_chunk(tmp); |
| 7960 | BIO_free(bio); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7961 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7962 | ++ocsp->refcount; |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 7963 | appctx->svcctx = ocsp; |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 7964 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 7965 | return 0; |
| 7966 | #else |
| 7967 | return cli_err(appctx, "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"); |
| 7968 | #endif |
| 7969 | } |
| 7970 | |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 7971 | #ifdef HAVE_SSL_PROVIDERS |
| 7972 | struct provider_name { |
| 7973 | const char *name; |
| 7974 | struct list list; |
| 7975 | }; |
| 7976 | |
| 7977 | |
| 7978 | static int ssl_provider_get_name_cb(OSSL_PROVIDER *provider, void *cbdata) |
| 7979 | { |
| 7980 | struct list *provider_names = cbdata; |
| 7981 | struct provider_name *item = NULL; |
| 7982 | const char *name = OSSL_PROVIDER_get0_name(provider); |
| 7983 | |
| 7984 | if (!provider_names) |
| 7985 | return 0; |
| 7986 | |
| 7987 | item = calloc(1, sizeof(*item)); |
| 7988 | |
| 7989 | if (!item) |
| 7990 | return 0; |
| 7991 | |
| 7992 | item->name = name; |
| 7993 | LIST_APPEND(provider_names, &item->list); |
| 7994 | |
| 7995 | return 1; |
| 7996 | } |
| 7997 | |
| 7998 | static void ssl_provider_get_name_list(struct list *provider_names) |
| 7999 | { |
| 8000 | if (!provider_names) |
| 8001 | return; |
| 8002 | |
| 8003 | OSSL_PROVIDER_do_all(NULL, ssl_provider_get_name_cb, provider_names); |
| 8004 | } |
| 8005 | |
| 8006 | static void ssl_provider_clear_name_list(struct list *provider_names) |
| 8007 | { |
| 8008 | struct provider_name *item = NULL, *item_s = NULL; |
| 8009 | |
| 8010 | if (provider_names) { |
| 8011 | list_for_each_entry_safe(item, item_s, provider_names, list) { |
| 8012 | LIST_DELETE(&item->list); |
| 8013 | free(item); |
| 8014 | } |
| 8015 | } |
| 8016 | } |
| 8017 | |
| 8018 | static int cli_io_handler_show_providers(struct appctx *appctx) |
| 8019 | { |
| 8020 | struct buffer *trash = get_trash_chunk(); |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 8021 | struct list provider_names; |
| 8022 | struct provider_name *name; |
| 8023 | |
| 8024 | LIST_INIT(&provider_names); |
| 8025 | |
| 8026 | chunk_appendf(trash, "Loaded providers : \n"); |
| 8027 | |
| 8028 | ssl_provider_get_name_list(&provider_names); |
| 8029 | |
| 8030 | list_for_each_entry(name, &provider_names, list) { |
| 8031 | chunk_appendf(trash, "\t- %s\n", name->name); |
| 8032 | } |
| 8033 | |
| 8034 | ssl_provider_clear_name_list(&provider_names); |
| 8035 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 8036 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 8037 | goto yield; |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 8038 | |
| 8039 | return 1; |
| 8040 | |
| 8041 | yield: |
| 8042 | return 0; |
| 8043 | } |
| 8044 | #endif |
| 8045 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8046 | |
Remi Tricot-Le Breton | 3faf0cb | 2021-06-10 18:10:32 +0200 | [diff] [blame] | 8047 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8048 | /* |
| 8049 | * Dump the details about an OCSP response in DER format stored in |
| 8050 | * <ocsp_response> into buffer <out>. |
| 8051 | * Returns 0 in case of success. |
| 8052 | */ |
| 8053 | int ssl_ocsp_response_print(struct buffer *ocsp_response, struct buffer *out) |
| 8054 | { |
| 8055 | BIO *bio = NULL; |
| 8056 | int write = -1; |
| 8057 | OCSP_RESPONSE *resp; |
| 8058 | const unsigned char *p; |
Remi Tricot-Le Breton | a9a591a | 2022-02-16 14:42:22 +0100 | [diff] [blame] | 8059 | int retval = -1; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8060 | |
| 8061 | if (!ocsp_response) |
| 8062 | return -1; |
| 8063 | |
| 8064 | if ((bio = BIO_new(BIO_s_mem())) == NULL) |
| 8065 | return -1; |
| 8066 | |
| 8067 | p = (const unsigned char*)ocsp_response->area; |
| 8068 | |
| 8069 | resp = d2i_OCSP_RESPONSE(NULL, &p, ocsp_response->data); |
| 8070 | if (!resp) { |
| 8071 | chunk_appendf(out, "Unable to parse OCSP response"); |
Remi Tricot-Le Breton | 8081b67 | 2022-02-16 15:03:51 +0100 | [diff] [blame] | 8072 | goto end; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8073 | } |
| 8074 | |
Uriah Pollock | 3cbf09e | 2022-11-23 16:41:25 +0100 | [diff] [blame] | 8075 | #ifndef USE_OPENSSL_WOLFSSL |
| 8076 | if (OCSP_RESPONSE_print(bio, resp, 0) != 0) { |
| 8077 | #else |
| 8078 | if (wolfSSL_d2i_OCSP_RESPONSE_bio(bio, &resp) != 0) { |
| 8079 | #endif |
Remi Tricot-Le Breton | 2e7d1eb | 2022-01-11 10:11:10 +0100 | [diff] [blame] | 8080 | struct buffer *trash = get_trash_chunk(); |
| 8081 | struct ist ist_block = IST_NULL; |
| 8082 | struct ist ist_double_lf = IST_NULL; |
| 8083 | static struct ist double_lf = IST("\n\n"); |
| 8084 | |
| 8085 | write = BIO_read(bio, trash->area, trash->size - 1); |
Remi Tricot-Le Breton | 1b01b7f | 2022-02-16 15:17:09 +0100 | [diff] [blame] | 8086 | if (write <= 0) |
| 8087 | goto end; |
Remi Tricot-Le Breton | 2e7d1eb | 2022-01-11 10:11:10 +0100 | [diff] [blame] | 8088 | trash->data = write; |
| 8089 | |
| 8090 | /* Look for empty lines in the 'trash' buffer and add a space to |
| 8091 | * the beginning to avoid having empty lines in the output |
| 8092 | * (without changing the appearance of the information |
| 8093 | * displayed). |
| 8094 | */ |
| 8095 | ist_block = ist2(b_orig(trash), b_data(trash)); |
| 8096 | |
| 8097 | ist_double_lf = istist(ist_block, double_lf); |
| 8098 | |
| 8099 | while (istlen(ist_double_lf)) { |
| 8100 | /* istptr(ist_double_lf) points to the first \n of a |
| 8101 | * \n\n pattern. |
| 8102 | */ |
| 8103 | uint empty_line_offset = istptr(ist_double_lf) + 1 - istptr(ist_block); |
| 8104 | |
| 8105 | /* Write up to the first '\n' of the "\n\n" pattern into |
| 8106 | * the output buffer. |
| 8107 | */ |
| 8108 | b_putblk(out, istptr(ist_block), empty_line_offset); |
| 8109 | /* Add an extra space. */ |
| 8110 | b_putchr(out, ' '); |
| 8111 | |
| 8112 | /* Keep looking for empty lines in the rest of the data. */ |
| 8113 | ist_block = istadv(ist_block, empty_line_offset); |
| 8114 | |
| 8115 | ist_double_lf = istist(ist_block, double_lf); |
| 8116 | } |
| 8117 | |
Remi Tricot-Le Breton | a9a591a | 2022-02-16 14:42:22 +0100 | [diff] [blame] | 8118 | retval = (b_istput(out, ist_block) <= 0); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8119 | } |
| 8120 | |
Remi Tricot-Le Breton | 8081b67 | 2022-02-16 15:03:51 +0100 | [diff] [blame] | 8121 | end: |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8122 | if (bio) |
| 8123 | BIO_free(bio); |
| 8124 | |
Remi Tricot-Le Breton | 8081b67 | 2022-02-16 15:03:51 +0100 | [diff] [blame] | 8125 | OCSP_RESPONSE_free(resp); |
| 8126 | |
Remi Tricot-Le Breton | a9a591a | 2022-02-16 14:42:22 +0100 | [diff] [blame] | 8127 | return retval; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8128 | } |
| 8129 | |
| 8130 | /* |
| 8131 | * Dump the details of the OCSP response of ID <ocsp_certid> into buffer <out>. |
| 8132 | * Returns 0 in case of success. |
| 8133 | */ |
| 8134 | int ssl_get_ocspresponse_detail(unsigned char *ocsp_certid, struct buffer *out) |
| 8135 | { |
| 8136 | struct certificate_ocsp *ocsp; |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 8137 | int ret = 0; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8138 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 8139 | HA_SPIN_LOCK(OCSP_LOCK, &ocsp_tree_lock); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8140 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, ocsp_certid, OCSP_MAX_CERTID_ASN1_LENGTH); |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 8141 | if (!ocsp) { |
| 8142 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8143 | return -1; |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 8144 | } |
| 8145 | |
| 8146 | ret = ssl_ocsp_response_print(&ocsp->response, out); |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8147 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 8148 | HA_SPIN_UNLOCK(OCSP_LOCK, &ocsp_tree_lock); |
| 8149 | |
| 8150 | return ret; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8151 | } |
| 8152 | |
| 8153 | |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 8154 | /* IO handler of details "show ssl ocsp-response <id>". |
| 8155 | * The current entry is taken from appctx->svcctx. |
| 8156 | */ |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8157 | static int cli_io_handler_show_ocspresponse_detail(struct appctx *appctx) |
| 8158 | { |
Remi Tricot-Le Breton | e3d5f9a | 2022-12-15 15:44:36 +0100 | [diff] [blame] | 8159 | struct buffer *trash = get_trash_chunk(); |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 8160 | struct certificate_ocsp *ocsp = appctx->svcctx; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8161 | |
Remi Tricot-Le Breton | e3d5f9a | 2022-12-15 15:44:36 +0100 | [diff] [blame] | 8162 | if (ssl_ocsp_response_print(&ocsp->response, trash)) |
| 8163 | goto end; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8164 | |
Willy Tarreau | d0a06d5 | 2022-05-18 15:07:19 +0200 | [diff] [blame] | 8165 | if (applet_putchk(appctx, trash) == -1) |
Remi Tricot-Le Breton | e3d5f9a | 2022-12-15 15:44:36 +0100 | [diff] [blame] | 8166 | return 0; |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 8167 | |
Remi Tricot-Le Breton | e3d5f9a | 2022-12-15 15:44:36 +0100 | [diff] [blame] | 8168 | end: |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 8169 | ssl_sock_free_ocsp(ocsp); |
Willy Tarreau | 170b35b | 2022-05-05 09:09:15 +0200 | [diff] [blame] | 8170 | appctx->svcctx = NULL; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8171 | return 1; |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8172 | } |
Remi Tricot-Le Breton | eeaa29b | 2022-12-20 11:11:07 +0100 | [diff] [blame] | 8173 | |
| 8174 | struct ocsp_cli_ctx { |
| 8175 | struct httpclient *hc; |
| 8176 | struct ckch_data *ckch_data; |
| 8177 | uint flags; |
| 8178 | uint do_update; |
| 8179 | }; |
| 8180 | |
| 8181 | const struct http_hdr ocsp_request_hdrs[] = { |
| 8182 | { IST("Content-Type"), IST("application/ocsp-request") }, |
| 8183 | { IST_NULL, IST_NULL } |
| 8184 | }; |
| 8185 | |
| 8186 | void cli_ocsp_res_stline_cb(struct httpclient *hc) |
| 8187 | { |
| 8188 | struct appctx *appctx = hc->caller; |
| 8189 | struct ocsp_cli_ctx *ctx; |
| 8190 | |
| 8191 | if (!appctx) |
| 8192 | return; |
| 8193 | |
| 8194 | ctx = appctx->svcctx; |
| 8195 | ctx->flags |= HC_F_RES_STLINE; |
| 8196 | appctx_wakeup(appctx); |
| 8197 | } |
| 8198 | |
| 8199 | void cli_ocsp_res_headers_cb(struct httpclient *hc) |
| 8200 | { |
| 8201 | struct appctx *appctx = hc->caller; |
| 8202 | struct ocsp_cli_ctx *ctx; |
| 8203 | |
| 8204 | if (!appctx) |
| 8205 | return; |
| 8206 | |
| 8207 | ctx = appctx->svcctx; |
| 8208 | ctx->flags |= HC_F_RES_HDR; |
| 8209 | appctx_wakeup(appctx); |
| 8210 | } |
| 8211 | |
| 8212 | void cli_ocsp_res_body_cb(struct httpclient *hc) |
| 8213 | { |
| 8214 | struct appctx *appctx = hc->caller; |
| 8215 | struct ocsp_cli_ctx *ctx; |
| 8216 | |
| 8217 | if (!appctx) |
| 8218 | return; |
| 8219 | |
| 8220 | ctx = appctx->svcctx; |
| 8221 | ctx->flags |= HC_F_RES_BODY; |
| 8222 | appctx_wakeup(appctx); |
| 8223 | } |
| 8224 | |
| 8225 | void cli_ocsp_res_end_cb(struct httpclient *hc) |
| 8226 | { |
| 8227 | struct appctx *appctx = hc->caller; |
| 8228 | struct ocsp_cli_ctx *ctx; |
| 8229 | |
| 8230 | if (!appctx) |
| 8231 | return; |
| 8232 | |
| 8233 | ctx = appctx->svcctx; |
| 8234 | ctx->flags |= HC_F_RES_END; |
| 8235 | appctx_wakeup(appctx); |
| 8236 | } |
| 8237 | |
| 8238 | static int cli_parse_update_ocsp_response(char **args, char *payload, struct appctx *appctx, void *private) |
| 8239 | { |
| 8240 | int errcode = 0; |
| 8241 | char *err = NULL; |
| 8242 | struct ckch_store *ckch_store = NULL; |
| 8243 | X509 *cert = NULL; |
| 8244 | struct ocsp_cli_ctx *ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 8245 | struct httpclient *hc = NULL; |
| 8246 | struct buffer *req_url = NULL; |
| 8247 | struct buffer *req_body = NULL; |
| 8248 | OCSP_CERTID *certid = NULL; |
| 8249 | |
| 8250 | if (!*args[3]) { |
| 8251 | memprintf(&err, "'update ssl ocsp-response' expects a filename\n"); |
| 8252 | return cli_dynerr(appctx, err); |
| 8253 | } |
| 8254 | |
| 8255 | req_url = alloc_trash_chunk(); |
| 8256 | if (!req_url) { |
| 8257 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 8258 | errcode |= ERR_ALERT | ERR_FATAL; |
| 8259 | goto end; |
| 8260 | } |
| 8261 | |
| 8262 | req_body = alloc_trash_chunk(); |
| 8263 | if (!req_body) { |
| 8264 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 8265 | errcode |= ERR_ALERT | ERR_FATAL; |
| 8266 | goto end; |
| 8267 | } |
| 8268 | |
| 8269 | /* The operations on the CKCH architecture are locked so we can |
| 8270 | * manipulate ckch_store and ckch_inst */ |
| 8271 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) { |
| 8272 | memprintf(&err, "%sCan't update the certificate!\nOperations on certificates are currently locked!\n", err ? err : ""); |
| 8273 | errcode |= ERR_ALERT | ERR_FATAL; |
| 8274 | goto end; |
| 8275 | } |
| 8276 | |
| 8277 | ckch_store = ckchs_lookup(args[3]); |
| 8278 | |
| 8279 | if (!ckch_store) { |
| 8280 | memprintf(&err, "%sCkch_store not found!\n", err ? err : ""); |
| 8281 | errcode |= ERR_ALERT | ERR_FATAL; |
| 8282 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 8283 | goto end; |
| 8284 | } |
| 8285 | |
| 8286 | ctx->ckch_data = ckch_store->data; |
| 8287 | |
| 8288 | cert = ckch_store->data->cert; |
| 8289 | |
| 8290 | if (ssl_ocsp_get_uri_from_cert(cert, req_url, &err)) { |
| 8291 | errcode |= ERR_ALERT | ERR_FATAL; |
| 8292 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 8293 | goto end; |
| 8294 | } |
| 8295 | |
| 8296 | certid = OCSP_cert_to_id(NULL, ctx->ckch_data->cert, ctx->ckch_data->ocsp_issuer); |
| 8297 | if (certid == NULL) { |
| 8298 | memprintf(&err, "%sOCSP_cert_to_id() error\n", err ? err : ""); |
| 8299 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 8300 | goto end; |
| 8301 | } |
| 8302 | |
| 8303 | /* From here on the lock is not needed anymore. */ |
| 8304 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 8305 | |
| 8306 | /* Create ocsp request */ |
| 8307 | if (ssl_ocsp_create_request_details(certid, req_url, req_body, &err) != 0) { |
| 8308 | memprintf(&err, "%sCreate ocsp request error\n", err ? err : ""); |
| 8309 | goto end; |
| 8310 | } |
| 8311 | |
| 8312 | hc = httpclient_new(appctx, b_data(req_body) ? HTTP_METH_POST : HTTP_METH_GET, ist2(b_orig(req_url), b_data(req_url))); |
| 8313 | if (!hc) { |
| 8314 | memprintf(&err, "%sCan't allocate httpclient\n", err ? err : ""); |
| 8315 | goto end; |
| 8316 | } |
| 8317 | |
| 8318 | if (httpclient_req_gen(hc, hc->req.url, hc->req.meth, b_data(req_body) ? ocsp_request_hdrs : NULL, |
| 8319 | ist2(b_orig(req_body), b_data(req_body))) != ERR_NONE) { |
| 8320 | memprintf(&err, "%shttpclient_req_gen() error\n", err ? err : ""); |
| 8321 | goto end; |
| 8322 | } |
| 8323 | |
| 8324 | hc->ops.res_stline = cli_ocsp_res_stline_cb; |
| 8325 | hc->ops.res_headers = cli_ocsp_res_headers_cb; |
| 8326 | hc->ops.res_payload = cli_ocsp_res_body_cb; |
| 8327 | hc->ops.res_end = cli_ocsp_res_end_cb; |
| 8328 | |
| 8329 | ctx->hc = hc; /* store the httpclient ptr in the applet */ |
| 8330 | ctx->flags = 0; |
| 8331 | |
| 8332 | if (!httpclient_start(hc)) { |
| 8333 | memprintf(&err, "%shttpclient_start() error\n", err ? err : ""); |
| 8334 | goto end; |
| 8335 | } |
| 8336 | |
| 8337 | free_trash_chunk(req_url); |
| 8338 | |
| 8339 | return 0; |
| 8340 | |
| 8341 | end: |
| 8342 | free_trash_chunk(req_url); |
| 8343 | |
| 8344 | if (errcode & ERR_CODE) { |
| 8345 | return cli_dynerr(appctx, memprintf(&err, "%sCan't send ocsp request for %s!\n", err ? err : "", args[3])); |
| 8346 | } |
| 8347 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
| 8348 | } |
| 8349 | |
| 8350 | static int cli_io_handler_update_ocsp_response(struct appctx *appctx) |
| 8351 | { |
| 8352 | struct ocsp_cli_ctx *ctx = appctx->svcctx; |
| 8353 | struct httpclient *hc = ctx->hc; |
| 8354 | |
| 8355 | if (ctx->flags & HC_F_RES_STLINE) { |
| 8356 | if (hc->res.status != 200) { |
| 8357 | chunk_printf(&trash, "OCSP response error (status %d)\n", hc->res.status); |
| 8358 | if (applet_putchk(appctx, &trash) == -1) |
| 8359 | goto more; |
| 8360 | goto end; |
| 8361 | } |
| 8362 | ctx->flags &= ~HC_F_RES_STLINE; |
| 8363 | } |
| 8364 | |
| 8365 | if (ctx->flags & HC_F_RES_HDR) { |
| 8366 | struct http_hdr *hdr; |
| 8367 | int found = 0; |
| 8368 | /* Look for "Content-Type" header which should have |
| 8369 | * "application/ocsp-response" value. */ |
| 8370 | for (hdr = hc->res.hdrs; isttest(hdr->v); hdr++) { |
| 8371 | if (isteqi(hdr->n, ist("Content-Type")) && |
| 8372 | isteqi(hdr->v, ist("application/ocsp-response"))) { |
| 8373 | found = 1; |
| 8374 | break; |
| 8375 | } |
| 8376 | } |
| 8377 | if (!found) { |
| 8378 | fprintf(stderr, "Missing 'Content-Type: application/ocsp-response' header\n"); |
| 8379 | goto end; |
| 8380 | } |
| 8381 | ctx->flags &= ~HC_F_RES_HDR; |
| 8382 | } |
| 8383 | |
| 8384 | if (ctx->flags & HC_F_RES_BODY) { |
| 8385 | /* Wait until the full body is received and HC_F_RES_END flag is |
| 8386 | * set. */ |
| 8387 | } |
| 8388 | |
| 8389 | /* we must close only if F_END is the last flag */ |
| 8390 | if (ctx->flags & HC_F_RES_END) { |
| 8391 | char *err = NULL; |
| 8392 | |
| 8393 | if (ssl_ocsp_check_response(ctx->ckch_data->chain, ctx->ckch_data->ocsp_issuer, &hc->res.buf, &err)) { |
| 8394 | chunk_printf(&trash, "%s", err); |
| 8395 | if (applet_putchk(appctx, &trash) == -1) |
| 8396 | goto more; |
| 8397 | goto end; |
| 8398 | } |
| 8399 | |
| 8400 | if (ssl_sock_update_ocsp_response(&hc->res.buf, &err) != 0) { |
| 8401 | chunk_printf(&trash, "%s", err); |
| 8402 | if (applet_putchk(appctx, &trash) == -1) |
| 8403 | goto more; |
| 8404 | goto end; |
| 8405 | } |
| 8406 | |
| 8407 | chunk_reset(&trash); |
| 8408 | |
| 8409 | if (ssl_ocsp_response_print(&hc->res.buf, &trash)) |
| 8410 | goto end; |
| 8411 | |
| 8412 | if (applet_putchk(appctx, &trash) == -1) |
| 8413 | goto more; |
| 8414 | ctx->flags &= ~HC_F_RES_BODY; |
| 8415 | ctx->flags &= ~HC_F_RES_END; |
| 8416 | goto end; |
| 8417 | } |
| 8418 | |
| 8419 | more: |
| 8420 | if (!ctx->flags) |
| 8421 | applet_have_no_more_data(appctx); |
| 8422 | return 0; |
| 8423 | end: |
| 8424 | return 1; |
| 8425 | } |
| 8426 | |
| 8427 | static void cli_release_update_ocsp_response(struct appctx *appctx) |
| 8428 | { |
| 8429 | struct ocsp_cli_ctx *ctx = appctx->svcctx; |
| 8430 | struct httpclient *hc = ctx->hc; |
| 8431 | |
| 8432 | /* Everything possible was printed on the CLI, we can destroy the client */ |
| 8433 | httpclient_stop_and_destroy(hc); |
| 8434 | |
| 8435 | return; |
| 8436 | } |
| 8437 | |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8438 | #endif |
| 8439 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8440 | /* register cli keywords */ |
| 8441 | static struct cli_kw_list cli_kws = {{ },{ |
| 8442 | #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] | 8443 | { { "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 }, |
| 8444 | { { "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] | 8445 | #endif |
Remi Tricot-Le Breton | eeaa29b | 2022-12-20 11:11:07 +0100 | [diff] [blame] | 8446 | { { "set", "ssl", "ocsp-response", NULL }, "set ssl ocsp-response <resp|payload> : update a certificate's OCSP Response from a base64-encode DER", cli_parse_set_ocspresponse, NULL }, |
Remi Tricot-Le Breton | d92fd11 | 2021-06-10 13:51:13 +0200 | [diff] [blame] | 8447 | |
Remi Tricot-Le Breton | eeaa29b | 2022-12-20 11:11:07 +0100 | [diff] [blame] | 8448 | { { "show", "ssl", "ocsp-response", NULL }, "show ssl ocsp-response [id] : display the IDs of the OCSP responses used in memory, or the details of a single OCSP response", cli_parse_show_ocspresponse, cli_io_handler_show_ocspresponse, NULL }, |
| 8449 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 8450 | { { "update", "ssl", "ocsp-response", NULL }, "update ssl ocsp-response <certfile> : send ocsp request and update stored ocsp response", cli_parse_update_ocsp_response, cli_io_handler_update_ocsp_response, cli_release_update_ocsp_response }, |
| 8451 | #endif |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 8452 | #ifdef HAVE_SSL_PROVIDERS |
| 8453 | { { "show", "ssl", "providers", NULL }, "show ssl providers : show loaded SSL providers", NULL, cli_io_handler_show_providers }, |
| 8454 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8455 | { { NULL }, NULL, NULL, NULL } |
| 8456 | }}; |
| 8457 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8458 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8459 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 8460 | /* transport-layer operations for SSL sockets */ |
William Lallemand | dad3105 | 2020-05-14 17:47:32 +0200 | [diff] [blame] | 8461 | struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8462 | .snd_buf = ssl_sock_from_buf, |
| 8463 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 8464 | .subscribe = ssl_subscribe, |
| 8465 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 8466 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 8467 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8468 | .rcv_pipe = NULL, |
| 8469 | .snd_pipe = NULL, |
| 8470 | .shutr = NULL, |
| 8471 | .shutw = ssl_sock_shutw, |
| 8472 | .close = ssl_sock_close, |
| 8473 | .init = ssl_sock_init, |
Olivier Houchard | bc5ce92 | 2021-03-05 23:47:00 +0100 | [diff] [blame] | 8474 | .start = ssl_sock_start, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 8475 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 8476 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 8477 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 8478 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 8479 | .get_alpn = ssl_sock_get_alpn, |
Olivier Houchard | a74bb7e | 2020-07-03 14:01:21 +0200 | [diff] [blame] | 8480 | .takeover = ssl_takeover, |
Willy Tarreau | 4149168 | 2021-03-02 17:29:56 +0100 | [diff] [blame] | 8481 | .set_idle = ssl_set_idle, |
| 8482 | .set_used = ssl_set_used, |
Willy Tarreau | de82795 | 2022-04-11 10:43:28 +0200 | [diff] [blame] | 8483 | .get_ssl_sock_ctx = ssl_sock_get_ctx, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 8484 | .name = "SSL", |
Willy Tarreau | de5675a | 2021-01-20 14:41:29 +0100 | [diff] [blame] | 8485 | .show_fd = ssl_sock_show_fd, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8486 | }; |
| 8487 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8488 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 8489 | struct session *sess, struct stream *s, int flags) |
| 8490 | { |
| 8491 | struct connection *conn; |
| 8492 | |
| 8493 | conn = objt_conn(sess->origin); |
| 8494 | |
Willy Tarreau | b52d4d2 | 2022-05-27 10:44:39 +0200 | [diff] [blame] | 8495 | if (conn) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8496 | 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] | 8497 | sc_ep_set(s->scf, SE_FL_WAIT_FOR_HS); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 8498 | s->req.flags |= CF_READ_NULL; |
| 8499 | return ACT_RET_YIELD; |
| 8500 | } |
| 8501 | } |
| 8502 | return (ACT_RET_CONT); |
| 8503 | } |
| 8504 | |
| 8505 | 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) |
| 8506 | { |
| 8507 | rule->action_ptr = ssl_action_wait_for_hs; |
| 8508 | |
| 8509 | return ACT_RET_PRS_OK; |
| 8510 | } |
| 8511 | |
| 8512 | static struct action_kw_list http_req_actions = {ILH, { |
| 8513 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 8514 | { /* END */ } |
| 8515 | }}; |
| 8516 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 8517 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 8518 | |
Ilya Shipitsin | f00cdb1 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 8519 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 8520 | |
| 8521 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8522 | { |
| 8523 | if (ptr) { |
| 8524 | chunk_destroy(ptr); |
| 8525 | free(ptr); |
| 8526 | } |
| 8527 | } |
| 8528 | |
| 8529 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 8530 | |
| 8531 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 8532 | static void ssl_sock_ocsp_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8533 | { |
| 8534 | struct ocsp_cbk_arg *ocsp_arg; |
| 8535 | |
| 8536 | if (ptr) { |
| 8537 | ocsp_arg = ptr; |
| 8538 | |
| 8539 | if (ocsp_arg->is_single) { |
| 8540 | ssl_sock_free_ocsp(ocsp_arg->s_ocsp); |
| 8541 | ocsp_arg->s_ocsp = NULL; |
| 8542 | } else { |
| 8543 | int i; |
| 8544 | |
| 8545 | for (i = 0; i < SSL_SOCK_NUM_KEYTYPES; i++) { |
| 8546 | ssl_sock_free_ocsp(ocsp_arg->m_ocsp[i]); |
| 8547 | ocsp_arg->m_ocsp[i] = NULL; |
| 8548 | } |
| 8549 | } |
| 8550 | free(ocsp_arg); |
| 8551 | } |
| 8552 | } |
| 8553 | #endif |
| 8554 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8555 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8556 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8557 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 8558 | } |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 8559 | |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 8560 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 8561 | static void ssl_sock_keylog_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8562 | { |
| 8563 | struct ssl_keylog *keylog; |
| 8564 | |
| 8565 | if (!ptr) |
| 8566 | return; |
| 8567 | |
| 8568 | keylog = ptr; |
| 8569 | |
| 8570 | pool_free(pool_head_ssl_keylog_str, keylog->client_random); |
| 8571 | pool_free(pool_head_ssl_keylog_str, keylog->client_early_traffic_secret); |
| 8572 | pool_free(pool_head_ssl_keylog_str, keylog->client_handshake_traffic_secret); |
| 8573 | pool_free(pool_head_ssl_keylog_str, keylog->server_handshake_traffic_secret); |
| 8574 | pool_free(pool_head_ssl_keylog_str, keylog->client_traffic_secret_0); |
| 8575 | pool_free(pool_head_ssl_keylog_str, keylog->server_traffic_secret_0); |
| 8576 | pool_free(pool_head_ssl_keylog_str, keylog->exporter_secret); |
| 8577 | pool_free(pool_head_ssl_keylog_str, keylog->early_exporter_secret); |
| 8578 | |
| 8579 | pool_free(pool_head_ssl_keylog, ptr); |
| 8580 | } |
| 8581 | #endif |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 8582 | |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 8583 | static void ssl_sock_clt_crt_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8584 | { |
| 8585 | if (!ptr) |
| 8586 | return; |
| 8587 | |
| 8588 | X509_free((X509*)ptr); |
| 8589 | } |
| 8590 | |
Remi Tricot-Le Breton | a996763 | 2022-01-07 17:12:01 +0100 | [diff] [blame] | 8591 | static void ssl_sock_clt_sni_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 8592 | { |
| 8593 | pool_free(ssl_sock_client_sni_pool, ptr); |
| 8594 | } |
| 8595 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8596 | static void __ssl_sock_init(void) |
| 8597 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 8598 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8599 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 8600 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 8601 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8602 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8603 | if (global_ssl.listen_default_ciphers) |
| 8604 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 8605 | if (global_ssl.connect_default_ciphers) |
| 8606 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Ilya Shipitsin | f34ed0b | 2020-11-21 14:37:34 +0500 | [diff] [blame] | 8607 | #ifdef HAVE_SSL_CTX_SET_CIPHERSUITES |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8608 | if (global_ssl.listen_default_ciphersuites) |
| 8609 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 8610 | if (global_ssl.connect_default_ciphersuites) |
| 8611 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8612 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 8613 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 8614 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8615 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8616 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8617 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 8618 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8619 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 8620 | n = sk_SSL_COMP_num(cm); |
| 8621 | while (n--) { |
| 8622 | (void) sk_SSL_COMP_pop(cm); |
| 8623 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 8624 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 8625 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8626 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 8627 | ssl_locking_init(); |
| 8628 | #endif |
Ilya Shipitsin | f00cdb1 | 2021-02-06 18:59:22 +0500 | [diff] [blame] | 8629 | #ifdef HAVE_SSL_CTX_ADD_SERVER_CUSTOM_EXT |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 8630 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 8631 | #endif |
William Lallemand | 76b4a12 | 2020-08-04 17:41:39 +0200 | [diff] [blame] | 8632 | |
| 8633 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) && !defined OPENSSL_IS_BORINGSSL) |
| 8634 | ocsp_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_ocsp_free_func); |
| 8635 | #endif |
| 8636 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 8637 | 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] | 8638 | 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] | 8639 | #ifdef USE_QUIC |
| 8640 | ssl_qc_app_data_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 8641 | #endif /* USE_QUIC */ |
William Lallemand | 722180a | 2021-06-09 16:46:12 +0200 | [diff] [blame] | 8642 | #ifdef HAVE_SSL_KEYLOG |
William Lallemand | 7d42ef5 | 2020-07-06 11:41:30 +0200 | [diff] [blame] | 8643 | ssl_keylog_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_keylog_free_func); |
| 8644 | #endif |
Remi Tricot-Le Breton | 74f6ab6 | 2021-08-19 18:06:30 +0200 | [diff] [blame] | 8645 | 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] | 8646 | 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] | 8647 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8648 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8649 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8650 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 8651 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8652 | hap_register_post_check(tlskeys_finalize_config); |
| 8653 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8654 | |
| 8655 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 8656 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 8657 | |
Emmanuel Hocdet | 70df7bf | 2019-01-04 11:08:20 +0100 | [diff] [blame] | 8658 | hap_register_post_deinit(ssl_free_global_issuers); |
| 8659 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8660 | #ifndef OPENSSL_NO_DH |
| 8661 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 8662 | hap_register_post_deinit(ssl_free_dh); |
| 8663 | #endif |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 8664 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8665 | hap_register_post_deinit(ssl_free_engines); |
| 8666 | #endif |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 8667 | #ifdef HAVE_SSL_PROVIDERS |
| 8668 | hap_register_post_deinit(ssl_unload_providers); |
| 8669 | #endif |
Remi Tricot-Le Breton | 78a36e3 | 2022-02-11 12:04:45 +0100 | [diff] [blame] | 8670 | #if HA_OPENSSL_VERSION_NUMBER < 0x3000000fL |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8671 | /* Load SSL string for the verbose & debug mode. */ |
| 8672 | ERR_load_SSL_strings(); |
Remi Tricot-Le Breton | 78a36e3 | 2022-02-11 12:04:45 +0100 | [diff] [blame] | 8673 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 8674 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 8675 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 8676 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 8677 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 8678 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 8679 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 8680 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 8681 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 8682 | |
| 8683 | HA_SPIN_INIT(&ckch_lock); |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 8684 | |
Remi Tricot-Le Breton | 2b96364 | 2022-12-20 11:11:02 +0100 | [diff] [blame] | 8685 | HA_SPIN_INIT(&ocsp_tree_lock); |
| 8686 | |
Dragan Dosen | 9ac9809 | 2020-05-11 15:51:45 +0200 | [diff] [blame] | 8687 | /* Try to register dedicated SSL/TLS protocol message callbacks for |
| 8688 | * heartbleed attack (CVE-2014-0160) and clienthello. |
| 8689 | */ |
| 8690 | hap_register_post_check(ssl_sock_register_msg_callbacks); |
| 8691 | |
Dragan Dosen | 1e7ed04 | 2020-05-08 18:30:00 +0200 | [diff] [blame] | 8692 | /* Try to free all callbacks that were registered by using |
| 8693 | * ssl_sock_register_msg_callback(). |
| 8694 | */ |
| 8695 | hap_register_post_deinit(ssl_sock_unregister_msg_callbacks); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8696 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 8697 | INITCALL0(STG_REGISTER, __ssl_sock_init); |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 8698 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8699 | /* Compute and register the version string */ |
| 8700 | static void ssl_register_build_options() |
| 8701 | { |
| 8702 | char *ptr = NULL; |
| 8703 | int i; |
| 8704 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8705 | memprintf(&ptr, "Built with OpenSSL version : " |
| 8706 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8707 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8708 | #else /* OPENSSL_IS_BORINGSSL */ |
| 8709 | OPENSSL_VERSION_TEXT |
| 8710 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8711 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 8712 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8713 | #endif |
| 8714 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8715 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8716 | "no (library version too old)" |
| 8717 | #elif defined(OPENSSL_NO_TLSEXT) |
| 8718 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 8719 | #else |
| 8720 | "yes" |
| 8721 | #endif |
| 8722 | "", ptr); |
| 8723 | |
| 8724 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 8725 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8726 | "yes" |
| 8727 | #else |
| 8728 | #ifdef OPENSSL_NO_TLSEXT |
| 8729 | "no (because of OPENSSL_NO_TLSEXT)" |
| 8730 | #else |
| 8731 | "no (version might be too old, 0.9.8f min needed)" |
| 8732 | #endif |
| 8733 | #endif |
| 8734 | "", ptr); |
| 8735 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 8736 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 8737 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 8738 | if (methodVersions[i].option) |
| 8739 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 8740 | |
Remi Tricot-Le Breton | f87c67e | 2022-04-21 12:06:41 +0200 | [diff] [blame] | 8741 | #ifdef HAVE_SSL_PROVIDERS |
| 8742 | { |
| 8743 | struct list provider_names; |
| 8744 | struct provider_name *name; |
| 8745 | LIST_INIT(&provider_names); |
| 8746 | ssl_provider_get_name_list(&provider_names); |
| 8747 | |
| 8748 | memprintf(&ptr, "%s\nOpenSSL providers loaded :", ptr); |
| 8749 | |
| 8750 | list_for_each_entry(name, &provider_names, list) { |
| 8751 | memprintf(&ptr, "%s %s", ptr, name->name); |
| 8752 | } |
| 8753 | |
| 8754 | ssl_provider_clear_name_list(&provider_names); |
| 8755 | } |
| 8756 | #endif |
| 8757 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8758 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8759 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 8760 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8761 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 8762 | |
William Lallemand | d7bfbe2 | 2022-04-11 18:41:24 +0200 | [diff] [blame] | 8763 | #if defined(USE_ENGINE) && !defined(OPENSSL_NO_ENGINE) |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8764 | void ssl_free_engines(void) { |
| 8765 | struct ssl_engine_list *wl, *wlb; |
| 8766 | /* free up engine list */ |
| 8767 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 8768 | ENGINE_finish(wl->e); |
| 8769 | ENGINE_free(wl->e); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8770 | LIST_DELETE(&wl->list); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8771 | free(wl); |
| 8772 | } |
| 8773 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8774 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8775 | |
Remi Tricot-Le Breton | 1746a38 | 2022-05-16 16:24:33 +0200 | [diff] [blame] | 8776 | #ifdef HAVE_SSL_PROVIDERS |
| 8777 | void ssl_unload_providers(void) { |
| 8778 | struct ssl_provider_list *prov, *provb; |
| 8779 | list_for_each_entry_safe(prov, provb, &openssl_providers, list) { |
| 8780 | OSSL_PROVIDER_unload(prov->provider); |
| 8781 | LIST_DELETE(&prov->list); |
| 8782 | free(prov); |
| 8783 | } |
| 8784 | } |
| 8785 | #endif |
| 8786 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8787 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8788 | void ssl_free_dh(void) { |
| 8789 | if (local_dh_1024) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 8790 | HASSL_DH_free(local_dh_1024); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8791 | local_dh_1024 = NULL; |
| 8792 | } |
| 8793 | if (local_dh_2048) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 8794 | HASSL_DH_free(local_dh_2048); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8795 | local_dh_2048 = NULL; |
| 8796 | } |
| 8797 | if (local_dh_4096) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 8798 | HASSL_DH_free(local_dh_4096); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8799 | local_dh_4096 = NULL; |
| 8800 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 8801 | if (global_dh) { |
Remi Tricot-Le Breton | c76c3c4 | 2022-02-11 12:04:55 +0100 | [diff] [blame] | 8802 | HASSL_DH_free(global_dh); |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 8803 | global_dh = NULL; |
| 8804 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8805 | } |
| 8806 | #endif |
| 8807 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8808 | static void __ssl_sock_deinit(void) |
| 8809 | { |
| 8810 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 8811 | if (ssl_ctx_lru_tree) { |
| 8812 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 8813 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 8814 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8815 | #endif |
| 8816 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8817 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8818 | ERR_remove_state(0); |
| 8819 | ERR_free_strings(); |
| 8820 | |
| 8821 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 8822 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8823 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8824 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8825 | CRYPTO_cleanup_all_ex_data(); |
| 8826 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 8827 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8828 | } |
Willy Tarreau | 79367f9 | 2022-04-25 19:18:24 +0200 | [diff] [blame] | 8829 | REGISTER_POST_DEINIT(__ssl_sock_deinit); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 8830 | |
William Dauchy | f637044 | 2020-11-14 19:25:33 +0100 | [diff] [blame] | 8831 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 8832 | /* |
| 8833 | * Local variables: |
| 8834 | * c-indent-level: 8 |
| 8835 | * c-basic-offset: 8 |
| 8836 | * End: |
| 8837 | */ |