yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 1 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 2 | /* |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 3 | * SSL/TLS transport layer over SOCK_STREAM sockets |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2012 EXCELIANCE, Emeric Brun <ebrun@exceliance.fr> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * as published by the Free Software Foundation; either version |
| 10 | * 2 of the License, or (at your option) any later version. |
| 11 | * |
Willy Tarreau | 69845df | 2012-09-10 09:43:09 +0200 | [diff] [blame] | 12 | * Acknowledgement: |
| 13 | * We'd like to specially thank the Stud project authors for a very clean |
| 14 | * and well documented code which helped us understand how the OpenSSL API |
| 15 | * ought to be used in non-blocking mode. This is one difficult part which |
| 16 | * is not easy to get from the OpenSSL doc, and reading the Stud code made |
| 17 | * it much more obvious than the examples in the OpenSSL package. Keep up |
| 18 | * the good works, guys ! |
| 19 | * |
| 20 | * Stud is an extremely efficient and scalable SSL/TLS proxy which combines |
| 21 | * particularly well with haproxy. For more info about this project, visit : |
| 22 | * https://github.com/bumptech/stud |
| 23 | * |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 24 | */ |
| 25 | |
Willy Tarreau | 8d164dc | 2019-05-10 09:35:00 +0200 | [diff] [blame] | 26 | /* Note: do NOT include openssl/xxx.h here, do it in openssl-compat.h */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 27 | #define _GNU_SOURCE |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 28 | #include <ctype.h> |
| 29 | #include <dirent.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 30 | #include <errno.h> |
| 31 | #include <fcntl.h> |
| 32 | #include <stdio.h> |
| 33 | #include <stdlib.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 34 | #include <string.h> |
| 35 | #include <unistd.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 36 | |
| 37 | #include <sys/socket.h> |
| 38 | #include <sys/stat.h> |
| 39 | #include <sys/types.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 40 | #include <netdb.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 41 | #include <netinet/tcp.h> |
| 42 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 43 | #include <import/lru.h> |
| 44 | #include <import/xxhash.h> |
| 45 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 46 | #include <common/buffer.h> |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 47 | #include <common/chunk.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 48 | #include <common/compat.h> |
| 49 | #include <common/config.h> |
| 50 | #include <common/debug.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 51 | #include <common/errors.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 52 | #include <common/initcall.h> |
Willy Tarreau | 5599456 | 2019-05-09 14:52:44 +0200 | [diff] [blame] | 53 | #include <common/openssl-compat.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 54 | #include <common/standard.h> |
| 55 | #include <common/ticks.h> |
| 56 | #include <common/time.h> |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 57 | #include <common/cfgparse.h> |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 58 | #include <common/base64.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 59 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 60 | #include <ebsttree.h> |
| 61 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 62 | #include <types/applet.h> |
| 63 | #include <types/cli.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 64 | #include <types/global.h> |
| 65 | #include <types/ssl_sock.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 66 | #include <types/stats.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 67 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 68 | #include <proto/acl.h> |
| 69 | #include <proto/arg.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 70 | #include <proto/channel.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 71 | #include <proto/connection.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 72 | #include <proto/cli.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 73 | #include <proto/fd.h> |
| 74 | #include <proto/freq_ctr.h> |
| 75 | #include <proto/frontend.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 76 | #include <proto/http_rules.h> |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 77 | #include <proto/listener.h> |
Thierry FOURNIER | ed66c29 | 2013-11-28 11:05:19 +0100 | [diff] [blame] | 78 | #include <proto/pattern.h> |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 79 | #include <proto/proto_tcp.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 80 | #include <proto/http_ana.h> |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 81 | #include <proto/server.h> |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 82 | #include <proto/stream_interface.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 83 | #include <proto/log.h> |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 84 | #include <proto/proxy.h> |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 85 | #include <proto/shctx.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 86 | #include <proto/ssl_sock.h> |
Willy Tarreau | 9ad7bd4 | 2015-04-03 19:19:59 +0200 | [diff] [blame] | 87 | #include <proto/stream.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 88 | #include <proto/task.h> |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 89 | #include <proto/vars.h> |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 90 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 91 | /* ***** READ THIS before adding code here! ***** |
| 92 | * |
| 93 | * Due to API incompatibilities between multiple OpenSSL versions and their |
| 94 | * derivatives, it's often tempting to add macros to (re-)define certain |
| 95 | * symbols. Please do not do this here, and do it in common/openssl-compat.h |
| 96 | * exclusively so that the whole code consistently uses the same macros. |
| 97 | * |
| 98 | * Whenever possible if a macro is missing in certain versions, it's better |
| 99 | * to conditionally define it in openssl-compat.h than using lots of ifdefs. |
| 100 | */ |
| 101 | |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 102 | /* Warning, these are bits, not integers! */ |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 103 | #define SSL_SOCK_ST_FL_VERIFY_DONE 0x00000001 |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 104 | #define SSL_SOCK_ST_FL_16K_WBFSIZE 0x00000002 |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 105 | #define SSL_SOCK_SEND_UNLIMITED 0x00000004 |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 106 | #define SSL_SOCK_RECV_HEARTBEAT 0x00000008 |
| 107 | |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 108 | /* bits 0xFFFF0000 are reserved to store verify errors */ |
| 109 | |
| 110 | /* Verify errors macros */ |
| 111 | #define SSL_SOCK_CA_ERROR_TO_ST(e) (((e > 63) ? 63 : e) << (16)) |
| 112 | #define SSL_SOCK_CAEDEPTH_TO_ST(d) (((d > 15) ? 15 : d) << (6+16)) |
| 113 | #define SSL_SOCK_CRTERROR_TO_ST(e) (((e > 63) ? 63 : e) << (4+6+16)) |
| 114 | |
| 115 | #define SSL_SOCK_ST_TO_CA_ERROR(s) ((s >> (16)) & 63) |
| 116 | #define SSL_SOCK_ST_TO_CAEDEPTH(s) ((s >> (6+16)) & 15) |
| 117 | #define SSL_SOCK_ST_TO_CRTERROR(s) ((s >> (4+6+16)) & 63) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 118 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 119 | /* ssl_methods flags for ssl options */ |
| 120 | #define MC_SSL_O_ALL 0x0000 |
| 121 | #define MC_SSL_O_NO_SSLV3 0x0001 /* disable SSLv3 */ |
| 122 | #define MC_SSL_O_NO_TLSV10 0x0002 /* disable TLSv10 */ |
| 123 | #define MC_SSL_O_NO_TLSV11 0x0004 /* disable TLSv11 */ |
| 124 | #define MC_SSL_O_NO_TLSV12 0x0008 /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 125 | #define MC_SSL_O_NO_TLSV13 0x0010 /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 126 | |
| 127 | /* ssl_methods versions */ |
| 128 | enum { |
| 129 | CONF_TLSV_NONE = 0, |
| 130 | CONF_TLSV_MIN = 1, |
| 131 | CONF_SSLV3 = 1, |
| 132 | CONF_TLSV10 = 2, |
| 133 | CONF_TLSV11 = 3, |
| 134 | CONF_TLSV12 = 4, |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 135 | CONF_TLSV13 = 5, |
| 136 | CONF_TLSV_MAX = 5, |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 137 | }; |
| 138 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 139 | /* server and bind verify method, it uses a global value as default */ |
| 140 | enum { |
| 141 | SSL_SOCK_VERIFY_DEFAULT = 0, |
| 142 | SSL_SOCK_VERIFY_REQUIRED = 1, |
| 143 | SSL_SOCK_VERIFY_OPTIONAL = 2, |
| 144 | SSL_SOCK_VERIFY_NONE = 3, |
| 145 | }; |
| 146 | |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 147 | |
Willy Tarreau | 71b734c | 2014-01-28 15:19:44 +0100 | [diff] [blame] | 148 | int sslconns = 0; |
| 149 | int totalsslconns = 0; |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 150 | static struct xprt_ops ssl_sock; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 151 | int nb_engines = 0; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 152 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 153 | static struct { |
| 154 | char *crt_base; /* base directory path for certificates */ |
| 155 | char *ca_base; /* base directory path for CAs and CRLs */ |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 156 | int async; /* whether we use ssl async mode */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 157 | |
| 158 | char *listen_default_ciphers; |
| 159 | char *connect_default_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 160 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 161 | char *listen_default_ciphersuites; |
| 162 | char *connect_default_ciphersuites; |
| 163 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 164 | int listen_default_ssloptions; |
| 165 | int connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 166 | struct tls_version_filter listen_default_sslmethods; |
| 167 | struct tls_version_filter connect_default_sslmethods; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 168 | |
| 169 | int private_cache; /* Force to use a private session cache even if nbproc > 1 */ |
| 170 | unsigned int life_time; /* SSL session lifetime in seconds */ |
| 171 | unsigned int max_record; /* SSL max record size */ |
| 172 | unsigned int default_dh_param; /* SSL maximum DH parameter size */ |
| 173 | int ctx_cache; /* max number of entries in the ssl_ctx cache. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 174 | int capture_cipherlist; /* Size of the cipherlist buffer. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 175 | } global_ssl = { |
| 176 | #ifdef LISTEN_DEFAULT_CIPHERS |
| 177 | .listen_default_ciphers = LISTEN_DEFAULT_CIPHERS, |
| 178 | #endif |
| 179 | #ifdef CONNECT_DEFAULT_CIPHERS |
| 180 | .connect_default_ciphers = CONNECT_DEFAULT_CIPHERS, |
| 181 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 182 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 183 | #ifdef LISTEN_DEFAULT_CIPHERSUITES |
| 184 | .listen_default_ciphersuites = LISTEN_DEFAULT_CIPHERSUITES, |
| 185 | #endif |
| 186 | #ifdef CONNECT_DEFAULT_CIPHERSUITES |
| 187 | .connect_default_ciphersuites = CONNECT_DEFAULT_CIPHERSUITES, |
| 188 | #endif |
| 189 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 190 | .listen_default_ssloptions = BC_SSL_O_NONE, |
| 191 | .connect_default_ssloptions = SRV_SSL_O_NONE, |
| 192 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 193 | .listen_default_sslmethods.flags = MC_SSL_O_ALL, |
| 194 | .listen_default_sslmethods.min = CONF_TLSV_NONE, |
| 195 | .listen_default_sslmethods.max = CONF_TLSV_NONE, |
| 196 | .connect_default_sslmethods.flags = MC_SSL_O_ALL, |
| 197 | .connect_default_sslmethods.min = CONF_TLSV_NONE, |
| 198 | .connect_default_sslmethods.max = CONF_TLSV_NONE, |
| 199 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 200 | #ifdef DEFAULT_SSL_MAX_RECORD |
| 201 | .max_record = DEFAULT_SSL_MAX_RECORD, |
| 202 | #endif |
| 203 | .default_dh_param = SSL_DEFAULT_DH_PARAM, |
| 204 | .ctx_cache = DEFAULT_SSL_CTX_CACHE, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 205 | .capture_cipherlist = 0, |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 206 | }; |
| 207 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 208 | static BIO_METHOD *ha_meth; |
| 209 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 210 | struct ssl_sock_ctx { |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 211 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 212 | SSL *ssl; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 213 | BIO *bio; |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 214 | const struct xprt_ops *xprt; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 215 | void *xprt_ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 216 | struct wait_event wait_event; |
| 217 | struct wait_event *recv_wait; |
| 218 | struct wait_event *send_wait; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 219 | int xprt_st; /* transport layer state, initialized to zero */ |
| 220 | int tmp_early_data; /* 1st byte of early data, if any */ |
| 221 | int sent_early_data; /* Amount of early data we sent so far */ |
| 222 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 223 | }; |
| 224 | |
| 225 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 226 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 227 | static struct task *ssl_sock_io_cb(struct task *, void *, unsigned short); |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 228 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 229 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 230 | /* Methods to implement OpenSSL BIO */ |
| 231 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 232 | { |
| 233 | struct buffer tmpbuf; |
| 234 | struct ssl_sock_ctx *ctx; |
| 235 | int ret; |
| 236 | |
| 237 | ctx = BIO_get_data(h); |
| 238 | tmpbuf.size = num; |
| 239 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 240 | tmpbuf.data = num; |
| 241 | tmpbuf.head = 0; |
| 242 | 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] | 243 | 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] | 244 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 245 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 246 | } else if (ret == 0) |
| 247 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 252 | { |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int ha_ssl_puts(BIO *h, const char *str) |
| 258 | { |
| 259 | |
| 260 | return ha_ssl_write(h, str, strlen(str)); |
| 261 | } |
| 262 | |
| 263 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 264 | { |
| 265 | struct buffer tmpbuf; |
| 266 | struct ssl_sock_ctx *ctx; |
| 267 | int ret; |
| 268 | |
| 269 | ctx = BIO_get_data(h); |
| 270 | tmpbuf.size = size; |
| 271 | tmpbuf.area = buf; |
| 272 | tmpbuf.data = 0; |
| 273 | tmpbuf.head = 0; |
| 274 | 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] | 275 | 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] | 276 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 277 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 278 | } else if (ret == 0) |
| 279 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 280 | |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 285 | { |
| 286 | int ret = 0; |
| 287 | switch (cmd) { |
| 288 | case BIO_CTRL_DUP: |
| 289 | case BIO_CTRL_FLUSH: |
| 290 | ret = 1; |
| 291 | break; |
| 292 | } |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static int ha_ssl_new(BIO *h) |
| 297 | { |
| 298 | BIO_set_init(h, 1); |
| 299 | BIO_set_data(h, NULL); |
| 300 | BIO_clear_flags(h, ~0); |
| 301 | return 1; |
| 302 | } |
| 303 | |
| 304 | static int ha_ssl_free(BIO *data) |
| 305 | { |
| 306 | |
| 307 | return 1; |
| 308 | } |
| 309 | |
| 310 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 311 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 312 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 313 | static HA_RWLOCK_T *ssl_rwlocks; |
| 314 | |
| 315 | |
| 316 | unsigned long ssl_id_function(void) |
| 317 | { |
| 318 | return (unsigned long)tid; |
| 319 | } |
| 320 | |
| 321 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 322 | { |
| 323 | if (mode & CRYPTO_LOCK) { |
| 324 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 325 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 326 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 327 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 328 | } |
| 329 | else { |
| 330 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 331 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 332 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 333 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 334 | } |
| 335 | } |
| 336 | |
| 337 | static int ssl_locking_init(void) |
| 338 | { |
| 339 | int i; |
| 340 | |
| 341 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 342 | if (!ssl_rwlocks) |
| 343 | return -1; |
| 344 | |
| 345 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 346 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 347 | |
| 348 | CRYPTO_set_id_callback(ssl_id_function); |
| 349 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 350 | |
| 351 | return 0; |
| 352 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 353 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 354 | #endif |
| 355 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 356 | __decl_hathreads(HA_SPINLOCK_T ckch_lock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 357 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 358 | /* Uncommitted CKCH transaction */ |
| 359 | |
| 360 | static struct { |
| 361 | struct ckch_store *new_ckchs; |
| 362 | struct ckch_store *old_ckchs; |
| 363 | char *path; |
| 364 | } ckchs_transaction; |
| 365 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 366 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 367 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 368 | unsigned long long int xxh64; |
| 369 | unsigned char ciphersuite_len; |
| 370 | char ciphersuite[0]; |
| 371 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 372 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 373 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 374 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 375 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 376 | static int ssl_pkey_info_index = -1; |
| 377 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 378 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 379 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 380 | #endif |
| 381 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 382 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 383 | static unsigned int openssl_engines_initialized; |
| 384 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 385 | struct ssl_engine_list { |
| 386 | struct list list; |
| 387 | ENGINE *e; |
| 388 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 389 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 390 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 391 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 392 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 393 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 394 | static DH *local_dh_1024 = NULL; |
| 395 | static DH *local_dh_2048 = NULL; |
| 396 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 397 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 398 | #endif /* OPENSSL_NO_DH */ |
| 399 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 400 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 401 | /* X509V3 Extensions that will be added on generated certificates */ |
| 402 | #define X509V3_EXT_SIZE 5 |
| 403 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 404 | "basicConstraints", |
| 405 | "nsComment", |
| 406 | "subjectKeyIdentifier", |
| 407 | "authorityKeyIdentifier", |
| 408 | "keyUsage", |
| 409 | }; |
| 410 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 411 | "CA:FALSE", |
| 412 | "\"OpenSSL Generated Certificate\"", |
| 413 | "hash", |
| 414 | "keyid,issuer:always", |
| 415 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 416 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 417 | /* LRU cache to store generated certificate */ |
| 418 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 419 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 420 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 421 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 422 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 423 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 424 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 425 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 426 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 427 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 428 | /* The order here matters for picking a default context, |
| 429 | * keep the most common keytype at the bottom of the list |
| 430 | */ |
| 431 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 432 | "dsa", |
| 433 | "ecdsa", |
| 434 | "rsa" |
| 435 | }; |
| 436 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 437 | #else |
| 438 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 439 | #endif |
| 440 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 441 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 442 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 443 | |
| 444 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 445 | |
| 446 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 447 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 448 | |
| 449 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 450 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 451 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 452 | /* |
| 453 | * This function gives the detail of the SSL error. It is used only |
| 454 | * if the debug mode and the verbose mode are activated. It dump all |
| 455 | * the SSL error until the stack was empty. |
| 456 | */ |
| 457 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 458 | { |
| 459 | unsigned long ret; |
| 460 | |
| 461 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 462 | while(1) { |
| 463 | ret = ERR_get_error(); |
| 464 | if (ret == 0) |
| 465 | return; |
| 466 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 467 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 468 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 473 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 474 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 475 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 476 | { |
| 477 | int err_code = ERR_ABORT; |
| 478 | ENGINE *engine; |
| 479 | struct ssl_engine_list *el; |
| 480 | |
| 481 | /* grab the structural reference to the engine */ |
| 482 | engine = ENGINE_by_id(engine_id); |
| 483 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 484 | 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] | 485 | goto fail_get; |
| 486 | } |
| 487 | |
| 488 | if (!ENGINE_init(engine)) { |
| 489 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 490 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 491 | goto fail_init; |
| 492 | } |
| 493 | |
| 494 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 495 | 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] | 496 | goto fail_set_method; |
| 497 | } |
| 498 | |
| 499 | el = calloc(1, sizeof(*el)); |
| 500 | el->e = engine; |
| 501 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 502 | nb_engines++; |
| 503 | if (global_ssl.async) |
| 504 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 505 | return 0; |
| 506 | |
| 507 | fail_set_method: |
| 508 | /* release the functional reference from ENGINE_init() */ |
| 509 | ENGINE_finish(engine); |
| 510 | |
| 511 | fail_init: |
| 512 | /* release the structural reference from ENGINE_by_id() */ |
| 513 | ENGINE_free(engine); |
| 514 | |
| 515 | fail_get: |
| 516 | return err_code; |
| 517 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 518 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 519 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 520 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 521 | /* |
| 522 | * openssl async fd handler |
| 523 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 524 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 525 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 526 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 527 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 528 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 529 | * to poll this fd until it is requested |
| 530 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 531 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 532 | fd_cant_recv(fd); |
| 533 | |
| 534 | /* crypto engine is available, let's notify the associated |
| 535 | * connection that it can pursue its processing. |
| 536 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 537 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 540 | /* |
| 541 | * openssl async delayed SSL_free handler |
| 542 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 543 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 544 | { |
| 545 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 546 | OSSL_ASYNC_FD all_fd[32]; |
| 547 | size_t num_all_fds = 0; |
| 548 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 549 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 550 | /* We suppose that the async job for a same SSL * |
| 551 | * are serialized. So if we are awake it is |
| 552 | * because the running job has just finished |
| 553 | * and we can remove all async fds safely |
| 554 | */ |
| 555 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 556 | if (num_all_fds > 32) { |
| 557 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 558 | return; |
| 559 | } |
| 560 | |
| 561 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 562 | for (i=0 ; i < num_all_fds ; i++) |
| 563 | fd_remove(all_fd[i]); |
| 564 | |
| 565 | /* 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] | 566 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 567 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 568 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 569 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 570 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 571 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 572 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 573 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 574 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 575 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 576 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 577 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 578 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 579 | size_t num_add_fds = 0; |
| 580 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 581 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 582 | |
| 583 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 584 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 585 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 586 | 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] | 587 | return; |
| 588 | } |
| 589 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 590 | 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] | 591 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 592 | /* We remove unused fds from the fdtab */ |
| 593 | for (i=0 ; i < num_del_fds ; i++) |
| 594 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 595 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 596 | /* We add new fds to the fdtab */ |
| 597 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 598 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 601 | num_add_fds = 0; |
| 602 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 603 | if (num_add_fds > 32) { |
| 604 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 605 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 606 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 607 | |
| 608 | /* We activate the polling for all known async fds */ |
| 609 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 610 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 611 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 612 | /* To ensure that the fd cache won't be used |
| 613 | * We'll prefer to catch a real RD event |
| 614 | * because handling an EAGAIN on this fd will |
| 615 | * result in a context switch and also |
| 616 | * some engines uses a fd in blocking mode. |
| 617 | */ |
| 618 | fd_cant_recv(add_fd[i]); |
| 619 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 620 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 621 | } |
| 622 | #endif |
| 623 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 624 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 625 | /* |
| 626 | * This function returns the number of seconds elapsed |
| 627 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 628 | * date presented un ASN1_GENERALIZEDTIME. |
| 629 | * |
| 630 | * In parsing error case, it returns -1. |
| 631 | */ |
| 632 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 633 | { |
| 634 | long epoch; |
| 635 | char *p, *end; |
| 636 | const unsigned short month_offset[12] = { |
| 637 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 638 | }; |
| 639 | int year, month; |
| 640 | |
| 641 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 642 | |
| 643 | p = (char *)d->data; |
| 644 | end = p + d->length; |
| 645 | |
| 646 | if (end - p < 4) return -1; |
| 647 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 648 | p += 4; |
| 649 | if (end - p < 2) return -1; |
| 650 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 651 | if (month < 1 || month > 12) return -1; |
| 652 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 653 | We consider leap years and the current month (<marsh or not) */ |
| 654 | epoch = ( ((year - 1970) * 365) |
| 655 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 656 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 657 | + month_offset[month-1] |
| 658 | ) * 24 * 60 * 60; |
| 659 | p += 2; |
| 660 | if (end - p < 2) return -1; |
| 661 | /* Add the number of seconds of completed days of current month */ |
| 662 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 663 | p += 2; |
| 664 | if (end - p < 2) return -1; |
| 665 | /* Add the completed hours of the current day */ |
| 666 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 667 | p += 2; |
| 668 | if (end - p < 2) return -1; |
| 669 | /* Add the completed minutes of the current hour */ |
| 670 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 671 | p += 2; |
| 672 | if (p == end) return -1; |
| 673 | /* Test if there is available seconds */ |
| 674 | if (p[0] < '0' || p[0] > '9') |
| 675 | goto nosec; |
| 676 | if (end - p < 2) return -1; |
| 677 | /* Add the seconds of the current minute */ |
| 678 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 679 | p += 2; |
| 680 | if (p == end) return -1; |
| 681 | /* Ignore seconds float part if present */ |
| 682 | if (p[0] == '.') { |
| 683 | do { |
| 684 | if (++p == end) return -1; |
| 685 | } while (p[0] >= '0' && p[0] <= '9'); |
| 686 | } |
| 687 | |
| 688 | nosec: |
| 689 | if (p[0] == 'Z') { |
| 690 | if (end - p != 1) return -1; |
| 691 | return epoch; |
| 692 | } |
| 693 | else if (p[0] == '+') { |
| 694 | if (end - p != 5) return -1; |
| 695 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 696 | 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] | 697 | } |
| 698 | else if (p[0] == '-') { |
| 699 | if (end - p != 5) return -1; |
| 700 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 701 | 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] | 702 | } |
| 703 | |
| 704 | return -1; |
| 705 | } |
| 706 | |
William Lallemand | 104a7a6 | 2019-10-14 14:14:59 +0200 | [diff] [blame] | 707 | /* |
| 708 | * struct alignment works here such that the key.key is the same as key_data |
| 709 | * Do not change the placement of key_data |
| 710 | */ |
| 711 | struct certificate_ocsp { |
| 712 | struct ebmb_node key; |
| 713 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 714 | struct buffer response; |
| 715 | long expire; |
| 716 | }; |
| 717 | |
| 718 | struct ocsp_cbk_arg { |
| 719 | int is_single; |
| 720 | int single_kt; |
| 721 | union { |
| 722 | struct certificate_ocsp *s_ocsp; |
| 723 | /* |
| 724 | * m_ocsp will have multiple entries dependent on key type |
| 725 | * Entry 0 - DSA |
| 726 | * Entry 1 - ECDSA |
| 727 | * Entry 2 - RSA |
| 728 | */ |
| 729 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 730 | }; |
| 731 | }; |
| 732 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 733 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 734 | |
| 735 | /* This function starts to check if the OCSP response (in DER format) contained |
| 736 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 737 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 738 | * contained in the OCSP Response and exits on error if no match. |
| 739 | * If it's a valid OCSP Response: |
| 740 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 741 | * pointed by 'ocsp'. |
| 742 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 743 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 744 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 745 | * already present in the container, it will be overwritten. |
| 746 | * |
| 747 | * Note: OCSP response containing more than one OCSP Single response is not |
| 748 | * considered valid. |
| 749 | * |
| 750 | * Returns 0 on success, 1 in error case. |
| 751 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 752 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 753 | struct certificate_ocsp *ocsp, |
| 754 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 755 | { |
| 756 | OCSP_RESPONSE *resp; |
| 757 | OCSP_BASICRESP *bs = NULL; |
| 758 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 759 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 760 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 761 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 762 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 763 | int reason; |
| 764 | int ret = 1; |
| 765 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 766 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 767 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 768 | if (!resp) { |
| 769 | memprintf(err, "Unable to parse OCSP response"); |
| 770 | goto out; |
| 771 | } |
| 772 | |
| 773 | rc = OCSP_response_status(resp); |
| 774 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 775 | memprintf(err, "OCSP response status not successful"); |
| 776 | goto out; |
| 777 | } |
| 778 | |
| 779 | bs = OCSP_response_get1_basic(resp); |
| 780 | if (!bs) { |
| 781 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 782 | goto out; |
| 783 | } |
| 784 | |
| 785 | count_sr = OCSP_resp_count(bs); |
| 786 | if (count_sr > 1) { |
| 787 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 788 | goto out; |
| 789 | } |
| 790 | |
| 791 | sr = OCSP_resp_get0(bs, 0); |
| 792 | if (!sr) { |
| 793 | memprintf(err, "Failed to get OCSP single response"); |
| 794 | goto out; |
| 795 | } |
| 796 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 797 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 798 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 799 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 800 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 801 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 802 | goto out; |
| 803 | } |
| 804 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 805 | if (!nextupd) { |
| 806 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 807 | goto out; |
| 808 | } |
| 809 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 810 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 811 | if (!rc) { |
| 812 | memprintf(err, "OCSP single response: no longer valid."); |
| 813 | goto out; |
| 814 | } |
| 815 | |
| 816 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 817 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 818 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 819 | goto out; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | if (!ocsp) { |
| 824 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 825 | unsigned char *p; |
| 826 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 827 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 828 | if (!rc) { |
| 829 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 830 | goto out; |
| 831 | } |
| 832 | |
| 833 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 834 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 835 | goto out; |
| 836 | } |
| 837 | |
| 838 | p = key; |
| 839 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 840 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 841 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 842 | if (!ocsp) { |
| 843 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 844 | goto out; |
| 845 | } |
| 846 | } |
| 847 | |
| 848 | /* According to comments on "chunk_dup", the |
| 849 | previous chunk buffer will be freed */ |
| 850 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 851 | memprintf(err, "OCSP response: Memory allocation error"); |
| 852 | goto out; |
| 853 | } |
| 854 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 855 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 856 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 857 | ret = 0; |
| 858 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 859 | ERR_clear_error(); |
| 860 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 861 | if (bs) |
| 862 | OCSP_BASICRESP_free(bs); |
| 863 | |
| 864 | if (resp) |
| 865 | OCSP_RESPONSE_free(resp); |
| 866 | |
| 867 | return ret; |
| 868 | } |
| 869 | /* |
| 870 | * External function use to update the OCSP response in the OCSP response's |
| 871 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 872 | * to update in DER format. |
| 873 | * |
| 874 | * Returns 0 on success, 1 in error case. |
| 875 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 876 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 877 | { |
| 878 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 879 | } |
| 880 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 881 | #endif |
| 882 | |
| 883 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 884 | /* |
| 885 | * This function load the OCSP Resonse in DER format contained in file at |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 886 | * path 'ocsp_path' or base64 in a buffer <buf> |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 887 | * |
| 888 | * Returns 0 on success, 1 in error case. |
| 889 | */ |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 890 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 891 | { |
| 892 | int fd = -1; |
| 893 | int r = 0; |
| 894 | int ret = 1; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 895 | struct buffer *ocsp_response; |
| 896 | struct buffer *src = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 897 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 898 | if (buf) { |
| 899 | int i, j; |
| 900 | /* if it's from a buffer it will be base64 */ |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 901 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 902 | /* remove \r and \n from the payload */ |
| 903 | for (i = 0, j = 0; buf[i]; i++) { |
| 904 | if (buf[i] == '\r' || buf[i] == '\n') |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 905 | continue; |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 906 | buf[j++] = buf[i]; |
| 907 | } |
| 908 | buf[j] = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 909 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 910 | ret = base64dec(buf, j, trash.area, trash.size); |
| 911 | if (ret < 0) { |
| 912 | memprintf(err, "Error reading OCSP response in base64 format"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 913 | goto end; |
| 914 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 915 | trash.data = ret; |
| 916 | src = &trash; |
| 917 | } else { |
| 918 | fd = open(ocsp_path, O_RDONLY); |
| 919 | if (fd == -1) { |
| 920 | memprintf(err, "Error opening OCSP response file"); |
| 921 | goto end; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 922 | } |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 923 | |
| 924 | trash.data = 0; |
| 925 | while (trash.data < trash.size) { |
| 926 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 927 | if (r < 0) { |
| 928 | if (errno == EINTR) |
| 929 | continue; |
| 930 | |
| 931 | memprintf(err, "Error reading OCSP response from file"); |
| 932 | goto end; |
| 933 | } |
| 934 | else if (r == 0) { |
| 935 | break; |
| 936 | } |
| 937 | trash.data += r; |
| 938 | } |
| 939 | close(fd); |
| 940 | fd = -1; |
| 941 | src = &trash; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 942 | } |
| 943 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 944 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 945 | if (!chunk_dup(ocsp_response, src)) { |
| 946 | free(ocsp_response); |
| 947 | ocsp_response = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 948 | goto end; |
| 949 | } |
| 950 | |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 951 | ckch->ocsp_response = ocsp_response; |
William Lallemand | e0f48ae | 2019-10-15 13:44:57 +0200 | [diff] [blame] | 952 | ret = 0; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 953 | end: |
| 954 | if (fd != -1) |
| 955 | close(fd); |
| 956 | |
| 957 | return ret; |
| 958 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 959 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 960 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 961 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 962 | static int ssl_tlsext_ticket_key_cb(SSL *s, unsigned char key_name[16], unsigned char *iv, EVP_CIPHER_CTX *ectx, HMAC_CTX *hctx, int enc) |
| 963 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 964 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 965 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 966 | struct connection *conn; |
| 967 | int head; |
| 968 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 969 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 970 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 971 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 972 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 973 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 974 | |
| 975 | keys = ref->tlskeys; |
| 976 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 977 | |
| 978 | if (enc) { |
| 979 | memcpy(key_name, keys[head].name, 16); |
| 980 | |
| 981 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 982 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 983 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 984 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 985 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 986 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 987 | goto end; |
| 988 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 989 | HMAC_Init_ex(hctx, keys[head].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 990 | ret = 1; |
| 991 | } |
| 992 | else if (ref->key_size_bits == 256 ) { |
| 993 | |
| 994 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 995 | goto end; |
| 996 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 997 | HMAC_Init_ex(hctx, keys[head].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 998 | ret = 1; |
| 999 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1000 | } else { |
| 1001 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 1002 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 1003 | goto found; |
| 1004 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1005 | ret = 0; |
| 1006 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1007 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1008 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1009 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1010 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_128.hmac_key, 16, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1011 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 1012 | goto end; |
| 1013 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1014 | ret = i ? 2 : 1; |
| 1015 | } |
| 1016 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 1017 | HMAC_Init_ex(hctx, keys[(head + i) % TLS_TICKETS_NO].key_256.hmac_key, 32, TLS_TICKET_HASH_FUNCT(), NULL); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1018 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 1019 | goto end; |
| 1020 | /* 2 for key renewal, 1 if current key is still valid */ |
| 1021 | ret = i ? 2 : 1; |
| 1022 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1023 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1024 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1025 | end: |
| 1026 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 1027 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1028 | } |
| 1029 | |
| 1030 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1031 | { |
| 1032 | struct tls_keys_ref *ref; |
| 1033 | |
| 1034 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1035 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1036 | return ref; |
| 1037 | return NULL; |
| 1038 | } |
| 1039 | |
| 1040 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1041 | { |
| 1042 | struct tls_keys_ref *ref; |
| 1043 | |
| 1044 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1045 | if (ref->unique_id == unique_id) |
| 1046 | return ref; |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1050 | /* Update the key into ref: if keysize doesnt |
| 1051 | * match existing ones, this function returns -1 |
| 1052 | * else it returns 0 on success. |
| 1053 | */ |
| 1054 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1055 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1056 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1057 | if (ref->key_size_bits == 128) { |
| 1058 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1059 | return -1; |
| 1060 | } |
| 1061 | else if (ref->key_size_bits == 256) { |
| 1062 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1063 | return -1; |
| 1064 | } |
| 1065 | else |
| 1066 | return -1; |
| 1067 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1068 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1069 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1070 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1071 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1072 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1073 | |
| 1074 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1077 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1078 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1079 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1080 | |
| 1081 | if(!ref) { |
| 1082 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1083 | return 1; |
| 1084 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1085 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1086 | memprintf(err, "Invalid key size"); |
| 1087 | return 1; |
| 1088 | } |
| 1089 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1090 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1091 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1092 | |
| 1093 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1094 | * automatic ids. It's called just after the basic checks. It returns |
| 1095 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1096 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1097 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1098 | { |
| 1099 | int i = 0; |
| 1100 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1101 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1102 | |
| 1103 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1104 | if (ref->unique_id == -1) { |
| 1105 | /* Look for the first free id. */ |
| 1106 | while (1) { |
| 1107 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1108 | if (ref2->unique_id == i) { |
| 1109 | i++; |
| 1110 | break; |
| 1111 | } |
| 1112 | } |
| 1113 | if (&ref2->list == &tlskeys_reference) |
| 1114 | break; |
| 1115 | } |
| 1116 | |
| 1117 | /* Uses the unique id and increment it for the next entry. */ |
| 1118 | ref->unique_id = i; |
| 1119 | i++; |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | /* This sort the reference list by id. */ |
| 1124 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1125 | LIST_DEL(&ref->list); |
| 1126 | list_for_each_entry(ref3, &tkr, list) { |
| 1127 | if (ref->unique_id < ref3->unique_id) { |
| 1128 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1129 | break; |
| 1130 | } |
| 1131 | } |
| 1132 | if (&ref3->list == &tkr) |
| 1133 | LIST_ADDQ(&tkr, &ref->list); |
| 1134 | } |
| 1135 | |
| 1136 | /* swap root */ |
| 1137 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1138 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1139 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1140 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1141 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1142 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1143 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1144 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1145 | { |
| 1146 | switch (evp_keytype) { |
| 1147 | case EVP_PKEY_RSA: |
| 1148 | return 2; |
| 1149 | case EVP_PKEY_DSA: |
| 1150 | return 0; |
| 1151 | case EVP_PKEY_EC: |
| 1152 | return 1; |
| 1153 | } |
| 1154 | |
| 1155 | return -1; |
| 1156 | } |
| 1157 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1158 | /* |
| 1159 | * Callback used to set OCSP status extension content in server hello. |
| 1160 | */ |
| 1161 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1162 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1163 | struct certificate_ocsp *ocsp; |
| 1164 | struct ocsp_cbk_arg *ocsp_arg; |
| 1165 | char *ssl_buf; |
| 1166 | EVP_PKEY *ssl_pkey; |
| 1167 | int key_type; |
| 1168 | int index; |
| 1169 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1170 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1171 | |
| 1172 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1173 | if (!ssl_pkey) |
| 1174 | return SSL_TLSEXT_ERR_NOACK; |
| 1175 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1176 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1177 | |
| 1178 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1179 | ocsp = ocsp_arg->s_ocsp; |
| 1180 | else { |
| 1181 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1182 | * the certificate type |
| 1183 | */ |
| 1184 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1185 | |
| 1186 | if (index < 0) |
| 1187 | return SSL_TLSEXT_ERR_NOACK; |
| 1188 | |
| 1189 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1190 | |
| 1191 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1192 | |
| 1193 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1194 | !ocsp->response.area || |
| 1195 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1196 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1197 | return SSL_TLSEXT_ERR_NOACK; |
| 1198 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1199 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1200 | if (!ssl_buf) |
| 1201 | return SSL_TLSEXT_ERR_NOACK; |
| 1202 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1203 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1204 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1205 | |
| 1206 | return SSL_TLSEXT_ERR_OK; |
| 1207 | } |
| 1208 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1209 | #endif |
| 1210 | |
| 1211 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1212 | /* |
| 1213 | * 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] | 1214 | * ocsp_response buffer was found in the cert_key_and_chain. To enable OCSP |
| 1215 | * status extension, the issuer's certificate is mandatory. It should be |
| 1216 | * present in ckch->ocsp_issuer. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1217 | * |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1218 | * In addition, the ckch->ocsp_reponse buffer is loaded as a DER format of an |
| 1219 | * OCSP response. If file is empty or content is not a valid OCSP response, |
| 1220 | * OCSP status extension is enabled but OCSP response is ignored (a warning is |
| 1221 | * displayed). |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1222 | * |
| 1223 | * 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] | 1224 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1225 | */ |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1226 | #ifndef OPENSSL_IS_BORINGSSL |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1227 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1228 | { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1229 | X509 *x = NULL, *issuer = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1230 | OCSP_CERTID *cid = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1231 | int i, ret = -1; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1232 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1233 | char *warn = NULL; |
| 1234 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1235 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1236 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1237 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1238 | x = ckch->cert; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1239 | if (!x) |
| 1240 | goto out; |
| 1241 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1242 | issuer = ckch->ocsp_issuer; |
| 1243 | if (!issuer) |
| 1244 | goto out; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1245 | |
| 1246 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1247 | if (!cid) |
| 1248 | goto out; |
| 1249 | |
| 1250 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1251 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1252 | goto out; |
| 1253 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1254 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1255 | if (!ocsp) |
| 1256 | goto out; |
| 1257 | |
| 1258 | p = ocsp->key_data; |
| 1259 | i2d_OCSP_CERTID(cid, &p); |
| 1260 | |
| 1261 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1262 | if (iocsp == ocsp) |
| 1263 | ocsp = NULL; |
| 1264 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1265 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1266 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1267 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1268 | #endif |
| 1269 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1270 | |
| 1271 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1272 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1273 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1274 | |
| 1275 | cb_arg->is_single = 1; |
| 1276 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1277 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1278 | pkey = X509_get_pubkey(x); |
| 1279 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1280 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1281 | |
| 1282 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1283 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1284 | } else { |
| 1285 | /* |
| 1286 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1287 | * Update that cb_arg with the new cert's staple |
| 1288 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1289 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1290 | struct certificate_ocsp *tmp_ocsp; |
| 1291 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1292 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1293 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1294 | |
| 1295 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1296 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1297 | #else |
| 1298 | cb_arg = ctx->tlsext_status_arg; |
| 1299 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1300 | |
| 1301 | /* |
| 1302 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1303 | * the order of operations below matter, take care when changing it |
| 1304 | */ |
| 1305 | tmp_ocsp = cb_arg->s_ocsp; |
| 1306 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1307 | cb_arg->s_ocsp = NULL; |
| 1308 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1309 | cb_arg->is_single = 0; |
| 1310 | cb_arg->single_kt = 0; |
| 1311 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1312 | pkey = X509_get_pubkey(x); |
| 1313 | key_type = EVP_PKEY_base_id(pkey); |
| 1314 | EVP_PKEY_free(pkey); |
| 1315 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1316 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1317 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1318 | cb_arg->m_ocsp[index] = iocsp; |
| 1319 | |
| 1320 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1321 | |
| 1322 | ret = 0; |
| 1323 | |
| 1324 | warn = NULL; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 1325 | if (ssl_sock_load_ocsp_response(ckch->ocsp_response, ocsp, cid, &warn)) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 1326 | memprintf(&warn, "Loading: %s. Content will be ignored", warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1327 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | out: |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1331 | if (cid) |
| 1332 | OCSP_CERTID_free(cid); |
| 1333 | |
| 1334 | if (ocsp) |
| 1335 | free(ocsp); |
| 1336 | |
| 1337 | if (warn) |
| 1338 | free(warn); |
| 1339 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1340 | return ret; |
| 1341 | } |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1342 | #else /* OPENSSL_IS_BORINGSSL */ |
| 1343 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const struct cert_key_and_chain *ckch) |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1344 | { |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1345 | 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] | 1346 | } |
| 1347 | #endif |
| 1348 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 1349 | #endif |
| 1350 | |
| 1351 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1352 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1353 | |
| 1354 | #define CT_EXTENSION_TYPE 18 |
| 1355 | |
| 1356 | static int sctl_ex_index = -1; |
| 1357 | |
| 1358 | /* |
| 1359 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1360 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1361 | * is performed. |
| 1362 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1363 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1364 | { |
| 1365 | int ret = 1; |
| 1366 | int len, pos, sct_len; |
| 1367 | unsigned char *data; |
| 1368 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1369 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1370 | goto out; |
| 1371 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1372 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1373 | len = (data[0] << 8) | data[1]; |
| 1374 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1375 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1376 | goto out; |
| 1377 | |
| 1378 | data = data + 2; |
| 1379 | pos = 0; |
| 1380 | while (pos < len) { |
| 1381 | if (len - pos < 2) |
| 1382 | goto out; |
| 1383 | |
| 1384 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1385 | if (pos + sct_len + 2 > len) |
| 1386 | goto out; |
| 1387 | |
| 1388 | pos += sct_len + 2; |
| 1389 | } |
| 1390 | |
| 1391 | ret = 0; |
| 1392 | |
| 1393 | out: |
| 1394 | return ret; |
| 1395 | } |
| 1396 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1397 | /* Try to load a sctl from a buffer <buf> if not NULL, or read the file <sctl_path> |
| 1398 | * It fills the ckch->sctl buffer |
| 1399 | * return 0 on success or != 0 on failure */ |
| 1400 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, char *buf, struct cert_key_and_chain *ckch, char **err) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1401 | { |
| 1402 | int fd = -1; |
| 1403 | int r = 0; |
| 1404 | int ret = 1; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1405 | struct buffer tmp; |
| 1406 | struct buffer *src; |
| 1407 | struct buffer *sctl; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1408 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1409 | if (buf) { |
| 1410 | tmp.area = buf; |
| 1411 | tmp.data = strlen(buf); |
| 1412 | tmp.size = tmp.data + 1; |
| 1413 | src = &tmp; |
| 1414 | } else { |
| 1415 | fd = open(sctl_path, O_RDONLY); |
| 1416 | if (fd == -1) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1417 | goto end; |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1418 | |
| 1419 | trash.data = 0; |
| 1420 | while (trash.data < trash.size) { |
| 1421 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
| 1422 | if (r < 0) { |
| 1423 | if (errno == EINTR) |
| 1424 | continue; |
| 1425 | goto end; |
| 1426 | } |
| 1427 | else if (r == 0) { |
| 1428 | break; |
| 1429 | } |
| 1430 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1431 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1432 | src = &trash; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1433 | } |
| 1434 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1435 | ret = ssl_sock_parse_sctl(src); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1436 | if (ret) |
| 1437 | goto end; |
| 1438 | |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1439 | sctl = calloc(1, sizeof(*sctl)); |
| 1440 | if (!chunk_dup(sctl, src)) { |
| 1441 | free(sctl); |
| 1442 | sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1443 | goto end; |
| 1444 | } |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 1445 | ret = 0; |
| 1446 | /* TODO: free the previous SCTL in the ckch */ |
| 1447 | ckch->sctl = sctl; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1448 | |
| 1449 | end: |
| 1450 | if (fd != -1) |
| 1451 | close(fd); |
| 1452 | |
| 1453 | return ret; |
| 1454 | } |
| 1455 | |
| 1456 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1457 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1458 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1459 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1460 | *out = (unsigned char *) sctl->area; |
| 1461 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1462 | |
| 1463 | return 1; |
| 1464 | } |
| 1465 | |
| 1466 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1467 | { |
| 1468 | return 1; |
| 1469 | } |
| 1470 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1471 | static int ssl_sock_load_sctl(SSL_CTX *ctx, struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1472 | { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1473 | int ret = -1; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1474 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 1475 | 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] | 1476 | goto out; |
| 1477 | |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1478 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1479 | |
| 1480 | ret = 0; |
| 1481 | |
| 1482 | out: |
| 1483 | return ret; |
| 1484 | } |
| 1485 | |
| 1486 | #endif |
| 1487 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1488 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1489 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1490 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1491 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1492 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1493 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1494 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1495 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1496 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1497 | * change this to #if and do not assign a default value to this macro! |
| 1498 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1499 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1500 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 1501 | if ((conn->flags & (CO_FL_CONNECTED | CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_CONNECTED) { |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1502 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1503 | conn->err_code = CO_ER_SSL_RENEG; |
| 1504 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1505 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1506 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1507 | |
| 1508 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1509 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1510 | /* Long certificate chains optimz |
| 1511 | If write and read bios are differents, we |
| 1512 | consider that the buffering was activated, |
| 1513 | so we rise the output buffer size from 4k |
| 1514 | to 16k */ |
| 1515 | write_bio = SSL_get_wbio(ssl); |
| 1516 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1517 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1518 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1519 | } |
| 1520 | } |
| 1521 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1522 | } |
| 1523 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1524 | /* Callback is called for each certificate of the chain during a verify |
| 1525 | ok is set to 1 if preverify detect no error on current certificate. |
| 1526 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1527 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1528 | { |
| 1529 | SSL *ssl; |
| 1530 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1531 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1532 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1533 | |
| 1534 | 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] | 1535 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1536 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1537 | ctx = conn->xprt_ctx; |
| 1538 | |
| 1539 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1540 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1541 | if (ok) /* no errors */ |
| 1542 | return ok; |
| 1543 | |
| 1544 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1545 | err = X509_STORE_CTX_get_error(x_store); |
| 1546 | |
| 1547 | /* check if CA error needs to be ignored */ |
| 1548 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1549 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1550 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1551 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1552 | } |
| 1553 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1554 | if (__objt_listener(conn->target)->bind_conf->ca_ignerr & (1ULL << err)) { |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 1555 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1556 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1557 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1558 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1559 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1560 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1561 | return 0; |
| 1562 | } |
| 1563 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1564 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1565 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1566 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1567 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1568 | if (__objt_listener(conn->target)->bind_conf->crt_ignerr & (1ULL << err)) { |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 1569 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1570 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1571 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1572 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1573 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1574 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1575 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1576 | } |
| 1577 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1578 | static inline |
| 1579 | void ssl_sock_parse_clienthello(int write_p, int version, int content_type, |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1580 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1581 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1582 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1583 | unsigned char *msg; |
| 1584 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1585 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1586 | |
| 1587 | /* This function is called for "from client" and "to server" |
| 1588 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1589 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1590 | */ |
| 1591 | |
| 1592 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1593 | * otherwise it is set to 1. |
| 1594 | */ |
| 1595 | if (write_p != 0) |
| 1596 | return; |
| 1597 | |
| 1598 | /* content_type contains the type of message received or sent |
| 1599 | * according with the SSL/TLS protocol spec. This message is |
| 1600 | * encoded with one byte. The value 256 (two bytes) is used |
| 1601 | * for designing the SSL/TLS record layer. According with the |
| 1602 | * rfc6101, the expected message (other than 256) are: |
| 1603 | * - change_cipher_spec(20) |
| 1604 | * - alert(21) |
| 1605 | * - handshake(22) |
| 1606 | * - application_data(23) |
| 1607 | * - (255) |
| 1608 | * We are interessed by the handshake and specially the client |
| 1609 | * hello. |
| 1610 | */ |
| 1611 | if (content_type != 22) |
| 1612 | return; |
| 1613 | |
| 1614 | /* The message length is at least 4 bytes, containing the |
| 1615 | * message type and the message length. |
| 1616 | */ |
| 1617 | if (len < 4) |
| 1618 | return; |
| 1619 | |
| 1620 | /* First byte of the handshake message id the type of |
| 1621 | * message. The konwn types are: |
| 1622 | * - hello_request(0) |
| 1623 | * - client_hello(1) |
| 1624 | * - server_hello(2) |
| 1625 | * - certificate(11) |
| 1626 | * - server_key_exchange (12) |
| 1627 | * - certificate_request(13) |
| 1628 | * - server_hello_done(14) |
| 1629 | * We are interested by the client hello. |
| 1630 | */ |
| 1631 | msg = (unsigned char *)buf; |
| 1632 | if (msg[0] != 1) |
| 1633 | return; |
| 1634 | |
| 1635 | /* Next three bytes are the length of the message. The total length |
| 1636 | * must be this decoded length + 4. If the length given as argument |
| 1637 | * is not the same, we abort the protocol dissector. |
| 1638 | */ |
| 1639 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1640 | if (len < rec_len + 4) |
| 1641 | return; |
| 1642 | msg += 4; |
| 1643 | end = msg + rec_len; |
| 1644 | if (end < msg) |
| 1645 | return; |
| 1646 | |
| 1647 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1648 | * for minor, the random, composed by 4 bytes for the unix time and |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1649 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1650 | */ |
| 1651 | msg += 1 + 1 + 4 + 28; |
| 1652 | if (msg > end) |
| 1653 | return; |
| 1654 | |
| 1655 | /* Next, is session id: |
| 1656 | * if present, we have to jump by length + 1 for the size information |
| 1657 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1658 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1659 | if (msg[0] > 0) |
| 1660 | msg += msg[0]; |
| 1661 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1662 | if (msg > end) |
| 1663 | return; |
| 1664 | |
| 1665 | /* Next two bytes are the ciphersuite length. */ |
| 1666 | if (msg + 2 > end) |
| 1667 | return; |
| 1668 | rec_len = (msg[0] << 8) + msg[1]; |
| 1669 | msg += 2; |
| 1670 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1671 | return; |
| 1672 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1673 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1674 | if (!capture) |
| 1675 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1676 | /* Compute the xxh64 of the ciphersuite. */ |
| 1677 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1678 | |
| 1679 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1680 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1681 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1682 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1683 | |
| 1684 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1685 | } |
| 1686 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1687 | /* Callback is called for ssl protocol analyse */ |
| 1688 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1689 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1690 | #ifdef TLS1_RT_HEARTBEAT |
| 1691 | /* test heartbeat received (write_p is set to 0 |
| 1692 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1693 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1694 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
William Lallemand | 7e1770b | 2019-05-13 14:31:34 +0200 | [diff] [blame] | 1695 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1696 | const unsigned char *p = buf; |
| 1697 | unsigned int payload; |
| 1698 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1699 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1700 | |
| 1701 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1702 | if (*p != TLS1_HB_REQUEST) |
| 1703 | return; |
| 1704 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1705 | if (len < 1 + 2 + 16) /* 1 type + 2 size + 0 payload + 16 padding */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1706 | goto kill_it; |
| 1707 | |
| 1708 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1709 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1710 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1711 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1712 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1713 | * advertised payload is larger than the advertised packet |
| 1714 | * length, so we have garbage in the buffer between the |
| 1715 | * payload and the end of the buffer (p+len). We can't know |
| 1716 | * if the SSL stack is patched, and we don't know if we can |
| 1717 | * safely wipe out the area between p+3+len and payload. |
| 1718 | * So instead, we prevent the response from being sent by |
| 1719 | * setting the max_send_fragment to 0 and we report an SSL |
| 1720 | * error, which will kill this connection. It will be reported |
| 1721 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1722 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1723 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1724 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1725 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1726 | return; |
| 1727 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1728 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1729 | if (global_ssl.capture_cipherlist > 0) |
| 1730 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1731 | } |
| 1732 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1733 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1734 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1735 | const unsigned char *in, unsigned int inlen, |
| 1736 | void *arg) |
| 1737 | { |
| 1738 | struct server *srv = arg; |
| 1739 | |
| 1740 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1741 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1742 | return SSL_TLSEXT_ERR_OK; |
| 1743 | return SSL_TLSEXT_ERR_NOACK; |
| 1744 | } |
| 1745 | #endif |
| 1746 | |
| 1747 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1748 | /* This callback is used so that the server advertises the list of |
| 1749 | * negociable protocols for NPN. |
| 1750 | */ |
| 1751 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1752 | unsigned int *len, void *arg) |
| 1753 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1754 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1755 | |
| 1756 | *data = (const unsigned char *)conf->npn_str; |
| 1757 | *len = conf->npn_len; |
| 1758 | return SSL_TLSEXT_ERR_OK; |
| 1759 | } |
| 1760 | #endif |
| 1761 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1762 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1763 | /* This callback is used so that the server advertises the list of |
| 1764 | * negociable protocols for ALPN. |
| 1765 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1766 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1767 | unsigned char *outlen, |
| 1768 | const unsigned char *server, |
| 1769 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1770 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1771 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1772 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1773 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1774 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1775 | return SSL_TLSEXT_ERR_NOACK; |
| 1776 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1777 | return SSL_TLSEXT_ERR_OK; |
| 1778 | } |
| 1779 | #endif |
| 1780 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1781 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1782 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1783 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1784 | /* Create a X509 certificate with the specified servername and serial. This |
| 1785 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1786 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1787 | 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] | 1788 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1789 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1790 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1791 | SSL_CTX *ssl_ctx = NULL; |
| 1792 | X509 *newcrt = NULL; |
| 1793 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1794 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1795 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1796 | X509_NAME *name; |
| 1797 | const EVP_MD *digest; |
| 1798 | X509V3_CTX ctx; |
| 1799 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1800 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1801 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1802 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1803 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1804 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1805 | #else |
| 1806 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1807 | if (tmp_ssl) |
| 1808 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1809 | #endif |
| 1810 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1811 | goto mkcert_error; |
| 1812 | |
| 1813 | /* Create the certificate */ |
| 1814 | if (!(newcrt = X509_new())) |
| 1815 | goto mkcert_error; |
| 1816 | |
| 1817 | /* Set version number for the certificate (X509v3) and the serial |
| 1818 | * number */ |
| 1819 | if (X509_set_version(newcrt, 2L) != 1) |
| 1820 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 1821 | ASN1_INTEGER_set(X509_get_serialNumber(newcrt), _HA_ATOMIC_ADD(&ssl_ctx_serial, 1)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1822 | |
| 1823 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1824 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1825 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1826 | goto mkcert_error; |
| 1827 | |
| 1828 | /* set public key in the certificate */ |
| 1829 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1830 | goto mkcert_error; |
| 1831 | |
| 1832 | /* Set issuer name from the CA */ |
| 1833 | if (!(name = X509_get_subject_name(cacert))) |
| 1834 | goto mkcert_error; |
| 1835 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1836 | goto mkcert_error; |
| 1837 | |
| 1838 | /* Set the subject name using the same, but the CN */ |
| 1839 | name = X509_NAME_dup(name); |
| 1840 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1841 | (const unsigned char *)servername, |
| 1842 | -1, -1, 0) != 1) { |
| 1843 | X509_NAME_free(name); |
| 1844 | goto mkcert_error; |
| 1845 | } |
| 1846 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1847 | X509_NAME_free(name); |
| 1848 | goto mkcert_error; |
| 1849 | } |
| 1850 | X509_NAME_free(name); |
| 1851 | |
| 1852 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1853 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1854 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1855 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1856 | X509_EXTENSION *ext; |
| 1857 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1858 | 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] | 1859 | goto mkcert_error; |
| 1860 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1861 | X509_EXTENSION_free(ext); |
| 1862 | goto mkcert_error; |
| 1863 | } |
| 1864 | X509_EXTENSION_free(ext); |
| 1865 | } |
| 1866 | |
| 1867 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1868 | |
| 1869 | key_type = EVP_PKEY_base_id(capkey); |
| 1870 | |
| 1871 | if (key_type == EVP_PKEY_DSA) |
| 1872 | digest = EVP_sha1(); |
| 1873 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1874 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1875 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1876 | digest = EVP_sha256(); |
| 1877 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 1878 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1879 | int nid; |
| 1880 | |
| 1881 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1882 | goto mkcert_error; |
| 1883 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1884 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1885 | #else |
| 1886 | goto mkcert_error; |
| 1887 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1888 | } |
| 1889 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1890 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1891 | goto mkcert_error; |
| 1892 | |
| 1893 | /* Create and set the new SSL_CTX */ |
| 1894 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1895 | goto mkcert_error; |
| 1896 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1897 | goto mkcert_error; |
| 1898 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1899 | goto mkcert_error; |
| 1900 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1901 | goto mkcert_error; |
| 1902 | |
| 1903 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1904 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1905 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1906 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1907 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1908 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1909 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1910 | 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] | 1911 | EC_KEY *ecc; |
| 1912 | int nid; |
| 1913 | |
| 1914 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1915 | goto end; |
| 1916 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1917 | goto end; |
| 1918 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1919 | EC_KEY_free(ecc); |
| 1920 | } |
| 1921 | #endif |
| 1922 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1923 | return ssl_ctx; |
| 1924 | |
| 1925 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1926 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1927 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1928 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1929 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1930 | return NULL; |
| 1931 | } |
| 1932 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1933 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1934 | ssl_sock_create_cert(struct connection *conn, const char *servername, unsigned int key) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1935 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1936 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1937 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1938 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1939 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1940 | } |
| 1941 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1942 | /* 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] | 1943 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1944 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1945 | 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] | 1946 | { |
| 1947 | struct lru64 *lru = NULL; |
| 1948 | |
| 1949 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1950 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1951 | lru = lru64_lookup(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1952 | if (lru && lru->domain) { |
| 1953 | if (ssl) |
| 1954 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1955 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1956 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1957 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1958 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1959 | } |
| 1960 | return NULL; |
| 1961 | } |
| 1962 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1963 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 1964 | * function is not thread-safe, it should only be used to check if a certificate |
| 1965 | * exists in the lru cache (with no warranty it will not be removed by another |
| 1966 | * thread). It is kept for backward compatibility. */ |
| 1967 | SSL_CTX * |
| 1968 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 1969 | { |
| 1970 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 1971 | } |
| 1972 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1973 | /* Set a certificate int the LRU cache used to store generated |
| 1974 | * certificate. Return 0 on success, otherwise -1 */ |
| 1975 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1976 | 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] | 1977 | { |
| 1978 | struct lru64 *lru = NULL; |
| 1979 | |
| 1980 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1981 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1982 | lru = lru64_get(key, ssl_ctx_lru_tree, bind_conf->ca_sign_cert, 0); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1983 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1984 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1985 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1986 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1987 | if (lru->domain && lru->data) |
| 1988 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1989 | lru64_commit(lru, ssl_ctx, bind_conf->ca_sign_cert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1990 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1991 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1992 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 1993 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1994 | } |
| 1995 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1996 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1997 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1998 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1999 | { |
| 2000 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2001 | } |
| 2002 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2003 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2004 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2005 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2006 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2007 | 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] | 2008 | { |
| 2009 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2010 | SSL_CTX *ssl_ctx = NULL; |
| 2011 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2012 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2013 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2014 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2015 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2016 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2017 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2018 | if (lru && lru->domain) |
| 2019 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2020 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2021 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2022 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2023 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2024 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2025 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2026 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2027 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2028 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2029 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2030 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2031 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2032 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2033 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2034 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2035 | return 0; |
| 2036 | } |
| 2037 | static int |
| 2038 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2039 | { |
| 2040 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2041 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2042 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2043 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2044 | 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] | 2045 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2046 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2047 | } |
| 2048 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2049 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2050 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2051 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2052 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2053 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2054 | |
| 2055 | 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] | 2056 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2057 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2058 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2059 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2060 | #endif |
| 2061 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2062 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2063 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2064 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2065 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2066 | 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] | 2067 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2068 | 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] | 2069 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2070 | #endif |
| 2071 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2072 | 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] | 2073 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2074 | 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] | 2075 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2076 | #endif |
| 2077 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2078 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2079 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2080 | /* Unusable in this context. */ |
| 2081 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2082 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2083 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2084 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2085 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2086 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2087 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2088 | |
| 2089 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2090 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2091 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2092 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2093 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2094 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2095 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2096 | } |
| 2097 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2098 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2099 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2100 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2101 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2102 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2103 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2104 | } |
| 2105 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2106 | 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] | 2107 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2108 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2109 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2110 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2111 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2112 | } |
| 2113 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2114 | 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] | 2115 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2116 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2117 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2118 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2119 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2120 | } |
| 2121 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) { |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2122 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2123 | 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] | 2124 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2125 | #endif |
| 2126 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2127 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2128 | #if SSL_OP_NO_TLSv1_3 |
| 2129 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2130 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2131 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2132 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2133 | #endif |
| 2134 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2135 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2136 | |
| 2137 | static struct { |
| 2138 | int option; |
| 2139 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2140 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2141 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2142 | const char *name; |
| 2143 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2144 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2145 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2146 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2147 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2148 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2149 | {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] | 2150 | }; |
| 2151 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2152 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2153 | { |
| 2154 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2155 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2156 | SSL_set_SSL_CTX(ssl, ctx); |
| 2157 | } |
| 2158 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2159 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2160 | |
| 2161 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2162 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2163 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2164 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2165 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2166 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2167 | return SSL_TLSEXT_ERR_OK; |
| 2168 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2169 | } |
| 2170 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2171 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2172 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2173 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2174 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2175 | #else |
| 2176 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2177 | { |
| 2178 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2179 | struct connection *conn; |
| 2180 | struct bind_conf *s; |
| 2181 | const uint8_t *extension_data; |
| 2182 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2183 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2184 | |
| 2185 | char *wildp = NULL; |
| 2186 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2187 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2188 | 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] | 2189 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2190 | int i; |
| 2191 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2192 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2193 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2194 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2195 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2196 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2197 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2198 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2199 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2200 | #else |
| 2201 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2202 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2203 | /* |
| 2204 | * The server_name extension was given too much extensibility when it |
| 2205 | * was written, so parsing the normal case is a bit complex. |
| 2206 | */ |
| 2207 | size_t len; |
| 2208 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2209 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2210 | /* Extract the length of the supplied list of names. */ |
| 2211 | len = (*extension_data++) << 8; |
| 2212 | len |= *extension_data++; |
| 2213 | if (len + 2 != extension_len) |
| 2214 | goto abort; |
| 2215 | /* |
| 2216 | * The list in practice only has a single element, so we only consider |
| 2217 | * the first one. |
| 2218 | */ |
| 2219 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2220 | goto abort; |
| 2221 | extension_len = len - 1; |
| 2222 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2223 | if (extension_len <= 2) |
| 2224 | goto abort; |
| 2225 | len = (*extension_data++) << 8; |
| 2226 | len |= *extension_data++; |
| 2227 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2228 | || memchr(extension_data, 0, len) != NULL) |
| 2229 | goto abort; |
| 2230 | servername = extension_data; |
| 2231 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2232 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2233 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2234 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2235 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2236 | } |
| 2237 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2238 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2239 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2240 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2241 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2242 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2243 | goto abort; |
| 2244 | } |
| 2245 | |
| 2246 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2247 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2248 | 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] | 2249 | #else |
| 2250 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2251 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2252 | uint8_t sign; |
| 2253 | size_t len; |
| 2254 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2255 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2256 | len = (*extension_data++) << 8; |
| 2257 | len |= *extension_data++; |
| 2258 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2259 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2260 | if (len % 2 != 0) |
| 2261 | goto abort; |
| 2262 | for (; len > 0; len -= 2) { |
| 2263 | extension_data++; /* hash */ |
| 2264 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2265 | switch (sign) { |
| 2266 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2267 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2268 | break; |
| 2269 | case TLSEXT_signature_ecdsa: |
| 2270 | has_ecdsa_sig = 1; |
| 2271 | break; |
| 2272 | default: |
| 2273 | continue; |
| 2274 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2275 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2276 | break; |
| 2277 | } |
| 2278 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2279 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2280 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2281 | } |
| 2282 | 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] | 2283 | const SSL_CIPHER *cipher; |
| 2284 | size_t len; |
| 2285 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2286 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2287 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2288 | len = ctx->cipher_suites_len; |
| 2289 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2290 | #else |
| 2291 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2292 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2293 | if (len % 2 != 0) |
| 2294 | goto abort; |
| 2295 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2296 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2297 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2298 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2299 | #else |
| 2300 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2301 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2302 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2303 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2304 | break; |
| 2305 | } |
| 2306 | } |
| 2307 | } |
| 2308 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2309 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2310 | trash.area[i] = tolower(servername[i]); |
| 2311 | if (!wildp && (trash.area[i] == '.')) |
| 2312 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2313 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2314 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2315 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2316 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2317 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2318 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2319 | |
| 2320 | /* lookup a not neg filter */ |
| 2321 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2322 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2323 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2324 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2325 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2326 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2327 | break; |
| 2328 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2329 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2330 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2331 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2332 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2333 | if (!node_anonymous) |
| 2334 | node_anonymous = n; |
| 2335 | break; |
| 2336 | } |
| 2337 | } |
| 2338 | } |
| 2339 | if (wildp) { |
| 2340 | /* lookup in wildcards names */ |
| 2341 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2342 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2343 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2344 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2345 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2346 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2347 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2348 | break; |
| 2349 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2350 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2351 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2352 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2353 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2354 | if (!node_anonymous) |
| 2355 | node_anonymous = n; |
| 2356 | break; |
| 2357 | } |
| 2358 | } |
| 2359 | } |
| 2360 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2361 | /* select by key_signature priority order */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2362 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2363 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2364 | : (node_anonymous ? node_anonymous |
| 2365 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2366 | : node_rsa /* no rsa signature case (far far away) */ |
| 2367 | ))); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2368 | if (node) { |
| 2369 | /* switch ctx */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 2370 | struct ssl_bind_conf *conf = container_of(node, struct sni_ctx, name)->conf; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2371 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2372 | if (conf) { |
| 2373 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2374 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2375 | if (conf->early_data) |
| 2376 | allow_early = 1; |
| 2377 | } |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2378 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Olivier Houchard | 35a63cc | 2017-11-02 19:04:38 +0100 | [diff] [blame] | 2379 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2380 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2381 | |
William Lallemand | 0201047 | 2019-10-18 11:02:19 +0200 | [diff] [blame] | 2382 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2383 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2384 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2385 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2386 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2387 | } |
| 2388 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2389 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2390 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2391 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2392 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2393 | allow_early: |
| 2394 | #ifdef OPENSSL_IS_BORINGSSL |
| 2395 | if (allow_early) |
| 2396 | SSL_set_early_data_enabled(ssl, 1); |
| 2397 | #else |
| 2398 | if (!allow_early) |
| 2399 | SSL_set_max_early_data(ssl, 0); |
| 2400 | #endif |
| 2401 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2402 | abort: |
| 2403 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2404 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2405 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2406 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2407 | #else |
| 2408 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2409 | return 0; |
| 2410 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2411 | } |
| 2412 | |
| 2413 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2414 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2415 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2416 | * warning when no match is found, which implies the default (first) cert |
| 2417 | * will keep being used. |
| 2418 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2419 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2420 | { |
| 2421 | const char *servername; |
| 2422 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2423 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2424 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2425 | int i; |
| 2426 | (void)al; /* shut gcc stupid warning */ |
| 2427 | |
| 2428 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2429 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2430 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2431 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2432 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2433 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2434 | if (s->strict_sni) |
| 2435 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2436 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2437 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2438 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2439 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2440 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2441 | if (!servername[i]) |
| 2442 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2443 | trash.area[i] = tolower(servername[i]); |
| 2444 | if (!wildp && (trash.area[i] == '.')) |
| 2445 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2446 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2447 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2448 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2449 | HA_RWLOCK_RDLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2450 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2451 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2452 | |
| 2453 | /* lookup a not neg filter */ |
| 2454 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2455 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2456 | node = n; |
| 2457 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2458 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2459 | } |
| 2460 | if (!node && wildp) { |
| 2461 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2462 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2463 | } |
| 2464 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2465 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2466 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2467 | /* switch ctx done in ssl_sock_generate_certificate */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2468 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2469 | return SSL_TLSEXT_ERR_OK; |
| 2470 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2471 | #endif |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 2472 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2473 | if (s->strict_sni) |
| 2474 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2475 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2476 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2480 | 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] | 2481 | HA_RWLOCK_RDUNLOCK(SNI_LOCK, &s->sni_lock); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2482 | return SSL_TLSEXT_ERR_OK; |
| 2483 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2484 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2485 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2486 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2487 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2488 | |
| 2489 | static DH * ssl_get_dh_1024(void) |
| 2490 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2491 | static unsigned char dh1024_p[]={ |
| 2492 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2493 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2494 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2495 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2496 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2497 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2498 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2499 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2500 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2501 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2502 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2503 | }; |
| 2504 | static unsigned char dh1024_g[]={ |
| 2505 | 0x02, |
| 2506 | }; |
| 2507 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2508 | BIGNUM *p; |
| 2509 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2510 | DH *dh = DH_new(); |
| 2511 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2512 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2513 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2514 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2515 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2516 | DH_free(dh); |
| 2517 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2518 | } else { |
| 2519 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2520 | } |
| 2521 | } |
| 2522 | return dh; |
| 2523 | } |
| 2524 | |
| 2525 | static DH *ssl_get_dh_2048(void) |
| 2526 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2527 | static unsigned char dh2048_p[]={ |
| 2528 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2529 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2530 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2531 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2532 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2533 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2534 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2535 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2536 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2537 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2538 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2539 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2540 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2541 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2542 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2543 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2544 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2545 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2546 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2547 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2548 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2549 | 0xB7,0x1F,0x77,0xF3, |
| 2550 | }; |
| 2551 | static unsigned char dh2048_g[]={ |
| 2552 | 0x02, |
| 2553 | }; |
| 2554 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2555 | BIGNUM *p; |
| 2556 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2557 | DH *dh = DH_new(); |
| 2558 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2559 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2560 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2561 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2562 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2563 | DH_free(dh); |
| 2564 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2565 | } else { |
| 2566 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2567 | } |
| 2568 | } |
| 2569 | return dh; |
| 2570 | } |
| 2571 | |
| 2572 | static DH *ssl_get_dh_4096(void) |
| 2573 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2574 | static unsigned char dh4096_p[]={ |
| 2575 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2576 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2577 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2578 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2579 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2580 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2581 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2582 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2583 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2584 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2585 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2586 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2587 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2588 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2589 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2590 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2591 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2592 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2593 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2594 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2595 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2596 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2597 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2598 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2599 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2600 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2601 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2602 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2603 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2604 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2605 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2606 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2607 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2608 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2609 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2610 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2611 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2612 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2613 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2614 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2615 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2616 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2617 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2618 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2619 | static unsigned char dh4096_g[]={ |
| 2620 | 0x02, |
| 2621 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2622 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2623 | BIGNUM *p; |
| 2624 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2625 | DH *dh = DH_new(); |
| 2626 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2627 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2628 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2629 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2630 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2631 | DH_free(dh); |
| 2632 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2633 | } else { |
| 2634 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | return dh; |
| 2638 | } |
| 2639 | |
| 2640 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2641 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2642 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2643 | { |
| 2644 | DH *dh = NULL; |
| 2645 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2646 | int type; |
| 2647 | |
| 2648 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2649 | |
| 2650 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2651 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2652 | */ |
| 2653 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2654 | keylen = EVP_PKEY_bits(pkey); |
| 2655 | } |
| 2656 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2657 | if (keylen > global_ssl.default_dh_param) { |
| 2658 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2659 | } |
| 2660 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2661 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2662 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2663 | } |
| 2664 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2665 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2666 | } |
| 2667 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2668 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2669 | } |
| 2670 | |
| 2671 | return dh; |
| 2672 | } |
| 2673 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2674 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2675 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2676 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2677 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2678 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2679 | if (in == NULL) |
| 2680 | goto end; |
| 2681 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2682 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2683 | goto end; |
| 2684 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2685 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2686 | |
| 2687 | end: |
| 2688 | if (in) |
| 2689 | BIO_free(in); |
| 2690 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2691 | ERR_clear_error(); |
| 2692 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2693 | return dh; |
| 2694 | } |
| 2695 | |
| 2696 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2697 | { |
| 2698 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2699 | |
| 2700 | if (global_dh) { |
| 2701 | return 0; |
| 2702 | } |
| 2703 | |
| 2704 | return -1; |
| 2705 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2706 | #endif |
| 2707 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2708 | /* Alloc and init a ckch_inst */ |
| 2709 | static struct ckch_inst *ckch_inst_new() |
| 2710 | { |
| 2711 | struct ckch_inst *ckch_inst; |
| 2712 | |
| 2713 | ckch_inst = calloc(1, sizeof *ckch_inst); |
| 2714 | if (ckch_inst) |
| 2715 | LIST_INIT(&ckch_inst->sni_ctx); |
| 2716 | |
| 2717 | return ckch_inst; |
| 2718 | } |
| 2719 | |
| 2720 | |
| 2721 | /* 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] | 2722 | 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] | 2723 | struct bind_conf *s, struct ssl_bind_conf *conf, |
| 2724 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2725 | { |
| 2726 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2727 | int wild = 0, neg = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2728 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2729 | if (*name == '!') { |
| 2730 | neg = 1; |
| 2731 | name++; |
| 2732 | } |
| 2733 | if (*name == '*') { |
| 2734 | wild = 1; |
| 2735 | name++; |
| 2736 | } |
| 2737 | /* !* filter is a nop */ |
| 2738 | if (neg && wild) |
| 2739 | return order; |
| 2740 | if (*name) { |
| 2741 | int j, len; |
| 2742 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2743 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2744 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2745 | if (j >= trash.size) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2746 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2747 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2748 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2749 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2750 | if (!sc) |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 2751 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2752 | memcpy(sc->name.key, trash.area, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2753 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2754 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2755 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2756 | sc->order = order++; |
| 2757 | sc->neg = neg; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2758 | sc->wild = wild; |
| 2759 | sc->name.node.leaf_p = NULL; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2760 | if (kinfo.sig != TLSEXT_signature_anonymous) |
| 2761 | SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo); |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 2762 | |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2763 | LIST_ADDQ(&ckch_inst->sni_ctx, &sc->by_ckch_inst); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2764 | } |
| 2765 | return order; |
| 2766 | } |
| 2767 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2768 | /* |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2769 | * Insert the sni_ctxs that are listed in the ckch_inst, in the bind_conf's sni_ctx tree |
| 2770 | * This function can't return an error. |
| 2771 | * |
| 2772 | * *CAUTION*: The caller must lock the sni tree if called in multithreading mode |
| 2773 | */ |
| 2774 | static void ssl_sock_load_cert_sni(struct ckch_inst *ckch_inst, struct bind_conf *bind_conf) |
| 2775 | { |
| 2776 | |
| 2777 | struct sni_ctx *sc0, *sc0b, *sc1; |
| 2778 | struct ebmb_node *node; |
| 2779 | |
| 2780 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 2781 | |
| 2782 | /* ignore if sc0 was already inserted in a tree */ |
| 2783 | if (sc0->name.node.leaf_p) |
| 2784 | continue; |
| 2785 | |
| 2786 | /* Check for duplicates. */ |
| 2787 | if (sc0->wild) |
| 2788 | node = ebst_lookup(&bind_conf->sni_w_ctx, (char *)sc0->name.key); |
| 2789 | else |
| 2790 | node = ebst_lookup(&bind_conf->sni_ctx, (char *)sc0->name.key); |
| 2791 | |
| 2792 | for (; node; node = ebmb_next_dup(node)) { |
| 2793 | sc1 = ebmb_entry(node, struct sni_ctx, name); |
| 2794 | if (sc1->ctx == sc0->ctx && sc1->conf == sc0->conf |
| 2795 | && sc1->neg == sc0->neg && sc1->wild == sc0->wild) { |
| 2796 | /* it's a duplicate, we should remove and free it */ |
| 2797 | LIST_DEL(&sc0->by_ckch_inst); |
| 2798 | free(sc0); |
| 2799 | sc0 = NULL; |
William Lallemand | e15029b | 2019-10-14 10:46:58 +0200 | [diff] [blame] | 2800 | break; |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 2801 | } |
| 2802 | } |
| 2803 | |
| 2804 | /* if duplicate, ignore the insertion */ |
| 2805 | if (!sc0) |
| 2806 | continue; |
| 2807 | |
| 2808 | if (sc0->wild) |
| 2809 | ebst_insert(&bind_conf->sni_w_ctx, &sc0->name); |
| 2810 | else |
| 2811 | ebst_insert(&bind_conf->sni_ctx, &sc0->name); |
| 2812 | } |
| 2813 | } |
| 2814 | |
| 2815 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2816 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2817 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2818 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2819 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2820 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2821 | /* Loads Diffie-Hellman parameter from a ckchs to an SSL_CTX. |
| 2822 | * If there is no DH paramater availaible in the ckchs, the global |
| 2823 | * DH parameter is loaded into the SSL_CTX and if there is no |
| 2824 | * DH parameter available in ckchs nor in global, the default |
| 2825 | * DH parameters are applied on the SSL_CTX. |
| 2826 | * Returns a bitfield containing the flags: |
| 2827 | * ERR_FATAL in any fatal error case |
| 2828 | * ERR_ALERT if a reason of the error is availabine in err |
| 2829 | * ERR_WARN if a warning is available into err |
| 2830 | * The value 0 means there is no error nor warning and |
| 2831 | * the operation succeed. |
| 2832 | */ |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2833 | #ifndef OPENSSL_NO_DH |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2834 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch, |
| 2835 | const char *path, char **err) |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2836 | { |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2837 | int ret = 0; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2838 | DH *dh = NULL; |
| 2839 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 2840 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2841 | dh = ckch->dh; |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2842 | if (!SSL_CTX_set_tmp_dh(ctx, dh)) { |
| 2843 | memprintf(err, "%sunable to load the DH parameter specified in '%s'", |
| 2844 | err && *err ? *err : "", path); |
| 2845 | #if defined(SSL_CTX_set_dh_auto) |
| 2846 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2847 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2848 | err && *err ? *err : ""); |
| 2849 | #else |
| 2850 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2851 | err && *err ? *err : ""); |
| 2852 | #endif |
| 2853 | ret |= ERR_WARN; |
| 2854 | goto end; |
| 2855 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2856 | |
| 2857 | if (ssl_dh_ptr_index >= 0) { |
| 2858 | /* store a pointer to the DH params to avoid complaining about |
| 2859 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2860 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2861 | } |
| 2862 | } |
| 2863 | else if (global_dh) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2864 | if (!SSL_CTX_set_tmp_dh(ctx, global_dh)) { |
| 2865 | memprintf(err, "%sunable to use the global DH parameter for certificate '%s'", |
| 2866 | err && *err ? *err : "", path); |
| 2867 | #if defined(SSL_CTX_set_dh_auto) |
| 2868 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2869 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2870 | err && *err ? *err : ""); |
| 2871 | #else |
| 2872 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2873 | err && *err ? *err : ""); |
| 2874 | #endif |
| 2875 | ret |= ERR_WARN; |
| 2876 | goto end; |
| 2877 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2878 | } |
| 2879 | else { |
| 2880 | /* Clear openssl global errors stack */ |
| 2881 | ERR_clear_error(); |
| 2882 | |
| 2883 | if (global_ssl.default_dh_param <= 1024) { |
| 2884 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 2885 | if (local_dh_1024 == NULL) |
| 2886 | local_dh_1024 = ssl_get_dh_1024(); |
| 2887 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2888 | if (local_dh_1024 == NULL) { |
| 2889 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 2890 | err && *err ? *err : "", path); |
| 2891 | ret |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2892 | goto end; |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 2893 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2894 | |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 2895 | if (!SSL_CTX_set_tmp_dh(ctx, local_dh_1024)) { |
| 2896 | memprintf(err, "%sunable to load default 1024 bits DH parameter for certificate '%s'.\n", |
| 2897 | err && *err ? *err : "", path); |
| 2898 | #if defined(SSL_CTX_set_dh_auto) |
| 2899 | SSL_CTX_set_dh_auto(ctx, 1); |
| 2900 | memprintf(err, "%s, SSL library will use an automatically generated DH parameter.\n", |
| 2901 | err && *err ? *err : ""); |
| 2902 | #else |
| 2903 | memprintf(err, "%s, DH ciphers won't be available.\n", |
| 2904 | err && *err ? *err : ""); |
| 2905 | #endif |
| 2906 | ret |= ERR_WARN; |
| 2907 | goto end; |
| 2908 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2909 | } |
| 2910 | else { |
| 2911 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2912 | } |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | end: |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2916 | return ret; |
| 2917 | } |
| 2918 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2919 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2920 | /* Frees the contents of a cert_key_and_chain |
| 2921 | */ |
| 2922 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2923 | { |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2924 | if (!ckch) |
| 2925 | return; |
| 2926 | |
| 2927 | /* Free the certificate and set pointer to NULL */ |
| 2928 | if (ckch->cert) |
| 2929 | X509_free(ckch->cert); |
| 2930 | ckch->cert = NULL; |
| 2931 | |
| 2932 | /* Free the key and set pointer to NULL */ |
| 2933 | if (ckch->key) |
| 2934 | EVP_PKEY_free(ckch->key); |
| 2935 | ckch->key = NULL; |
| 2936 | |
| 2937 | /* Free each certificate in the chain */ |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 2938 | if (ckch->chain) |
| 2939 | sk_X509_pop_free(ckch->chain, X509_free); |
| 2940 | ckch->chain = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2941 | |
William Lallemand | 455af50 | 2019-10-17 18:04:45 +0200 | [diff] [blame] | 2942 | if (ckch->dh) |
| 2943 | DH_free(ckch->dh); |
| 2944 | ckch->dh = NULL; |
| 2945 | |
| 2946 | if (ckch->sctl) { |
| 2947 | free(ckch->sctl->area); |
| 2948 | ckch->sctl->area = NULL; |
| 2949 | free(ckch->sctl); |
| 2950 | ckch->sctl = NULL; |
| 2951 | } |
| 2952 | |
| 2953 | if (ckch->ocsp_response) { |
| 2954 | free(ckch->ocsp_response->area); |
| 2955 | ckch->ocsp_response->area = NULL; |
| 2956 | free(ckch->ocsp_response); |
| 2957 | ckch->ocsp_response = NULL; |
| 2958 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2959 | } |
| 2960 | |
William Lallemand | 8d0f893 | 2019-10-17 18:03:58 +0200 | [diff] [blame] | 2961 | /* |
| 2962 | * |
| 2963 | * This function copy a cert_key_and_chain in memory |
| 2964 | * |
| 2965 | * It's used to try to apply changes on a ckch before committing them, because |
| 2966 | * most of the time it's not possible to revert those changes |
| 2967 | * |
| 2968 | * Return a the dst or NULL |
| 2969 | */ |
| 2970 | static struct cert_key_and_chain *ssl_sock_copy_cert_key_and_chain(struct cert_key_and_chain *src, |
| 2971 | struct cert_key_and_chain *dst) |
| 2972 | { |
| 2973 | if (src->cert) { |
| 2974 | dst->cert = src->cert; |
| 2975 | X509_up_ref(src->cert); |
| 2976 | } |
| 2977 | |
| 2978 | if (src->key) { |
| 2979 | dst->key = src->key; |
| 2980 | EVP_PKEY_up_ref(src->key); |
| 2981 | } |
| 2982 | |
| 2983 | if (src->chain) { |
| 2984 | dst->chain = X509_chain_up_ref(src->chain); |
| 2985 | } |
| 2986 | |
| 2987 | if (src->dh) { |
| 2988 | DH_up_ref(src->dh); |
| 2989 | dst->dh = src->dh; |
| 2990 | } |
| 2991 | |
| 2992 | if (src->sctl) { |
| 2993 | struct buffer *sctl; |
| 2994 | |
| 2995 | sctl = calloc(1, sizeof(*sctl)); |
| 2996 | if (!chunk_dup(sctl, src->sctl)) { |
| 2997 | free(sctl); |
| 2998 | sctl = NULL; |
| 2999 | goto error; |
| 3000 | } |
| 3001 | dst->sctl = sctl; |
| 3002 | } |
| 3003 | |
| 3004 | if (src->ocsp_response) { |
| 3005 | struct buffer *ocsp_response; |
| 3006 | |
| 3007 | ocsp_response = calloc(1, sizeof(*ocsp_response)); |
| 3008 | if (!chunk_dup(ocsp_response, src->ocsp_response)) { |
| 3009 | free(ocsp_response); |
| 3010 | ocsp_response = NULL; |
| 3011 | goto error; |
| 3012 | } |
| 3013 | dst->ocsp_response = ocsp_response; |
| 3014 | } |
| 3015 | |
| 3016 | if (src->ocsp_issuer) { |
| 3017 | X509_up_ref(src->ocsp_issuer); |
| 3018 | dst->ocsp_issuer = src->ocsp_issuer; |
| 3019 | } |
| 3020 | |
| 3021 | return dst; |
| 3022 | |
| 3023 | error: |
| 3024 | |
| 3025 | /* free everything */ |
| 3026 | ssl_sock_free_cert_key_and_chain_contents(dst); |
| 3027 | |
| 3028 | return NULL; |
| 3029 | } |
| 3030 | |
| 3031 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3032 | /* checks if a key and cert exists in the ckch |
| 3033 | */ |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3034 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3035 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 3036 | { |
| 3037 | return (ckch->cert != NULL && ckch->key != NULL); |
| 3038 | } |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 3039 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3040 | |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3041 | /* |
| 3042 | * return 0 on success or != 0 on failure |
| 3043 | */ |
| 3044 | static int ssl_sock_load_issuer_file_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch, char **err) |
| 3045 | { |
| 3046 | int ret = 1; |
| 3047 | BIO *in = NULL; |
| 3048 | X509 *issuer; |
| 3049 | |
| 3050 | if (buf) { |
| 3051 | /* reading from a buffer */ |
| 3052 | in = BIO_new_mem_buf(buf, -1); |
| 3053 | if (in == NULL) { |
| 3054 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3055 | goto end; |
| 3056 | } |
| 3057 | |
| 3058 | } else { |
| 3059 | /* reading from a file */ |
| 3060 | in = BIO_new(BIO_s_file()); |
| 3061 | if (in == NULL) |
| 3062 | goto end; |
| 3063 | |
| 3064 | if (BIO_read_filename(in, path) <= 0) |
| 3065 | goto end; |
| 3066 | } |
| 3067 | |
| 3068 | issuer = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3069 | if (!issuer) { |
| 3070 | memprintf(err, "%s'%s' cannot be read or parsed'.\n", |
| 3071 | *err ? *err : "", path); |
| 3072 | goto end; |
| 3073 | } |
| 3074 | ret = 0; |
| 3075 | ckch->ocsp_issuer = issuer; |
| 3076 | |
| 3077 | end: |
| 3078 | |
| 3079 | ERR_clear_error(); |
| 3080 | if (in) |
| 3081 | BIO_free(in); |
| 3082 | |
| 3083 | return ret; |
| 3084 | } |
| 3085 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3086 | |
| 3087 | /* |
| 3088 | * Try to load a PEM file from a <path> or a buffer <buf> |
| 3089 | * The PEM must contain at least a Private Key and a Certificate, |
| 3090 | * It could contain a DH and a certificate chain. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3091 | * |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3092 | * If it failed you should not attempt to use the ckch but free it. |
| 3093 | * |
| 3094 | * Return 0 on success or != 0 on failure |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3095 | */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3096 | static int ssl_sock_load_pem_into_ckch(const char *path, char *buf, struct cert_key_and_chain *ckch , char **err) |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3097 | { |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3098 | BIO *in = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3099 | int ret = 1; |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3100 | X509 *ca; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3101 | X509 *cert = NULL; |
| 3102 | EVP_PKEY *key = NULL; |
| 3103 | DH *dh; |
| 3104 | |
| 3105 | if (buf) { |
| 3106 | /* reading from a buffer */ |
| 3107 | in = BIO_new_mem_buf(buf, -1); |
| 3108 | if (in == NULL) { |
| 3109 | memprintf(err, "%sCan't allocate memory\n", err && *err ? *err : ""); |
| 3110 | goto end; |
| 3111 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3112 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3113 | } else { |
| 3114 | /* reading from a file */ |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3115 | in = BIO_new(BIO_s_file()); |
| 3116 | if (in == NULL) |
| 3117 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3118 | |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3119 | if (BIO_read_filename(in, path) <= 0) |
| 3120 | goto end; |
William Lallemand | f11365b | 2019-09-19 14:25:58 +0200 | [diff] [blame] | 3121 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3122 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3123 | /* Read Private Key */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3124 | key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 3125 | if (key == NULL) { |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3126 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3127 | err && *err ? *err : "", path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3128 | goto end; |
| 3129 | } |
| 3130 | |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3131 | #ifndef OPENSSL_NO_DH |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3132 | /* Seek back to beginning of file */ |
| 3133 | if (BIO_reset(in) == -1) { |
| 3134 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3135 | err && *err ? *err : "", path); |
| 3136 | goto end; |
| 3137 | } |
| 3138 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3139 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 3140 | /* no need to return an error there, dh is not mandatory */ |
| 3141 | |
| 3142 | if (dh) { |
| 3143 | if (ckch->dh) |
| 3144 | DH_free(ckch->dh); |
| 3145 | ckch->dh = dh; |
| 3146 | } |
| 3147 | |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 3148 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3149 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3150 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 3151 | if (BIO_reset(in) == -1) { |
| 3152 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3153 | err && *err ? *err : "", path); |
| 3154 | goto end; |
| 3155 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3156 | |
| 3157 | /* Read Certificate */ |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3158 | cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3159 | if (cert == NULL) { |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3160 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3161 | err && *err ? *err : "", path); |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3162 | goto end; |
| 3163 | } |
| 3164 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3165 | if (!X509_check_private_key(cert, key)) { |
Emmanuel Hocdet | 03e09f3 | 2019-07-30 14:21:25 +0200 | [diff] [blame] | 3166 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3167 | err && *err ? *err : "", path); |
Emmanuel Hocdet | 03e09f3 | 2019-07-30 14:21:25 +0200 | [diff] [blame] | 3168 | goto end; |
| 3169 | } |
| 3170 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3171 | /* Key and Cert are good, we can use them in the ckch */ |
| 3172 | if (ckch->key) /* free the previous key */ |
| 3173 | EVP_PKEY_free(ckch->key); |
| 3174 | ckch->key = key; |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3175 | key = NULL; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3176 | |
| 3177 | if (ckch->cert) /* free the previous cert */ |
| 3178 | X509_free(ckch->cert); |
| 3179 | ckch->cert = cert; |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3180 | cert = NULL; |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3181 | |
| 3182 | /* Look for a Certificate Chain */ |
| 3183 | ca = PEM_read_bio_X509(in, NULL, NULL, NULL); |
| 3184 | if (ca) { |
| 3185 | /* there is a chain a in the PEM, clean the previous one in the CKCH */ |
| 3186 | if (ckch->chain) /* free the previous chain */ |
| 3187 | sk_X509_pop_free(ckch->chain, X509_free); |
| 3188 | ckch->chain = sk_X509_new_null(); |
| 3189 | if (!sk_X509_push(ckch->chain, ca)) { |
| 3190 | X509_free(ca); |
| 3191 | goto end; |
| 3192 | } |
| 3193 | } |
| 3194 | /* look for other crt in the chain */ |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3195 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) |
| 3196 | if (!sk_X509_push(ckch->chain, ca)) { |
| 3197 | X509_free(ca); |
| 3198 | goto end; |
| 3199 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3200 | |
Emmanuel Hocdet | ed17f47 | 2019-10-24 18:28:33 +0200 | [diff] [blame] | 3201 | /* no chain */ |
| 3202 | if (ckch->chain == NULL) { |
| 3203 | ckch->chain = sk_X509_new_null(); |
| 3204 | } |
| 3205 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3206 | ret = ERR_get_error(); |
| 3207 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 3208 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3209 | err && *err ? *err : "", path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3210 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3211 | } |
| 3212 | |
| 3213 | ret = 0; |
| 3214 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3215 | end: |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3216 | |
| 3217 | ERR_clear_error(); |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3218 | if (in) |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3219 | BIO_free(in); |
Emmanuel Hocdet | 83cbd3c | 2019-10-25 11:55:03 +0200 | [diff] [blame] | 3220 | if (key) |
| 3221 | EVP_PKEY_free(key); |
| 3222 | if (cert) |
| 3223 | X509_free(cert); |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3224 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3225 | return ret; |
| 3226 | } |
| 3227 | |
| 3228 | /* |
| 3229 | * Try to load in a ckch every files related to a ckch. |
| 3230 | * (PEM, sctl, ocsp, issuer etc.) |
| 3231 | * |
| 3232 | * This function is only used to load files during the configuration parsing, |
| 3233 | * it is not used with the CLI. |
| 3234 | * |
| 3235 | * This allows us to carry the contents of the file without having to read the |
| 3236 | * file multiple times. The caller must call |
| 3237 | * ssl_sock_free_cert_key_and_chain_contents. |
| 3238 | * |
| 3239 | * returns: |
| 3240 | * 0 on Success |
| 3241 | * 1 on SSL Failure |
| 3242 | */ |
| 3243 | static int ssl_sock_load_files_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 3244 | { |
| 3245 | int ret = 1; |
| 3246 | |
| 3247 | /* try to load the PEM */ |
| 3248 | if (ssl_sock_load_pem_into_ckch(path, NULL, ckch , err) != 0) { |
| 3249 | goto end; |
| 3250 | } |
| 3251 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3252 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3253 | /* try to load the sctl file */ |
| 3254 | { |
| 3255 | char fp[MAXPATHLEN+1]; |
| 3256 | struct stat st; |
| 3257 | |
| 3258 | snprintf(fp, MAXPATHLEN+1, "%s.sctl", path); |
| 3259 | if (stat(fp, &st) == 0) { |
William Lallemand | 0dfae6c | 2019-10-16 18:06:58 +0200 | [diff] [blame] | 3260 | if (ssl_sock_load_sctl_from_file(fp, NULL, ckch, err)) { |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3261 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3262 | *err ? *err : "", fp); |
| 3263 | ret = 1; |
| 3264 | goto end; |
| 3265 | } |
| 3266 | } |
| 3267 | } |
| 3268 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3269 | |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3270 | /* try to load an ocsp response file */ |
| 3271 | { |
| 3272 | char fp[MAXPATHLEN+1]; |
| 3273 | struct stat st; |
| 3274 | |
| 3275 | snprintf(fp, MAXPATHLEN+1, "%s.ocsp", path); |
| 3276 | if (stat(fp, &st) == 0) { |
William Lallemand | 3b5f360 | 2019-10-16 18:05:05 +0200 | [diff] [blame] | 3277 | if (ssl_sock_load_ocsp_response_from_file(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3278 | ret = 1; |
| 3279 | goto end; |
| 3280 | } |
| 3281 | } |
| 3282 | } |
| 3283 | |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3284 | #ifndef OPENSSL_IS_BORINGSSL /* Useless for BoringSSL */ |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3285 | if (ckch->ocsp_response) { |
| 3286 | X509 *issuer; |
| 3287 | int i; |
| 3288 | |
| 3289 | /* check if one of the certificate of the chain is the issuer */ |
| 3290 | for (i = 0; i < sk_X509_num(ckch->chain); i++) { |
| 3291 | issuer = sk_X509_value(ckch->chain, i); |
| 3292 | if (X509_check_issued(issuer, ckch->cert) == X509_V_OK) { |
| 3293 | ckch->ocsp_issuer = issuer; |
| 3294 | break; |
| 3295 | } else |
| 3296 | issuer = NULL; |
| 3297 | } |
| 3298 | |
| 3299 | /* if no issuer was found, try to load an issuer from the .issuer */ |
| 3300 | if (!issuer) { |
| 3301 | struct stat st; |
| 3302 | char fp[MAXPATHLEN+1]; |
| 3303 | |
| 3304 | snprintf(fp, MAXPATHLEN+1, "%s.issuer", path); |
| 3305 | if (stat(fp, &st) == 0) { |
William Lallemand | f9568fc | 2019-10-16 18:27:58 +0200 | [diff] [blame] | 3306 | if (ssl_sock_load_issuer_file_into_ckch(fp, NULL, ckch, err)) { |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3307 | ret = 1; |
| 3308 | goto end; |
| 3309 | } |
| 3310 | |
| 3311 | if (X509_check_issued(ckch->ocsp_issuer, ckch->cert) != X509_V_OK) { |
William Lallemand | 786188f | 2019-10-15 10:05:37 +0200 | [diff] [blame] | 3312 | memprintf(err, "%s '%s' is not an issuer'.\n", |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3313 | *err ? *err : "", fp); |
| 3314 | ret = 1; |
| 3315 | goto end; |
| 3316 | } |
| 3317 | } else { |
| 3318 | memprintf(err, "%sNo issuer found, cannot use the OCSP response'.\n", |
| 3319 | *err ? *err : ""); |
| 3320 | ret = 1; |
| 3321 | goto end; |
| 3322 | } |
| 3323 | } |
| 3324 | } |
Emmanuel Hocdet | eaad5cc | 2019-10-25 12:19:00 +0200 | [diff] [blame] | 3325 | #endif |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3326 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3327 | ret = 0; |
| 3328 | |
| 3329 | end: |
| 3330 | |
| 3331 | ERR_clear_error(); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3332 | |
| 3333 | /* Something went wrong in one of the reads */ |
| 3334 | if (ret != 0) |
| 3335 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 3336 | |
| 3337 | return ret; |
| 3338 | } |
| 3339 | |
| 3340 | /* Loads the info in ckch into ctx |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3341 | * Returns a bitfield containing the flags: |
| 3342 | * ERR_FATAL in any fatal error case |
| 3343 | * ERR_ALERT if the reason of the error is available in err |
| 3344 | * ERR_WARN if a warning is available into err |
| 3345 | * The value 0 means there is no error nor warning and |
| 3346 | * the operation succeed. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3347 | */ |
| 3348 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3349 | { |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3350 | int errcode = 0; |
| 3351 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3352 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3353 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3354 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3355 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3356 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3357 | } |
| 3358 | |
| 3359 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3360 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3361 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3362 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3363 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3364 | } |
| 3365 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3366 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3367 | #ifdef SSL_CTX_set1_chain |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3368 | if (!SSL_CTX_set1_chain(ctx, ckch->chain)) { |
| 3369 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3370 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3371 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3372 | goto end; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3373 | } |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3374 | #else |
| 3375 | { /* legacy compat (< openssl 1.0.2) */ |
| 3376 | X509 *ca; |
| 3377 | while ((ca = sk_X509_shift(ckch->chain))) |
| 3378 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3379 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3380 | err && *err ? *err : "", path); |
| 3381 | X509_free(ca); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3382 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3383 | goto end; |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3384 | } |
| 3385 | } |
| 3386 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3387 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3388 | #ifndef OPENSSL_NO_DH |
| 3389 | /* store a NULL pointer to indicate we have not yet loaded |
| 3390 | a custom DH param file */ |
| 3391 | if (ssl_dh_ptr_index >= 0) { |
| 3392 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3393 | } |
| 3394 | |
Emeric Brun | 7a88336 | 2019-10-17 13:27:40 +0200 | [diff] [blame] | 3395 | errcode |= ssl_sock_load_dh_params(ctx, ckch, path, err); |
| 3396 | if (errcode & ERR_CODE) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3397 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3398 | err && *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3399 | goto end; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3400 | } |
| 3401 | #endif |
| 3402 | |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3403 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
| 3404 | if (sctl_ex_index >= 0 && ckch->sctl) { |
| 3405 | if (ssl_sock_load_sctl(ctx, ckch->sctl) < 0) { |
| 3406 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3407 | *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3408 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3409 | goto end; |
William Lallemand | a17f411 | 2019-10-10 15:16:44 +0200 | [diff] [blame] | 3410 | } |
| 3411 | } |
| 3412 | #endif |
| 3413 | |
William Lallemand | 4a66013 | 2019-10-14 14:51:41 +0200 | [diff] [blame] | 3414 | #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] | 3415 | /* Load OCSP Info into context */ |
| 3416 | if (ckch->ocsp_response) { |
| 3417 | if (ssl_sock_load_ocsp(ctx, ckch) < 0) { |
| 3418 | if (err) |
| 3419 | 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", |
| 3420 | *err ? *err : "", path); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3421 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3422 | goto end; |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3423 | } |
| 3424 | } |
William Lallemand | 246c024 | 2019-10-11 08:59:13 +0200 | [diff] [blame] | 3425 | #endif |
| 3426 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3427 | end: |
| 3428 | return errcode; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3429 | } |
| 3430 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3431 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3432 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3433 | static int ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3434 | { |
| 3435 | struct sni_keytype *s_kt = NULL; |
| 3436 | struct ebmb_node *node; |
| 3437 | int i; |
| 3438 | |
| 3439 | for (i = 0; i < trash.size; i++) { |
| 3440 | if (!str[i]) |
| 3441 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3442 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3443 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3444 | trash.area[i] = 0; |
| 3445 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3446 | if (!node) { |
| 3447 | /* CN not found in tree */ |
| 3448 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3449 | /* Using memcpy here instead of strncpy. |
| 3450 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3451 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3452 | */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3453 | if (!s_kt) |
| 3454 | return -1; |
| 3455 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3456 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3457 | s_kt->keytypes = 0; |
| 3458 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3459 | } else { |
| 3460 | /* CN found in tree */ |
| 3461 | s_kt = container_of(node, struct sni_keytype, name); |
| 3462 | } |
| 3463 | |
| 3464 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3465 | s_kt->keytypes |= 1<<key_index; |
| 3466 | |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3467 | return 0; |
| 3468 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3469 | } |
| 3470 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3471 | #endif |
William Lallemand | 8c1cdde | 2019-10-18 10:58:14 +0200 | [diff] [blame] | 3472 | /* |
| 3473 | * Free a ckch_store and its ckch(s) |
| 3474 | * The linked ckch_inst are not free'd |
| 3475 | */ |
| 3476 | void ckchs_free(struct ckch_store *ckchs) |
| 3477 | { |
| 3478 | if (!ckchs) |
| 3479 | return; |
| 3480 | |
| 3481 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3482 | if (ckchs->multi) { |
| 3483 | int n; |
| 3484 | |
| 3485 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3486 | ssl_sock_free_cert_key_and_chain_contents(&ckchs->ckch[n]); |
| 3487 | } else |
| 3488 | #endif |
| 3489 | { |
| 3490 | ssl_sock_free_cert_key_and_chain_contents(ckchs->ckch); |
| 3491 | ckchs->ckch = NULL; |
| 3492 | } |
| 3493 | |
| 3494 | free(ckchs); |
| 3495 | } |
| 3496 | |
| 3497 | /* allocate and duplicate a ckch_store |
| 3498 | * Return a new ckch_store or NULL */ |
| 3499 | static struct ckch_store *ckchs_dup(const struct ckch_store *src) |
| 3500 | { |
| 3501 | struct ckch_store *dst; |
| 3502 | int pathlen; |
| 3503 | |
| 3504 | pathlen = strlen(src->path); |
| 3505 | dst = calloc(1, sizeof(*dst) + pathlen + 1); |
| 3506 | if (!dst) |
| 3507 | return NULL; |
| 3508 | /* copy previous key */ |
| 3509 | memcpy(dst->path, src->path, pathlen + 1); |
| 3510 | dst->multi = src->multi; |
| 3511 | LIST_INIT(&dst->ckch_inst); |
| 3512 | |
| 3513 | dst->ckch = calloc((src->multi ? SSL_SOCK_NUM_KEYTYPES : 1), sizeof(*dst->ckch)); |
| 3514 | if (!dst->ckch) |
| 3515 | goto error; |
| 3516 | |
| 3517 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3518 | if (src->multi) { |
| 3519 | int n; |
| 3520 | |
| 3521 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3522 | if (&src->ckch[n]) { |
| 3523 | if (!ssl_sock_copy_cert_key_and_chain(&src->ckch[n], &dst->ckch[n])) |
| 3524 | goto error; |
| 3525 | } |
| 3526 | } |
| 3527 | } else |
| 3528 | #endif |
| 3529 | { |
| 3530 | if (!ssl_sock_copy_cert_key_and_chain(src->ckch, dst->ckch)) |
| 3531 | goto error; |
| 3532 | } |
| 3533 | |
| 3534 | return dst; |
| 3535 | |
| 3536 | error: |
| 3537 | ckchs_free(dst); |
| 3538 | |
| 3539 | return NULL; |
| 3540 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3541 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3542 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3543 | * lookup a path into the ckchs tree. |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3544 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3545 | static inline struct ckch_store *ckchs_lookup(char *path) |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3546 | { |
| 3547 | struct ebmb_node *eb; |
| 3548 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3549 | eb = ebst_lookup(&ckchs_tree, path); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3550 | if (!eb) |
| 3551 | return NULL; |
| 3552 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3553 | return ebmb_entry(eb, struct ckch_store, node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3554 | } |
| 3555 | |
| 3556 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3557 | * This function allocate a ckch_store and populate it with certificates from files. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3558 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3559 | static struct ckch_store *ckchs_load_cert_file(char *path, int multi, char **err) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3560 | { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3561 | struct ckch_store *ckchs; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3562 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3563 | ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1); |
| 3564 | if (!ckchs) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3565 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 3566 | goto end; |
| 3567 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3568 | ckchs->ckch = calloc(1, sizeof(*ckchs->ckch) * (multi ? SSL_SOCK_NUM_KEYTYPES : 1)); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3569 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3570 | if (!ckchs->ckch) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3571 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 3572 | goto end; |
| 3573 | } |
| 3574 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3575 | LIST_INIT(&ckchs->ckch_inst); |
| 3576 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3577 | if (!multi) { |
| 3578 | |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3579 | if (ssl_sock_load_files_into_ckch(path, ckchs->ckch, err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3580 | goto end; |
| 3581 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3582 | /* insert into the ckchs tree */ |
| 3583 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 3584 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3585 | } else { |
| 3586 | int found = 0; |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3587 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3588 | char fp[MAXPATHLEN+1] = {0}; |
| 3589 | int n = 0; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3590 | |
| 3591 | /* Load all possible certs and keys */ |
| 3592 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3593 | struct stat buf; |
| 3594 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3595 | if (stat(fp, &buf) == 0) { |
William Lallemand | 96a9c97 | 2019-10-17 11:56:17 +0200 | [diff] [blame] | 3596 | if (ssl_sock_load_files_into_ckch(fp, &ckchs->ckch[n], err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3597 | goto end; |
| 3598 | found = 1; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3599 | ckchs->multi = 1; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3600 | } |
| 3601 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3602 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3603 | |
| 3604 | if (!found) { |
William Lallemand | 6e5f2ce | 2019-08-01 14:43:20 +0200 | [diff] [blame] | 3605 | memprintf(err, "%sDidn't find any certificate for bundle '%s'.\n", err && *err ? *err : "", path); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3606 | goto end; |
| 3607 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3608 | /* insert into the ckchs tree */ |
| 3609 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 3610 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3611 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3612 | return ckchs; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3613 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3614 | end: |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3615 | if (ckchs) { |
| 3616 | free(ckchs->ckch); |
| 3617 | ebmb_delete(&ckchs->node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3618 | } |
| 3619 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3620 | free(ckchs); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3621 | |
| 3622 | return NULL; |
| 3623 | } |
| 3624 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3625 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3626 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3627 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3628 | * Take a ckch_store which contains a multi-certificate bundle. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3629 | * Group these certificates into a set of SSL_CTX* |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3630 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3631 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3632 | * This will allow the user to explicitly group multiple cert/keys for a single purpose |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3633 | * |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3634 | * Returns a bitfield containing the flags: |
| 3635 | * ERR_FATAL in any fatal error case |
| 3636 | * ERR_ALERT if the reason of the error is available in err |
| 3637 | * ERR_WARN if a warning is available into err |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3638 | * |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3639 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3640 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 3641 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3642 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3643 | { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3644 | int i = 0, n = 0; |
| 3645 | struct cert_key_and_chain *certs_and_keys; |
William Lallemand | 4b989f2 | 2019-10-04 18:36:55 +0200 | [diff] [blame] | 3646 | struct eb_root sni_keytypes_map = EB_ROOT; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3647 | struct ebmb_node *node; |
| 3648 | struct ebmb_node *next; |
| 3649 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 3650 | * of keytypes |
| 3651 | */ |
| 3652 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3653 | int errcode = 0; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3654 | X509_NAME *xname = NULL; |
| 3655 | char *str = NULL; |
| 3656 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3657 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 3658 | #endif |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3659 | struct ckch_inst *ckch_inst; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3660 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3661 | *ckchi = NULL; |
| 3662 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3663 | if (!ckchs || !ckchs->ckch || !ckchs->multi) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3664 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3665 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3666 | return ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3667 | } |
| 3668 | |
| 3669 | ckch_inst = ckch_inst_new(); |
| 3670 | if (!ckch_inst) { |
| 3671 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3672 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3673 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3674 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3675 | } |
| 3676 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3677 | certs_and_keys = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3678 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3679 | /* at least one of the instances is using filters during the config |
| 3680 | * parsing, that's ok to inherit this during loading on CLI */ |
| 3681 | ckchs->filters = !!fcount; |
| 3682 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3683 | /* Process each ckch and update keytypes for each CN/SAN |
| 3684 | * for example, if CN/SAN www.a.com is associated with |
| 3685 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3686 | * www.a.com will have: |
| 3687 | * keyindex = 0 | 1 | 4 = 5 |
| 3688 | */ |
| 3689 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3690 | int ret; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3691 | |
| 3692 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3693 | continue; |
| 3694 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3695 | if (fcount) { |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3696 | for (i = 0; i < fcount; i++) { |
| 3697 | ret = ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3698 | if (ret < 0) { |
| 3699 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3700 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3701 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3702 | goto end; |
| 3703 | } |
| 3704 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3705 | } else { |
| 3706 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3707 | * so the line that contains logic is marked via comments |
| 3708 | */ |
| 3709 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3710 | i = -1; |
| 3711 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3712 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3713 | ASN1_STRING *value; |
| 3714 | value = X509_NAME_ENTRY_get_data(entry); |
| 3715 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3716 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3717 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3718 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3719 | OPENSSL_free(str); |
| 3720 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3721 | if (ret < 0) { |
| 3722 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3723 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3724 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3725 | goto end; |
| 3726 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3727 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3728 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3729 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3730 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3731 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3732 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3733 | if (names) { |
| 3734 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3735 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3736 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3737 | if (name->type == GEN_DNS) { |
| 3738 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3739 | /* Important line is here */ |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3740 | ret = ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3741 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3742 | OPENSSL_free(str); |
| 3743 | str = NULL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3744 | if (ret < 0) { |
| 3745 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3746 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3747 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 28a8fce | 2019-10-04 17:36:55 +0200 | [diff] [blame] | 3748 | goto end; |
| 3749 | } |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3750 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3751 | } |
| 3752 | } |
| 3753 | } |
| 3754 | } |
| 3755 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3756 | } |
| 3757 | |
| 3758 | /* If no files found, return error */ |
| 3759 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3760 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3761 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3762 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3763 | goto end; |
| 3764 | } |
| 3765 | |
| 3766 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3767 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3768 | * and add each CTX to the SNI tree |
| 3769 | * |
| 3770 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3771 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3772 | * combination is denoted by the key in the map. Each key |
| 3773 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3774 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3775 | * entry in the array to correspond to the unique combo (key) |
| 3776 | * associated with i. This unique key combo (i) will be associated |
| 3777 | * with combos[i-1] |
| 3778 | */ |
| 3779 | |
| 3780 | node = ebmb_first(&sni_keytypes_map); |
| 3781 | while (node) { |
| 3782 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3783 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3784 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3785 | |
| 3786 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3787 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3788 | cur_ctx = key_combos[i-1].ctx; |
| 3789 | |
| 3790 | if (cur_ctx == NULL) { |
| 3791 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3792 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3793 | if (cur_ctx == NULL) { |
| 3794 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3795 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3796 | errcode |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3797 | goto end; |
| 3798 | } |
| 3799 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3800 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3801 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3802 | if (i & (1<<n)) { |
| 3803 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3804 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3805 | errcode |= ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err); |
| 3806 | if (errcode & ERR_CODE) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3807 | goto end; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3808 | } |
| 3809 | } |
| 3810 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3811 | /* Update key_combos */ |
| 3812 | key_combos[i-1].ctx = cur_ctx; |
| 3813 | } |
| 3814 | |
| 3815 | /* Update SNI Tree */ |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 3816 | |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3817 | key_combos[i-1].order = ckch_inst_add_cert_sni(cur_ctx, ckch_inst, bind_conf, ssl_conf, |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3818 | kinfo, str, key_combos[i-1].order); |
| 3819 | if (key_combos[i-1].order < 0) { |
| 3820 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3821 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3822 | goto end; |
| 3823 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3824 | node = ebmb_next(node); |
| 3825 | } |
| 3826 | |
| 3827 | |
| 3828 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3829 | if (!bind_conf->default_ctx) { |
| 3830 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3831 | if (key_combos[i].ctx) { |
| 3832 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3833 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3834 | break; |
| 3835 | } |
| 3836 | } |
| 3837 | } |
| 3838 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3839 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3840 | ckch_inst->ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3841 | end: |
| 3842 | |
| 3843 | if (names) |
| 3844 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3845 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3846 | node = ebmb_first(&sni_keytypes_map); |
| 3847 | while (node) { |
| 3848 | next = ebmb_next(node); |
| 3849 | ebmb_delete(node); |
William Lallemand | 8ed5b96 | 2019-10-04 17:24:39 +0200 | [diff] [blame] | 3850 | free(ebmb_entry(node, struct sni_keytype, name)); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3851 | node = next; |
| 3852 | } |
| 3853 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3854 | if (errcode & ERR_CODE && ckch_inst) { |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 3855 | struct sni_ctx *sc0, *sc0b; |
| 3856 | |
| 3857 | /* free the SSL_CTX in case of error */ |
| 3858 | for (i = 0; i < SSL_SOCK_POSSIBLE_KT_COMBOS; i++) { |
| 3859 | if (key_combos[i].ctx) |
| 3860 | SSL_CTX_free(key_combos[i].ctx); |
| 3861 | } |
| 3862 | |
| 3863 | /* free the sni_ctx in case of error */ |
| 3864 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 3865 | |
| 3866 | ebmb_delete(&sc0->name); |
| 3867 | LIST_DEL(&sc0->by_ckch_inst); |
| 3868 | free(sc0); |
| 3869 | } |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3870 | free(ckch_inst); |
| 3871 | ckch_inst = NULL; |
William Lallemand | 0c6d12f | 2019-10-04 18:38:51 +0200 | [diff] [blame] | 3872 | } |
| 3873 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3874 | *ckchi = ckch_inst; |
| 3875 | return errcode; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3876 | } |
| 3877 | #else |
| 3878 | /* This is a dummy, that just logs an error and returns error */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3879 | static int ckch_inst_new_load_multi_store(const char *path, struct ckch_store *ckchs, |
| 3880 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3881 | char **sni_filter, int fcount, struct ckch_inst **ckchi, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3882 | { |
| 3883 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3884 | err && *err ? *err : "", path, strerror(errno)); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3885 | return ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3886 | } |
| 3887 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3888 | #endif /* #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL: Support for loading multiple certs into a single SSL_CTX */ |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3889 | |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3890 | /* |
| 3891 | * This function allocate a ckch_inst and create its snis |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3892 | * |
| 3893 | * Returns a bitfield containing the flags: |
| 3894 | * ERR_FATAL in any fatal error case |
| 3895 | * ERR_ALERT if the reason of the error is available in err |
| 3896 | * ERR_WARN if a warning is available into err |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3897 | */ |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3898 | static int ckch_inst_new_load_store(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf, |
| 3899 | 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] | 3900 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3901 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3902 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3903 | int order = 0; |
| 3904 | X509_NAME *xname; |
| 3905 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3906 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3907 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3908 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3909 | STACK_OF(GENERAL_NAME) *names; |
| 3910 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3911 | struct cert_key_and_chain *ckch; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3912 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3913 | int errcode = 0; |
| 3914 | |
| 3915 | *ckchi = NULL; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3916 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3917 | if (!ckchs || !ckchs->ckch) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3918 | return ERR_FATAL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3919 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3920 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3921 | |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 3922 | /* at least one of the instances is using filters during the config |
| 3923 | * parsing, that's ok to inherit this during loading on CLI */ |
| 3924 | ckchs->filters = !!fcount; |
| 3925 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3926 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3927 | if (!ctx) { |
| 3928 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3929 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3930 | errcode |= ERR_ALERT | ERR_FATAL; |
| 3931 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3932 | } |
| 3933 | |
Emeric Brun | a96b582 | 2019-10-17 13:25:14 +0200 | [diff] [blame] | 3934 | errcode |= ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err); |
| 3935 | if (errcode & ERR_CODE) |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3936 | goto error; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 3937 | |
| 3938 | ckch_inst = ckch_inst_new(); |
| 3939 | if (!ckch_inst) { |
| 3940 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3941 | err && *err ? *err : "", path); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3942 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3943 | goto error; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3944 | } |
| 3945 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3946 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3947 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3948 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3949 | switch(EVP_PKEY_base_id(pkey)) { |
| 3950 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3951 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3952 | break; |
| 3953 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3954 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3955 | break; |
| 3956 | case EVP_PKEY_DSA: |
| 3957 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3958 | break; |
| 3959 | } |
| 3960 | EVP_PKEY_free(pkey); |
| 3961 | } |
| 3962 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3963 | if (fcount) { |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3964 | while (fcount--) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3965 | 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] | 3966 | if (order < 0) { |
| 3967 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3968 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3969 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3970 | } |
| 3971 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3972 | } |
| 3973 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3974 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3975 | names = X509_get_ext_d2i(ckch->cert, NID_subject_alt_name, NULL, NULL); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3976 | if (names) { |
| 3977 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3978 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3979 | if (name->type == GEN_DNS) { |
| 3980 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 3981 | 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] | 3982 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3983 | if (order < 0) { |
| 3984 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 3985 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 3986 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 3987 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3988 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3989 | } |
| 3990 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3991 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3992 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3993 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3994 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3995 | i = -1; |
| 3996 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3997 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3998 | ASN1_STRING *value; |
| 3999 | |
| 4000 | value = X509_NAME_ENTRY_get_data(entry); |
| 4001 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | 1d29c74 | 2019-10-04 00:53:29 +0200 | [diff] [blame] | 4002 | 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] | 4003 | OPENSSL_free(str); |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4004 | if (order < 0) { |
| 4005 | memprintf(err, "%sunable to create a sni context.\n", err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4006 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4007 | goto error; |
William Lallemand | fe49bb3 | 2019-10-03 23:46:33 +0200 | [diff] [blame] | 4008 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4009 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4010 | } |
| 4011 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4012 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 4013 | * the tree, so it will be discovered and cleaned in time. |
| 4014 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4015 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4016 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4017 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4018 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 4019 | err && *err ? *err : ""); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4020 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4021 | goto error; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4022 | } |
| 4023 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4024 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4025 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4026 | bind_conf->default_ssl_conf = ssl_conf; |
| 4027 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4028 | |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4029 | /* everything succeed, the ckch instance can be used */ |
| 4030 | ckch_inst->bind_conf = bind_conf; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 4031 | ckch_inst->ssl_conf = ssl_conf; |
William Lallemand | 9117de9 | 2019-10-04 00:29:42 +0200 | [diff] [blame] | 4032 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4033 | *ckchi = ckch_inst; |
| 4034 | return errcode; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4035 | |
| 4036 | error: |
| 4037 | /* free the allocated sni_ctxs */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4038 | if (ckch_inst) { |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4039 | struct sni_ctx *sc0, *sc0b; |
| 4040 | |
| 4041 | list_for_each_entry_safe(sc0, sc0b, &ckch_inst->sni_ctx, by_ckch_inst) { |
| 4042 | |
| 4043 | ebmb_delete(&sc0->name); |
| 4044 | LIST_DEL(&sc0->by_ckch_inst); |
| 4045 | free(sc0); |
| 4046 | } |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4047 | free(ckch_inst); |
| 4048 | ckch_inst = NULL; |
William Lallemand | d919937 | 2019-10-04 15:37:05 +0200 | [diff] [blame] | 4049 | } |
| 4050 | /* We only created 1 SSL_CTX so we can free it there */ |
| 4051 | SSL_CTX_free(ctx); |
| 4052 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4053 | return errcode; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4054 | } |
| 4055 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4056 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4057 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, |
| 4058 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 4059 | char **sni_filter, int fcount, char **err) |
| 4060 | { |
| 4061 | struct ckch_inst *ckch_inst = NULL; |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4062 | int errcode = 0; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4063 | |
| 4064 | /* we found the ckchs in the tree, we can use it directly */ |
| 4065 | if (ckchs->multi) |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4066 | errcode |= ckch_inst_new_load_multi_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] | 4067 | else |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4068 | 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] | 4069 | |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4070 | if (errcode & ERR_CODE) |
| 4071 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4072 | |
| 4073 | ssl_sock_load_cert_sni(ckch_inst, bind_conf); |
| 4074 | |
| 4075 | /* succeed, add the instance to the ckch_store's list of instance */ |
| 4076 | LIST_ADDQ(&ckchs->ckch_inst, &ckch_inst->by_ckchs); |
Emeric Brun | 054563d | 2019-10-17 13:16:58 +0200 | [diff] [blame] | 4077 | return errcode; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4078 | } |
| 4079 | |
| 4080 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4081 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4082 | int ssl_sock_load_cert(char *path, struct bind_conf *bind_conf, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4083 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4084 | struct dirent **de_list; |
| 4085 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4086 | DIR *dir; |
| 4087 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 4088 | char *end; |
| 4089 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4090 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4091 | struct ckch_store *ckchs; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4092 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4093 | int is_bundle; |
| 4094 | int j; |
| 4095 | #endif |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4096 | if ((ckchs = ckchs_lookup(path))) { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4097 | /* we found the ckchs in the tree, we can use it directly */ |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4098 | return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 4099 | } |
| 4100 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4101 | if (stat(path, &buf) == 0) { |
| 4102 | dir = opendir(path); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4103 | if (!dir) { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4104 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 4105 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4106 | return ERR_ALERT | ERR_FATAL; |
| 4107 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4108 | return ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4109 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4110 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4111 | /* strip trailing slashes, including first one */ |
| 4112 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 4113 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4114 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4115 | n = scandir(path, &de_list, 0, alphasort); |
| 4116 | if (n < 0) { |
| 4117 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 4118 | err && *err ? *err : "", path, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4119 | cfgerr |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4120 | } |
| 4121 | else { |
| 4122 | for (i = 0; i < n; i++) { |
| 4123 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 4124 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4125 | end = strrchr(de->d_name, '.'); |
| 4126 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 4127 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4128 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4129 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 4130 | if (stat(fp, &buf) != 0) { |
| 4131 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 4132 | err && *err ? *err : "", fp, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4133 | cfgerr |= ERR_ALERT | ERR_FATAL; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4134 | goto ignore_entry; |
| 4135 | } |
| 4136 | if (!S_ISREG(buf.st_mode)) |
| 4137 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4138 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4139 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4140 | is_bundle = 0; |
| 4141 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 4142 | |
| 4143 | if (end) { |
| 4144 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 4145 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 4146 | is_bundle = 1; |
| 4147 | break; |
| 4148 | } |
| 4149 | } |
| 4150 | |
| 4151 | if (is_bundle) { |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4152 | int dp_len; |
| 4153 | |
| 4154 | dp_len = end - de->d_name; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4155 | |
| 4156 | /* increment i and free de until we get to a non-bundle cert |
| 4157 | * Note here that we look at de_list[i + 1] before freeing de |
Willy Tarreau | 0580052 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 4158 | * this is important since ignore_entry will free de. This also |
| 4159 | * guarantees that de->d_name continues to hold the same prefix. |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4160 | */ |
Willy Tarreau | 0580052 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 4161 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, de->d_name, dp_len)) { |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4162 | free(de); |
| 4163 | i++; |
| 4164 | de = de_list[i]; |
| 4165 | } |
| 4166 | |
Willy Tarreau | 0580052 | 2019-10-29 10:48:50 +0100 | [diff] [blame] | 4167 | snprintf(fp, sizeof(fp), "%s/%.*s", path, dp_len, de->d_name); |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4168 | if ((ckchs = ckchs_lookup(fp)) == NULL) |
| 4169 | ckchs = ckchs_load_cert_file(fp, 1, err); |
| 4170 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4171 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4172 | else |
| 4173 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 4174 | /* Successfully processed the bundle */ |
| 4175 | goto ignore_entry; |
| 4176 | } |
| 4177 | } |
| 4178 | |
| 4179 | #endif |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4180 | if ((ckchs = ckchs_lookup(fp)) == NULL) |
| 4181 | ckchs = ckchs_load_cert_file(fp, 0, err); |
| 4182 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4183 | cfgerr |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 614ca0d | 2019-10-07 13:52:11 +0200 | [diff] [blame] | 4184 | else |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4185 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 4186 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4187 | ignore_entry: |
| 4188 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 4189 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4190 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4191 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4192 | closedir(dir); |
| 4193 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4194 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4195 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4196 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 4197 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4198 | return ERR_ALERT | ERR_FATAL; |
| 4199 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4200 | cfgerr |= ssl_sock_load_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 4201 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4202 | return cfgerr; |
| 4203 | } |
| 4204 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4205 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 4206 | * done once. Zero is returned if the operation fails. No error is returned |
| 4207 | * if the random is said as not implemented, because we expect that openssl |
| 4208 | * will use another method once needed. |
| 4209 | */ |
| 4210 | static int ssl_initialize_random() |
| 4211 | { |
| 4212 | unsigned char random; |
| 4213 | static int random_initialized = 0; |
| 4214 | |
| 4215 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 4216 | random_initialized = 1; |
| 4217 | |
| 4218 | return random_initialized; |
| 4219 | } |
| 4220 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4221 | /* release ssl bind conf */ |
| 4222 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4223 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4224 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4225 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4226 | free(conf->npn_str); |
| 4227 | conf->npn_str = NULL; |
| 4228 | #endif |
| 4229 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4230 | free(conf->alpn_str); |
| 4231 | conf->alpn_str = NULL; |
| 4232 | #endif |
| 4233 | free(conf->ca_file); |
| 4234 | conf->ca_file = NULL; |
| 4235 | free(conf->crl_file); |
| 4236 | conf->crl_file = NULL; |
| 4237 | free(conf->ciphers); |
| 4238 | conf->ciphers = NULL; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4239 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4240 | free(conf->ciphersuites); |
| 4241 | conf->ciphersuites = NULL; |
| 4242 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4243 | free(conf->curves); |
| 4244 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4245 | free(conf->ecdhe); |
| 4246 | conf->ecdhe = NULL; |
| 4247 | } |
| 4248 | } |
| 4249 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4250 | /* Returns a set of ERR_* flags possibly with an error in <err>. */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4251 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 4252 | { |
| 4253 | char thisline[CRT_LINESIZE]; |
| 4254 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4255 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4256 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4257 | int linenum = 0; |
| 4258 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4259 | struct ckch_store *ckchs; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4260 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4261 | if ((f = fopen(file, "r")) == NULL) { |
| 4262 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4263 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4264 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4265 | |
| 4266 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4267 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4268 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4269 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4270 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4271 | char *crt_path; |
| 4272 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4273 | |
| 4274 | linenum++; |
| 4275 | end = line + strlen(line); |
| 4276 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 4277 | /* Check if we reached the limit and the last char is not \n. |
| 4278 | * Watch out for the last line without the terminating '\n'! |
| 4279 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4280 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 4281 | linenum, file, (int)sizeof(thisline)-1); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4282 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4283 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4286 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4287 | newarg = 1; |
| 4288 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4289 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 4290 | /* end of string, end of loop */ |
| 4291 | *line = 0; |
| 4292 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4293 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4294 | newarg = 1; |
| 4295 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4296 | } else if (*line == '[') { |
| 4297 | if (ssl_b) { |
| 4298 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4299 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4300 | break; |
| 4301 | } |
| 4302 | if (!arg) { |
| 4303 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4304 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4305 | break; |
| 4306 | } |
| 4307 | ssl_b = arg; |
| 4308 | newarg = 1; |
| 4309 | *line = 0; |
| 4310 | } else if (*line == ']') { |
| 4311 | if (ssl_e) { |
| 4312 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4313 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4314 | break; |
| 4315 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4316 | if (!ssl_b) { |
| 4317 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4318 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4319 | break; |
| 4320 | } |
| 4321 | ssl_e = arg; |
| 4322 | newarg = 1; |
| 4323 | *line = 0; |
| 4324 | } else if (newarg) { |
| 4325 | if (arg == MAX_CRT_ARGS) { |
| 4326 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4327 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4328 | break; |
| 4329 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4330 | newarg = 0; |
| 4331 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4332 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 4333 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4334 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 4335 | if (cfgerr) |
| 4336 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4337 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4338 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4339 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4340 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4341 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4342 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4343 | crt_path = args[0]; |
| 4344 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 4345 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 4346 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 4347 | crt_path, linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4348 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4349 | break; |
| 4350 | } |
| 4351 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 4352 | crt_path = path; |
| 4353 | } |
| 4354 | |
| 4355 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 4356 | cur_arg = ssl_b ? ssl_b : 1; |
| 4357 | while (cur_arg < ssl_e) { |
| 4358 | newarg = 0; |
| 4359 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 4360 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 4361 | newarg = 1; |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4362 | cfgerr |= ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4363 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 4364 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 4365 | args[cur_arg], linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4366 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4367 | } |
| 4368 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 4369 | break; |
| 4370 | } |
| 4371 | } |
| 4372 | if (!cfgerr && !newarg) { |
| 4373 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 4374 | args[cur_arg], linenum, file); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4375 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4376 | break; |
| 4377 | } |
| 4378 | } |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4379 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4380 | if (cfgerr) { |
| 4381 | ssl_sock_free_ssl_conf(ssl_conf); |
| 4382 | free(ssl_conf); |
| 4383 | ssl_conf = NULL; |
| 4384 | break; |
| 4385 | } |
| 4386 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4387 | if ((ckchs = ckchs_lookup(crt_path)) == NULL) { |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4388 | if (stat(crt_path, &buf) == 0) |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4389 | ckchs = ckchs_load_cert_file(crt_path, 0, err); |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 4390 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 4391 | ckchs = ckchs_load_cert_file(crt_path, 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 4392 | } |
| 4393 | |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4394 | if (!ckchs) |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 4395 | cfgerr |= ERR_ALERT | ERR_FATAL; |
Willy Tarreau | 8c5414a | 2019-10-16 17:06:25 +0200 | [diff] [blame] | 4396 | else |
| 4397 | cfgerr |= ssl_sock_load_ckchs(crt_path, ckchs, bind_conf, ssl_conf, &args[cur_arg], arg - cur_arg - 1, err); |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 4398 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4399 | if (cfgerr) { |
| 4400 | memprintf(err, "error processing line %d in file '%s' : %s", linenum, file, *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4401 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 4402 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4403 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 4404 | fclose(f); |
| 4405 | return cfgerr; |
| 4406 | } |
| 4407 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4408 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4409 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4410 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4411 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4412 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4413 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4414 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4415 | SSL_OP_NO_SSLv2 | |
| 4416 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 4417 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4418 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4419 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 4420 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 4421 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4422 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4423 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4424 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4425 | SSL_MODE_RELEASE_BUFFERS | |
| 4426 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 4427 | 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] | 4428 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4429 | int flags = MC_SSL_O_ALL; |
| 4430 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4431 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4432 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4433 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4434 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4435 | 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] | 4436 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 4437 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4438 | 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] | 4439 | else |
| 4440 | flags = conf_ssl_methods->flags; |
| 4441 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 4442 | min = conf_ssl_methods->min; |
| 4443 | max = conf_ssl_methods->max; |
| 4444 | /* start with TLSv10 to remove SSLv3 per default */ |
| 4445 | if (!min && (!max || max >= CONF_TLSV10)) |
| 4446 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4447 | /* 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] | 4448 | if (min) |
| 4449 | flags |= (methodVersions[min].flag - 1); |
| 4450 | if (max) |
| 4451 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4452 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4453 | min = max = CONF_TLSV_NONE; |
| 4454 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4455 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4456 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4457 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4458 | if (min) { |
| 4459 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4460 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 4461 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4462 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 4463 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4464 | hole = 0; |
| 4465 | } |
| 4466 | max = i; |
| 4467 | } |
| 4468 | else { |
| 4469 | min = max = i; |
| 4470 | } |
| 4471 | } |
| 4472 | else { |
| 4473 | if (min) |
| 4474 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4475 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4476 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4477 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4478 | 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] | 4479 | cfgerr += 1; |
| 4480 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4481 | /* save real min/max in bind_conf */ |
| 4482 | conf_ssl_methods->min = min; |
| 4483 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4484 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4485 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4486 | /* 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] | 4487 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4488 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4489 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4490 | else |
| 4491 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4492 | if (flags & methodVersions[i].flag) |
| 4493 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4494 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4495 | /* 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] | 4496 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4497 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 4498 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4499 | |
| 4500 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 4501 | options |= SSL_OP_NO_TICKET; |
| 4502 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 4503 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 4504 | |
| 4505 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 4506 | options |= SSL_OP_NO_RENEGOTIATION; |
| 4507 | #endif |
| 4508 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4509 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4510 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4511 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4512 | if (global_ssl.async) |
| 4513 | mode |= SSL_MODE_ASYNC; |
| 4514 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4515 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4516 | if (global_ssl.life_time) |
| 4517 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4518 | |
| 4519 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4520 | #ifdef OPENSSL_IS_BORINGSSL |
| 4521 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 4522 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4523 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 4524 | if (bind_conf->ssl_conf.early_data) { |
| 4525 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 4526 | SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite); |
| 4527 | } |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 4528 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 4529 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4530 | #else |
| 4531 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4532 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 4533 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4534 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4535 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4536 | } |
| 4537 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4538 | |
| 4539 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 4540 | { |
| 4541 | if (first == block) { |
| 4542 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4543 | if (first->len > 0) |
| 4544 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 4545 | } |
| 4546 | } |
| 4547 | |
| 4548 | /* return first block from sh_ssl_sess */ |
| 4549 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 4550 | { |
| 4551 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 4552 | |
| 4553 | } |
| 4554 | |
| 4555 | /* store a session into the cache |
| 4556 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 4557 | * data: asn1 encoded session |
| 4558 | * data_len: asn1 encoded session length |
| 4559 | * Returns 1 id session was stored (else 0) |
| 4560 | */ |
| 4561 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 4562 | { |
| 4563 | struct shared_block *first; |
| 4564 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 4565 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4566 | 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] | 4567 | if (!first) { |
| 4568 | /* Could not retrieve enough free blocks to store that session */ |
| 4569 | return 0; |
| 4570 | } |
| 4571 | |
| 4572 | /* STORE the key in the first elem */ |
| 4573 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4574 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 4575 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4576 | |
| 4577 | /* it returns the already existing node |
| 4578 | or current node if none, never returns null */ |
| 4579 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4580 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4581 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4582 | /* release the reserved row */ |
| 4583 | shctx_row_dec_hot(ssl_shctx, first); |
| 4584 | /* replace the previous session already in the tree */ |
| 4585 | sh_ssl_sess = oldsh_ssl_sess; |
| 4586 | /* ignore the previous session data, only use the header */ |
| 4587 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4588 | shctx_row_inc_hot(ssl_shctx, first); |
| 4589 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4590 | } |
| 4591 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4592 | 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] | 4593 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4594 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4595 | } |
| 4596 | |
| 4597 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4598 | |
| 4599 | return 1; |
| 4600 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4601 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4602 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4603 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4604 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4605 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4606 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4607 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4608 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4609 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4610 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4611 | int len; |
| 4612 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4613 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4614 | len = i2d_SSL_SESSION(sess, NULL); |
| 4615 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4616 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4617 | } else { |
| 4618 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4619 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 4620 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4621 | } |
| 4622 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4623 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4624 | &ptr); |
| 4625 | } |
| 4626 | } else { |
| 4627 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4628 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4629 | } |
| 4630 | |
| 4631 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4632 | } |
| 4633 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4634 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4635 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4636 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4637 | { |
| 4638 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4639 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4640 | unsigned char *p; |
| 4641 | int data_len; |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4642 | unsigned int sid_length; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4643 | const unsigned char *sid_data; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4644 | |
| 4645 | /* Session id is already stored in to key and session id is known |
| 4646 | * so we dont store it to keep size. |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4647 | * note: SSL_SESSION_set1_id is using |
| 4648 | * a memcpy so we need to use a different pointer |
| 4649 | * than sid_data or sid_ctx_data to avoid valgrind |
| 4650 | * complaining. |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4651 | */ |
| 4652 | |
| 4653 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4654 | |
| 4655 | /* copy value in an other buffer */ |
| 4656 | memcpy(encid, sid_data, sid_length); |
| 4657 | |
| 4658 | /* pad with 0 */ |
| 4659 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4660 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4661 | |
| 4662 | /* force length to zero to avoid ASN1 encoding */ |
| 4663 | SSL_SESSION_set1_id(sess, encid, 0); |
| 4664 | |
| 4665 | /* force length to zero to avoid ASN1 encoding */ |
| 4666 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, 0); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4667 | |
| 4668 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4669 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4670 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4671 | goto err; |
| 4672 | |
| 4673 | p = encsess; |
| 4674 | |
| 4675 | /* process ASN1 session encoding before the lock */ |
| 4676 | i2d_SSL_SESSION(sess, &p); |
| 4677 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4678 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4679 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4680 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4681 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4682 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4683 | err: |
| 4684 | /* reset original length values */ |
Emeric Brun | eb46965 | 2019-10-08 18:27:37 +0200 | [diff] [blame] | 4685 | SSL_SESSION_set1_id(sess, encid, sid_length); |
| 4686 | 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] | 4687 | |
| 4688 | return 0; /* do not increment session reference count */ |
| 4689 | } |
| 4690 | |
| 4691 | /* 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] | 4692 | 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] | 4693 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4694 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4695 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4696 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4697 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4698 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4699 | |
| 4700 | global.shctx_lookups++; |
| 4701 | |
| 4702 | /* allow the session to be freed automatically by openssl */ |
| 4703 | *do_copy = 0; |
| 4704 | |
| 4705 | /* tree key is zeros padded sessionid */ |
| 4706 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4707 | memcpy(tmpkey, key, key_len); |
| 4708 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4709 | key = tmpkey; |
| 4710 | } |
| 4711 | |
| 4712 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4713 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4714 | |
| 4715 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4716 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4717 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4718 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4719 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4720 | global.shctx_misses++; |
| 4721 | return NULL; |
| 4722 | } |
| 4723 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4724 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4725 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4726 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4727 | 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] | 4728 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4729 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4730 | |
| 4731 | /* decode ASN1 session */ |
| 4732 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4733 | 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] | 4734 | /* Reset session id and session id contenxt */ |
| 4735 | if (sess) { |
| 4736 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4737 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4738 | } |
| 4739 | |
| 4740 | return sess; |
| 4741 | } |
| 4742 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4743 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4744 | /* 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] | 4745 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4746 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4747 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4748 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4749 | unsigned int sid_length; |
| 4750 | const unsigned char *sid_data; |
| 4751 | (void)ctx; |
| 4752 | |
| 4753 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4754 | /* tree key is zeros padded sessionid */ |
| 4755 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4756 | memcpy(tmpkey, sid_data, sid_length); |
| 4757 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4758 | sid_data = tmpkey; |
| 4759 | } |
| 4760 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4761 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4762 | |
| 4763 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4764 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4765 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4766 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4767 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4771 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4772 | } |
| 4773 | |
| 4774 | /* Set session cache mode to server and disable openssl internal cache. |
| 4775 | * Set shared cache callbacks on an ssl context. |
| 4776 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4777 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4778 | { |
| 4779 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4780 | |
| 4781 | if (!ssl_shctx) { |
| 4782 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4783 | return; |
| 4784 | } |
| 4785 | |
| 4786 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4787 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4788 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4789 | |
| 4790 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4791 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4792 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4793 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4794 | } |
| 4795 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4796 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 4797 | { |
| 4798 | struct proxy *curproxy = bind_conf->frontend; |
| 4799 | int cfgerr = 0; |
| 4800 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4801 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4802 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4803 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4804 | const char *conf_ciphersuites; |
| 4805 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4806 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4807 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4808 | if (ssl_conf) { |
| 4809 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4810 | int i, min, max; |
| 4811 | int flags = MC_SSL_O_ALL; |
| 4812 | |
| 4813 | /* 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] | 4814 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4815 | 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] | 4816 | if (min) |
| 4817 | flags |= (methodVersions[min].flag - 1); |
| 4818 | if (max) |
| 4819 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4820 | min = max = CONF_TLSV_NONE; |
| 4821 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4822 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4823 | if (min) |
| 4824 | max = i; |
| 4825 | else |
| 4826 | min = max = i; |
| 4827 | } |
| 4828 | /* save real min/max */ |
| 4829 | conf_ssl_methods->min = min; |
| 4830 | conf_ssl_methods->max = max; |
| 4831 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4832 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4833 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4834 | cfgerr += 1; |
| 4835 | } |
| 4836 | } |
| 4837 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4838 | 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] | 4839 | case SSL_SOCK_VERIFY_NONE: |
| 4840 | verify = SSL_VERIFY_NONE; |
| 4841 | break; |
| 4842 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4843 | verify = SSL_VERIFY_PEER; |
| 4844 | break; |
| 4845 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4846 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4847 | break; |
| 4848 | } |
| 4849 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4850 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4851 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 4852 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 4853 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4854 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4855 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4856 | ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
| 4857 | curproxy->id, ca_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4858 | cfgerr++; |
| 4859 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4860 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4861 | /* set CA names for client cert request, function returns void */ |
| 4862 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4863 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4864 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4865 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4866 | ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4867 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4868 | cfgerr++; |
| 4869 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4870 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4871 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4872 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4873 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4874 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4875 | ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4876 | curproxy->id, crl_file, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4877 | cfgerr++; |
| 4878 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4879 | else { |
| 4880 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4881 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4882 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4883 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4884 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4885 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4886 | #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] | 4887 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4888 | if (!SSL_CTX_set_tlsext_ticket_key_cb(ctx, ssl_tlsext_ticket_key_cb)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4889 | ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4890 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4891 | cfgerr++; |
| 4892 | } |
| 4893 | } |
| 4894 | #endif |
| 4895 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4896 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4897 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4898 | if (conf_ciphers && |
| 4899 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4900 | ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4901 | curproxy->id, conf_ciphers, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4902 | cfgerr++; |
| 4903 | } |
| 4904 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4905 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4906 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4907 | if (conf_ciphersuites && |
| 4908 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
| 4909 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4910 | curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4911 | cfgerr++; |
| 4912 | } |
| 4913 | #endif |
| 4914 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4915 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4916 | /* If tune.ssl.default-dh-param has not been set, |
| 4917 | neither has ssl-default-dh-file and no static DH |
| 4918 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4919 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4920 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4921 | (ssl_dh_ptr_index == -1 || |
| 4922 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4923 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4924 | const SSL_CIPHER * cipher = NULL; |
| 4925 | char cipher_description[128]; |
| 4926 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4927 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4928 | which is not ephemeral DH. */ |
| 4929 | const char dhe_description[] = " Kx=DH "; |
| 4930 | const char dhe_export_description[] = " Kx=DH("; |
| 4931 | int idx = 0; |
| 4932 | int dhe_found = 0; |
| 4933 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4934 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4935 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4936 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4937 | if (ssl) { |
| 4938 | ciphers = SSL_get_ciphers(ssl); |
| 4939 | |
| 4940 | if (ciphers) { |
| 4941 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4942 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4943 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4944 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4945 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4946 | dhe_found = 1; |
| 4947 | break; |
| 4948 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4949 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4950 | } |
| 4951 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4952 | SSL_free(ssl); |
| 4953 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4954 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4955 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4956 | if (dhe_found) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4957 | ha_warning("Setting tune.ssl.default-dh-param to 1024 by default, if your workload permits it you should set it to at least 2048. Please set a value >= 1024 to make this warning disappear.\n"); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4958 | } |
| 4959 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4960 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4961 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4962 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4963 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4964 | if (local_dh_1024 == NULL) { |
| 4965 | local_dh_1024 = ssl_get_dh_1024(); |
| 4966 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4967 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4968 | if (local_dh_2048 == NULL) { |
| 4969 | local_dh_2048 = ssl_get_dh_2048(); |
| 4970 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4971 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4972 | if (local_dh_4096 == NULL) { |
| 4973 | local_dh_4096 = ssl_get_dh_4096(); |
| 4974 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4975 | } |
| 4976 | } |
| 4977 | } |
| 4978 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4979 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4980 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4981 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4982 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4983 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4984 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4985 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4986 | ssl_conf_cur = NULL; |
| 4987 | if (ssl_conf && ssl_conf->npn_str) |
| 4988 | ssl_conf_cur = ssl_conf; |
| 4989 | else if (bind_conf->ssl_conf.npn_str) |
| 4990 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4991 | if (ssl_conf_cur) |
| 4992 | 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] | 4993 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4994 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4995 | ssl_conf_cur = NULL; |
| 4996 | if (ssl_conf && ssl_conf->alpn_str) |
| 4997 | ssl_conf_cur = ssl_conf; |
| 4998 | else if (bind_conf->ssl_conf.alpn_str) |
| 4999 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 5000 | if (ssl_conf_cur) |
| 5001 | 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] | 5002 | #endif |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5003 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5004 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 5005 | if (conf_curves) { |
| 5006 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5007 | ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 5008 | curproxy->id, conf_curves, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5009 | cfgerr++; |
| 5010 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 5011 | #if defined(SSL_CTX_set_ecdh_auto) |
| 5012 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 5013 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5014 | } |
| 5015 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5016 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 5017 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5018 | int i; |
| 5019 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5020 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5021 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5022 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5023 | NULL); |
| 5024 | |
| 5025 | if (ecdhe == NULL) { |
Emeric Brun | a9363eb | 2019-10-17 14:53:03 +0200 | [diff] [blame] | 5026 | SSL_CTX_set_ecdh_auto(ctx, 1); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5027 | return cfgerr; |
| 5028 | } |
| 5029 | #else |
| 5030 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 5031 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 5032 | ECDHE_DEFAULT_CURVE); |
| 5033 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5034 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5035 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5036 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5037 | ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 5038 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 5039 | cfgerr++; |
| 5040 | } |
| 5041 | else { |
| 5042 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 5043 | EC_KEY_free(ecdh); |
| 5044 | } |
| 5045 | } |
| 5046 | #endif |
| 5047 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5048 | return cfgerr; |
| 5049 | } |
| 5050 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5051 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 5052 | { |
| 5053 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 5054 | size_t prefixlen, suffixlen; |
| 5055 | |
| 5056 | /* Trivial case */ |
| 5057 | if (strcmp(pattern, hostname) == 0) |
| 5058 | return 1; |
| 5059 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5060 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 5061 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 5062 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5063 | pattern_wildcard = NULL; |
| 5064 | pattern_left_label_end = pattern; |
| 5065 | while (*pattern_left_label_end != '.') { |
| 5066 | switch (*pattern_left_label_end) { |
| 5067 | case 0: |
| 5068 | /* End of label not found */ |
| 5069 | return 0; |
| 5070 | case '*': |
| 5071 | /* If there is more than one wildcards */ |
| 5072 | if (pattern_wildcard) |
| 5073 | return 0; |
| 5074 | pattern_wildcard = pattern_left_label_end; |
| 5075 | break; |
| 5076 | } |
| 5077 | pattern_left_label_end++; |
| 5078 | } |
| 5079 | |
| 5080 | /* If it's not trivial and there is no wildcard, it can't |
| 5081 | * match */ |
| 5082 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5083 | return 0; |
| 5084 | |
| 5085 | /* Make sure all labels match except the leftmost */ |
| 5086 | hostname_left_label_end = strchr(hostname, '.'); |
| 5087 | if (!hostname_left_label_end |
| 5088 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 5089 | return 0; |
| 5090 | |
| 5091 | /* Make sure the leftmost label of the hostname is long enough |
| 5092 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 5093 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5094 | return 0; |
| 5095 | |
| 5096 | /* Finally compare the string on either side of the |
| 5097 | * wildcard */ |
| 5098 | prefixlen = pattern_wildcard - pattern; |
| 5099 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 5100 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 5101 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5102 | return 0; |
| 5103 | |
| 5104 | return 1; |
| 5105 | } |
| 5106 | |
| 5107 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 5108 | { |
| 5109 | SSL *ssl; |
| 5110 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5111 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5112 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5113 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5114 | |
| 5115 | int depth; |
| 5116 | X509 *cert; |
| 5117 | STACK_OF(GENERAL_NAME) *alt_names; |
| 5118 | int i; |
| 5119 | X509_NAME *cert_subject; |
| 5120 | char *str; |
| 5121 | |
| 5122 | if (ok == 0) |
| 5123 | return ok; |
| 5124 | |
| 5125 | 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] | 5126 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5127 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5128 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 5129 | /* We're checking if the provided hostnames match the desired one. The |
| 5130 | * desired hostname comes from the SNI we presented if any, or if not |
| 5131 | * provided then it may have been explicitly stated using a "verifyhost" |
| 5132 | * directive. If neither is set, we don't care about the name so the |
| 5133 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5134 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5135 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5136 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5137 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5138 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5139 | if (!servername) |
| 5140 | return ok; |
| 5141 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5142 | |
| 5143 | /* We only need to verify the CN on the actual server cert, |
| 5144 | * not the indirect CAs */ |
| 5145 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 5146 | if (depth != 0) |
| 5147 | return ok; |
| 5148 | |
| 5149 | /* At this point, the cert is *not* OK unless we can find a |
| 5150 | * hostname match */ |
| 5151 | ok = 0; |
| 5152 | |
| 5153 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 5154 | /* It seems like this might happen if verify peer isn't set */ |
| 5155 | if (!cert) |
| 5156 | return ok; |
| 5157 | |
| 5158 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 5159 | if (alt_names) { |
| 5160 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 5161 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 5162 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5163 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5164 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 5165 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5166 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 5167 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5168 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5169 | OPENSSL_free(str); |
| 5170 | } |
| 5171 | } |
| 5172 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 5173 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5174 | } |
| 5175 | |
| 5176 | cert_subject = X509_get_subject_name(cert); |
| 5177 | i = -1; |
| 5178 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 5179 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5180 | ASN1_STRING *value; |
| 5181 | value = X509_NAME_ENTRY_get_data(entry); |
| 5182 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5183 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 5184 | OPENSSL_free(str); |
| 5185 | } |
| 5186 | } |
| 5187 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 5188 | /* report the mismatch and indicate if SNI was used or not */ |
| 5189 | if (!ok && !conn->err_code) |
| 5190 | 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] | 5191 | return ok; |
| 5192 | } |
| 5193 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5194 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5195 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5196 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5197 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5198 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5199 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5200 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 5201 | SSL_OP_NO_SSLv2 | |
| 5202 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 5203 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5204 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 5205 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 5206 | SSL_MODE_RELEASE_BUFFERS | |
| 5207 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5208 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5209 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5210 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5211 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5212 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5213 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5214 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 5215 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5216 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 5217 | cfgerr++; |
| 5218 | } |
| 5219 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5220 | /* Automatic memory computations need to know we use SSL there */ |
| 5221 | global.ssl_used_backend = 1; |
| 5222 | |
| 5223 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5224 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5225 | if ((srv->ssl_ctx.reused_sess = calloc(1, global.nbthread*sizeof(*srv->ssl_ctx.reused_sess))) == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5226 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 5227 | curproxy->id, srv->id, |
| 5228 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5229 | cfgerr++; |
| 5230 | return cfgerr; |
| 5231 | } |
| 5232 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5233 | if (srv->use_ssl) |
| 5234 | srv->xprt = &ssl_sock; |
| 5235 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 5236 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5237 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5238 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5239 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5240 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 5241 | proxy_type_str(curproxy), curproxy->id, |
| 5242 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5243 | cfgerr++; |
| 5244 | return cfgerr; |
| 5245 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5246 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5247 | 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] | 5248 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 5249 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5250 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5251 | else |
| 5252 | flags = conf_ssl_methods->flags; |
| 5253 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5254 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 5255 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5256 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5257 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5258 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5259 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5260 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5261 | min = max = CONF_TLSV_NONE; |
| 5262 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5263 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5264 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5265 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5266 | if (min) { |
| 5267 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5268 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 5269 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 5270 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 5271 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5272 | hole = 0; |
| 5273 | } |
| 5274 | max = i; |
| 5275 | } |
| 5276 | else { |
| 5277 | min = max = i; |
| 5278 | } |
| 5279 | } |
| 5280 | else { |
| 5281 | if (min) |
| 5282 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5283 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5284 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5285 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 5286 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5287 | cfgerr += 1; |
| 5288 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5289 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5290 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5291 | /* 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] | 5292 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5293 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 5294 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5295 | else |
| 5296 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 5297 | if (flags & methodVersions[i].flag) |
| 5298 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5299 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 5300 | /* 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] | 5301 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 5302 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 5303 | #endif |
| 5304 | |
| 5305 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 5306 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5307 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5308 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5309 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5310 | if (global_ssl.async) |
| 5311 | mode |= SSL_MODE_ASYNC; |
| 5312 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 5313 | SSL_CTX_set_mode(ctx, mode); |
| 5314 | srv->ssl_ctx.ctx = ctx; |
| 5315 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5316 | if (srv->ssl_ctx.client_crt) { |
| 5317 | if (SSL_CTX_use_PrivateKey_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt, SSL_FILETYPE_PEM) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5318 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 5319 | proxy_type_str(curproxy), curproxy->id, |
| 5320 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5321 | cfgerr++; |
| 5322 | } |
| 5323 | else if (SSL_CTX_use_certificate_chain_file(srv->ssl_ctx.ctx, srv->ssl_ctx.client_crt) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5324 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 5325 | proxy_type_str(curproxy), curproxy->id, |
| 5326 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5327 | cfgerr++; |
| 5328 | } |
| 5329 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5330 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 5331 | proxy_type_str(curproxy), curproxy->id, |
| 5332 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 5333 | cfgerr++; |
| 5334 | } |
| 5335 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5336 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5337 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 5338 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5339 | switch (srv->ssl_ctx.verify) { |
| 5340 | case SSL_SOCK_VERIFY_NONE: |
| 5341 | verify = SSL_VERIFY_NONE; |
| 5342 | break; |
| 5343 | case SSL_SOCK_VERIFY_REQUIRED: |
| 5344 | verify = SSL_VERIFY_PEER; |
| 5345 | break; |
| 5346 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5347 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5348 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 5349 | (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] | 5350 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5351 | if (srv->ssl_ctx.ca_file) { |
| 5352 | /* load CAfile to verify */ |
| 5353 | if (!SSL_CTX_load_verify_locations(srv->ssl_ctx.ctx, srv->ssl_ctx.ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5354 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
| 5355 | curproxy->id, srv->id, |
| 5356 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5357 | cfgerr++; |
| 5358 | } |
| 5359 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5360 | else { |
| 5361 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5362 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled by default but no CA file specified. If you're running on a LAN where you're certain to trust the server's certificate, please set an explicit 'verify none' statement on the 'server' line, or use 'ssl-server-verify none' in the global section to disable server-side verifications by default.\n", |
| 5363 | curproxy->id, srv->id, |
| 5364 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5365 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5366 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 5367 | curproxy->id, srv->id, |
| 5368 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 5369 | cfgerr++; |
| 5370 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5371 | #ifdef X509_V_FLAG_CRL_CHECK |
| 5372 | if (srv->ssl_ctx.crl_file) { |
| 5373 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 5374 | |
| 5375 | if (!store || !X509_STORE_load_locations(store, srv->ssl_ctx.crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5376 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 5377 | curproxy->id, srv->id, |
| 5378 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 5379 | cfgerr++; |
| 5380 | } |
| 5381 | else { |
| 5382 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 5383 | } |
| 5384 | } |
| 5385 | #endif |
| 5386 | } |
| 5387 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 5388 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 5389 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 5390 | SSL_CTX_sess_set_new_cb(srv->ssl_ctx.ctx, ssl_sess_new_srv_cb); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5391 | if (srv->ssl_ctx.ciphers && |
| 5392 | !SSL_CTX_set_cipher_list(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5393 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 5394 | curproxy->id, srv->id, |
| 5395 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5396 | cfgerr++; |
| 5397 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5398 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 5399 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5400 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 5401 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 5402 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 5403 | curproxy->id, srv->id, |
| 5404 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 5405 | cfgerr++; |
| 5406 | } |
| 5407 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5408 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 5409 | if (srv->ssl_ctx.npn_str) |
| 5410 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 5411 | #endif |
| 5412 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5413 | if (srv->ssl_ctx.alpn_str) |
| 5414 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 5415 | #endif |
| 5416 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 5417 | |
| 5418 | return cfgerr; |
| 5419 | } |
| 5420 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5421 | /* 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] | 5422 | * be NULL, in which case nothing is done. Returns the number of errors |
| 5423 | * encountered. |
| 5424 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5425 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5426 | { |
| 5427 | struct ebmb_node *node; |
| 5428 | struct sni_ctx *sni; |
| 5429 | int err = 0; |
| 5430 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 5431 | /* Automatic memory computations need to know we use SSL there */ |
| 5432 | global.ssl_used_frontend = 1; |
| 5433 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5434 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 5435 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5436 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5437 | err++; |
| 5438 | } |
| 5439 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 5440 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 5441 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5442 | /* It should not be necessary to call this function, but it's |
| 5443 | necessary first to check and move all initialisation related |
| 5444 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 5445 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 5446 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5447 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5448 | err += ssl_sock_prepare_ctx(bind_conf, bind_conf->default_ssl_conf, bind_conf->default_ctx); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5449 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5450 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5451 | while (node) { |
| 5452 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5453 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 5454 | /* only initialize the CTX on its first occurrence and |
| 5455 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5456 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5457 | node = ebmb_next(node); |
| 5458 | } |
| 5459 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5460 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5461 | while (node) { |
| 5462 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 5463 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 5464 | /* only initialize the CTX on its first occurrence and |
| 5465 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5466 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5467 | node = ebmb_next(node); |
| 5468 | } |
| 5469 | return err; |
| 5470 | } |
| 5471 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5472 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 5473 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 5474 | * alerts are directly emitted since the rest of the stack does it below. |
| 5475 | */ |
| 5476 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 5477 | { |
| 5478 | struct proxy *px = bind_conf->frontend; |
| 5479 | int alloc_ctx; |
| 5480 | int err; |
| 5481 | |
| 5482 | if (!bind_conf->is_ssl) { |
| 5483 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5484 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 5485 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5486 | } |
| 5487 | return 0; |
| 5488 | } |
| 5489 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5490 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5491 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 5492 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5493 | } |
| 5494 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5495 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 5496 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 5497 | return -1; |
| 5498 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5499 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 5500 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5501 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 5502 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5503 | sizeof(*sh_ssl_sess_tree), |
| 5504 | ((global.nbthread > 1) || (!global_ssl.private_cache && (global.nbproc > 1))) ? 1 : 0); |
Frédéric Lécaille | 4c8aa11 | 2018-10-25 20:22:46 +0200 | [diff] [blame] | 5505 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 5506 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 5507 | 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"); |
| 5508 | else |
| 5509 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 5510 | return -1; |
| 5511 | } |
| 5512 | /* free block callback */ |
| 5513 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 5514 | /* init the root tree within the extra space */ |
| 5515 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 5516 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5517 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 5518 | err = 0; |
| 5519 | /* initialize all certificate contexts */ |
| 5520 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 5521 | |
| 5522 | /* initialize CA variables if the certificates generation is enabled */ |
| 5523 | err += ssl_sock_load_ca(bind_conf); |
| 5524 | |
| 5525 | return -err; |
| 5526 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5527 | |
| 5528 | /* release ssl context allocated for servers. */ |
| 5529 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 5530 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5531 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5532 | if (srv->ssl_ctx.alpn_str) |
| 5533 | free(srv->ssl_ctx.alpn_str); |
| 5534 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 5535 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5536 | if (srv->ssl_ctx.npn_str) |
| 5537 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 5538 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5539 | if (srv->ssl_ctx.ctx) |
| 5540 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 5541 | } |
| 5542 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5543 | /* 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] | 5544 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 5545 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5546 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5547 | { |
| 5548 | struct ebmb_node *node, *back; |
| 5549 | struct sni_ctx *sni; |
| 5550 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5551 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5552 | while (node) { |
| 5553 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5554 | back = ebmb_next(node); |
| 5555 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5556 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5557 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5558 | ssl_sock_free_ssl_conf(sni->conf); |
| 5559 | free(sni->conf); |
| 5560 | sni->conf = NULL; |
| 5561 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5562 | free(sni); |
| 5563 | node = back; |
| 5564 | } |
| 5565 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5566 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5567 | while (node) { |
| 5568 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5569 | back = ebmb_next(node); |
| 5570 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5571 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5572 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5573 | ssl_sock_free_ssl_conf(sni->conf); |
| 5574 | free(sni->conf); |
| 5575 | sni->conf = NULL; |
| 5576 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5577 | free(sni); |
| 5578 | node = back; |
| 5579 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5580 | SSL_CTX_free(bind_conf->initial_ctx); |
| 5581 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5582 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5583 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5584 | } |
| 5585 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5586 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5587 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5588 | { |
| 5589 | ssl_sock_free_ca(bind_conf); |
| 5590 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5591 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5592 | free(bind_conf->ca_sign_file); |
| 5593 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5594 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5595 | free(bind_conf->keys_ref->filename); |
| 5596 | free(bind_conf->keys_ref->tlskeys); |
| 5597 | LIST_DEL(&bind_conf->keys_ref->list); |
| 5598 | free(bind_conf->keys_ref); |
| 5599 | } |
| 5600 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5601 | bind_conf->ca_sign_pass = NULL; |
| 5602 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5603 | } |
| 5604 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5605 | /* Load CA cert file and private key used to generate certificates */ |
| 5606 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5607 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5608 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5609 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5610 | FILE *fp; |
| 5611 | X509 *cacert = NULL; |
| 5612 | EVP_PKEY *capkey = NULL; |
| 5613 | int err = 0; |
| 5614 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 5615 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5616 | return err; |
| 5617 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5618 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5619 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5620 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5621 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5622 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5623 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5624 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5625 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5626 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5627 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5628 | "no CA certificate File configured at [%s:%d].\n", |
| 5629 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5630 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5631 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5632 | |
| 5633 | /* read in the CA certificate */ |
| 5634 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5635 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5636 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5637 | goto load_error; |
| 5638 | } |
| 5639 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5640 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5641 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5642 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5643 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5644 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5645 | if (!(capkey = PEM_read_PrivateKey(fp, NULL, NULL, bind_conf->ca_sign_pass))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5646 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 5647 | px->id, bind_conf->ca_sign_file, bind_conf->file, bind_conf->line); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5648 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5649 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5650 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5651 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5652 | bind_conf->ca_sign_cert = cacert; |
| 5653 | bind_conf->ca_sign_pkey = capkey; |
| 5654 | return err; |
| 5655 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5656 | read_error: |
| 5657 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5658 | if (capkey) EVP_PKEY_free(capkey); |
| 5659 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5660 | load_error: |
| 5661 | bind_conf->generate_certs = 0; |
| 5662 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5663 | return err; |
| 5664 | } |
| 5665 | |
| 5666 | /* Release CA cert and private key used to generate certificated */ |
| 5667 | void |
| 5668 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5669 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5670 | if (bind_conf->ca_sign_pkey) |
| 5671 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 5672 | if (bind_conf->ca_sign_cert) |
| 5673 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 5674 | bind_conf->ca_sign_pkey = NULL; |
| 5675 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5676 | } |
| 5677 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5678 | /* |
| 5679 | * This function is called if SSL * context is not yet allocated. The function |
| 5680 | * is designed to be called before any other data-layer operation and sets the |
| 5681 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5682 | * It returns 0 on success and -1 in error case. |
| 5683 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5684 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5685 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5686 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5687 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5688 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5689 | return 0; |
| 5690 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5691 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5692 | return 0; |
| 5693 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5694 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 5695 | if (!ctx) { |
| 5696 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5697 | return -1; |
| 5698 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5699 | ctx->wait_event.tasklet = tasklet_new(); |
| 5700 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5701 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5702 | pool_free(ssl_sock_ctx_pool, ctx); |
| 5703 | return -1; |
| 5704 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5705 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 5706 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5707 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5708 | ctx->sent_early_data = 0; |
| 5709 | ctx->tmp_early_data = -1; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5710 | ctx->conn = conn; |
Olivier Houchard | 81284e6 | 2019-06-06 13:21:23 +0200 | [diff] [blame] | 5711 | ctx->send_wait = NULL; |
| 5712 | ctx->recv_wait = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 5713 | ctx->xprt_st = 0; |
| 5714 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5715 | |
| 5716 | /* Only work with sockets for now, this should be adapted when we'll |
| 5717 | * add QUIC support. |
| 5718 | */ |
| 5719 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5720 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5721 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 5722 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5723 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5724 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5725 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 5726 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5727 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5728 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5729 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5730 | /* If it is in client mode initiate SSL session |
| 5731 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5732 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5733 | int may_retry = 1; |
| 5734 | |
| 5735 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5736 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5737 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 5738 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5739 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5740 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5741 | goto retry_connect; |
| 5742 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5743 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5744 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5745 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5746 | ctx->bio = BIO_new(ha_meth); |
| 5747 | if (!ctx->bio) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5748 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5749 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5750 | goto retry_connect; |
| 5751 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5752 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5753 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5754 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5755 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5756 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5757 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5758 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5759 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5760 | SSL_free(ctx->ssl); |
| 5761 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5762 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5763 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5764 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5765 | goto retry_connect; |
| 5766 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5767 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5768 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5769 | } |
| 5770 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5771 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5772 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5773 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 5774 | 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] | 5775 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5776 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5777 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5778 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5779 | } else if (sess) { |
| 5780 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5781 | } |
| 5782 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5783 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5784 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5785 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5786 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5787 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5788 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5789 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5790 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5791 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5792 | if (conn->flags & CO_FL_ERROR) |
| 5793 | goto err; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5794 | return 0; |
| 5795 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5796 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5797 | int may_retry = 1; |
| 5798 | |
| 5799 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5800 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5801 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 5802 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5803 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5804 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5805 | goto retry_accept; |
| 5806 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5807 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5808 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5809 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5810 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5811 | ctx->bio = BIO_new(ha_meth); |
| 5812 | if (!ctx->bio) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5813 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5814 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5815 | goto retry_accept; |
| 5816 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5817 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5818 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5819 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5820 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5821 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5822 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5823 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5824 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5825 | SSL_free(ctx->ssl); |
| 5826 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5827 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5828 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5829 | goto retry_accept; |
| 5830 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5831 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5832 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5833 | } |
| 5834 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5835 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5836 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5837 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5838 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5839 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5840 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5841 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5842 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5843 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5844 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5845 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5846 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5847 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5848 | if (conn->flags & CO_FL_ERROR) |
| 5849 | goto err; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5850 | return 0; |
| 5851 | } |
| 5852 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5853 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5854 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5855 | if (ctx && ctx->wait_event.tasklet) |
| 5856 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5857 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5858 | return -1; |
| 5859 | } |
| 5860 | |
| 5861 | |
| 5862 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5863 | * updates the FD status if it wants some polling before being called again. |
| 5864 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5865 | * otherwise it returns non-zero and removes itself from the connection's |
| 5866 | * flags (the bit is provided in <flag> by the caller). |
| 5867 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5868 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5869 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5870 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5871 | int ret; |
| 5872 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5873 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5874 | return 0; |
| 5875 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5876 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5877 | goto out_error; |
| 5878 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5879 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5880 | /* |
| 5881 | * Check if we have early data. If we do, we have to read them |
| 5882 | * before SSL_do_handshake() is called, And there's no way to |
| 5883 | * detect early data, except to try to read them |
| 5884 | */ |
| 5885 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5886 | size_t read_data; |
| 5887 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5888 | ret = SSL_read_early_data(ctx->ssl, &ctx->tmp_early_data, |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5889 | 1, &read_data); |
| 5890 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5891 | goto check_error; |
| 5892 | if (ret == SSL_READ_EARLY_DATA_SUCCESS) { |
| 5893 | conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN); |
| 5894 | return 1; |
| 5895 | } else |
| 5896 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5897 | } |
| 5898 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5899 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5900 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5901 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5902 | * the reneg handshake. |
| 5903 | * Here we use SSL_peek as a workaround for reneg. |
| 5904 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5905 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5906 | char c; |
| 5907 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5908 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5909 | if (ret <= 0) { |
| 5910 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5911 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5912 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5913 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5914 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5915 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5916 | 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] | 5917 | return 0; |
| 5918 | } |
| 5919 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5920 | /* handshake may have been completed but we have |
| 5921 | * no more data to read. |
| 5922 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5923 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5924 | ret = 1; |
| 5925 | goto reneg_ok; |
| 5926 | } |
| 5927 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5928 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5929 | 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] | 5930 | return 0; |
| 5931 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5932 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5933 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5934 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5935 | return 0; |
| 5936 | } |
| 5937 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5938 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5939 | /* if errno is null, then connection was successfully established */ |
| 5940 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5941 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5942 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5943 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5944 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5945 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5946 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5947 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5948 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5949 | /* 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] | 5950 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5951 | empty_handshake = state == TLS_ST_BEFORE; |
| 5952 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5953 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5954 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5955 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5956 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5957 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5958 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5959 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5960 | else |
| 5961 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5962 | } |
| 5963 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5964 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5965 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5966 | else |
| 5967 | conn->err_code = CO_ER_SSL_ABORT; |
| 5968 | } |
| 5969 | } |
| 5970 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5971 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5972 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5973 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5974 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5975 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5976 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5977 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5978 | goto out_error; |
| 5979 | } |
| 5980 | else { |
| 5981 | /* Fail on all other handshake errors */ |
| 5982 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5983 | * buffer, causing an RST to be emitted upon close() on |
| 5984 | * TCP sockets. We first try to drain possibly pending |
| 5985 | * data to avoid this as much as possible. |
| 5986 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5987 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5988 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5989 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5990 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5991 | goto out_error; |
| 5992 | } |
| 5993 | } |
| 5994 | /* read some data: consider handshake completed */ |
| 5995 | goto reneg_ok; |
| 5996 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5997 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5998 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5999 | if (ret != 1) { |
| 6000 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6001 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6002 | |
| 6003 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 6004 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 6005 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6006 | 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] | 6007 | return 0; |
| 6008 | } |
| 6009 | else if (ret == SSL_ERROR_WANT_READ) { |
| 6010 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6011 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6012 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6013 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6014 | return 0; |
| 6015 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6016 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6017 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6018 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6019 | return 0; |
| 6020 | } |
| 6021 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6022 | else if (ret == SSL_ERROR_SYSCALL) { |
| 6023 | /* if errno is null, then connection was successfully established */ |
| 6024 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 6025 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6026 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6027 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 6028 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6029 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 6030 | #else |
| 6031 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6032 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6033 | /* 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] | 6034 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6035 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6036 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6037 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 6038 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6039 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6040 | if (empty_handshake) { |
| 6041 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6042 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6043 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6044 | else |
| 6045 | conn->err_code = CO_ER_SSL_EMPTY; |
| 6046 | } |
| 6047 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6048 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6049 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6050 | else |
| 6051 | conn->err_code = CO_ER_SSL_ABORT; |
| 6052 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6053 | } |
| 6054 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6055 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6056 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 6057 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 6058 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6059 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 6060 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 6061 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 6062 | goto out_error; |
| 6063 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6064 | else { |
| 6065 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 6066 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 6067 | * buffer, causing an RST to be emitted upon close() on |
| 6068 | * TCP sockets. We first try to drain possibly pending |
| 6069 | * data to avoid this as much as possible. |
| 6070 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 6071 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6072 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6073 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 6074 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6075 | goto out_error; |
| 6076 | } |
| 6077 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6078 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6079 | else { |
| 6080 | /* |
| 6081 | * If the server refused the early data, we have to send a |
| 6082 | * 425 to the client, as we no longer have the data to sent |
| 6083 | * them again. |
| 6084 | */ |
| 6085 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6086 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6087 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 6088 | goto out_error; |
| 6089 | } |
| 6090 | } |
| 6091 | } |
| 6092 | #endif |
| 6093 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6094 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 6095 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6096 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6097 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6098 | /* ASYNC engine API doesn't support moving read/write |
| 6099 | * buffers. So we disable ASYNC mode right after |
| 6100 | * the handshake to avoid buffer oveflows. |
| 6101 | */ |
| 6102 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6103 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6104 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6105 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6106 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6107 | if (objt_server(conn->target)) { |
| 6108 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 6109 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 6110 | 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] | 6111 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 6112 | else { |
| 6113 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 6114 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 6115 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 6116 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6117 | } |
| 6118 | |
| 6119 | /* The connection is now established at both layers, it's time to leave */ |
| 6120 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 6121 | return 1; |
| 6122 | |
| 6123 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6124 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6125 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6126 | ERR_clear_error(); |
| 6127 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6128 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 6129 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 6130 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 6131 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 6132 | } |
| 6133 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6134 | /* Fail on all other handshake errors */ |
| 6135 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 6136 | if (!conn->err_code) |
| 6137 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6138 | return 0; |
| 6139 | } |
| 6140 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6141 | static int ssl_subscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param) |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6142 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6143 | struct wait_event *sw; |
| 6144 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6145 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 6146 | if (!ctx) |
| 6147 | return -1; |
| 6148 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6149 | if (event_type & SUB_RETRY_RECV) { |
| 6150 | sw = param; |
| 6151 | BUG_ON(ctx->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 6152 | sw->events |= SUB_RETRY_RECV; |
| 6153 | ctx->recv_wait = sw; |
| 6154 | if (!(conn->flags & CO_FL_SSL_WAIT_HS) && |
| 6155 | !(ctx->wait_event.events & SUB_RETRY_RECV)) |
| 6156 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
| 6157 | event_type &= ~SUB_RETRY_RECV; |
| 6158 | } |
| 6159 | if (event_type & SUB_RETRY_SEND) { |
| 6160 | sw = param; |
| 6161 | BUG_ON(ctx->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 6162 | sw->events |= SUB_RETRY_SEND; |
| 6163 | ctx->send_wait = sw; |
| 6164 | if (!(conn->flags & CO_FL_SSL_WAIT_HS) && |
| 6165 | !(ctx->wait_event.events & SUB_RETRY_SEND)) |
| 6166 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
| 6167 | event_type &= ~SUB_RETRY_SEND; |
| 6168 | |
| 6169 | } |
| 6170 | if (event_type != 0) |
| 6171 | return -1; |
| 6172 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6173 | } |
| 6174 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6175 | static int ssl_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, void *param) |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6176 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6177 | struct wait_event *sw; |
| 6178 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6179 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6180 | if (event_type & SUB_RETRY_RECV) { |
| 6181 | sw = param; |
| 6182 | BUG_ON(ctx->recv_wait != sw); |
| 6183 | ctx->recv_wait = NULL; |
| 6184 | sw->events &= ~SUB_RETRY_RECV; |
| 6185 | /* If we subscribed, and we're not doing the handshake, |
| 6186 | * then we subscribed because the upper layer asked for it, |
| 6187 | * as the upper layer is no longer interested, we can |
| 6188 | * unsubscribe too. |
| 6189 | */ |
| 6190 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) && |
| 6191 | (ctx->wait_event.events & SUB_RETRY_RECV)) |
| 6192 | conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, |
| 6193 | &ctx->wait_event); |
| 6194 | } |
| 6195 | if (event_type & SUB_RETRY_SEND) { |
| 6196 | sw = param; |
| 6197 | BUG_ON(ctx->send_wait != sw); |
| 6198 | ctx->send_wait = NULL; |
| 6199 | sw->events &= ~SUB_RETRY_SEND; |
| 6200 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) && |
| 6201 | (ctx->wait_event.events & SUB_RETRY_SEND)) |
| 6202 | conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, |
| 6203 | &ctx->wait_event); |
| 6204 | |
| 6205 | } |
| 6206 | |
| 6207 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 6208 | } |
| 6209 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 6210 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 6211 | * Returns 0 on success, and non-zero on failure. |
| 6212 | */ |
| 6213 | 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) |
| 6214 | { |
| 6215 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6216 | |
| 6217 | if (oldxprt_ops != NULL) |
| 6218 | *oldxprt_ops = ctx->xprt; |
| 6219 | if (oldxprt_ctx != NULL) |
| 6220 | *oldxprt_ctx = ctx->xprt_ctx; |
| 6221 | ctx->xprt = toadd_ops; |
| 6222 | ctx->xprt_ctx = toadd_ctx; |
| 6223 | return 0; |
| 6224 | } |
| 6225 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 6226 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 6227 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 6228 | * XPRT. |
| 6229 | */ |
| 6230 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 6231 | { |
| 6232 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6233 | |
| 6234 | if (ctx->xprt_ctx == toremove_ctx) { |
| 6235 | ctx->xprt_ctx = newctx; |
| 6236 | ctx->xprt = newops; |
| 6237 | return 0; |
| 6238 | } |
| 6239 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 6240 | } |
| 6241 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6242 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 6243 | { |
| 6244 | struct ssl_sock_ctx *ctx = context; |
| 6245 | |
| 6246 | /* First if we're doing an handshake, try that */ |
| 6247 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 6248 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 6249 | /* If we had an error, or the handshake is done and I/O is available, |
| 6250 | * let the upper layer know. |
| 6251 | * If no mux was set up yet, and nobody subscribed, then call |
| 6252 | * xprt_done_cb() ourself if it's set, or destroy the connection, |
| 6253 | * we can't be sure conn_fd_handler() will be called again. |
| 6254 | */ |
| 6255 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 6256 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 6257 | int ret = 0; |
| 6258 | int woke = 0; |
| 6259 | |
| 6260 | /* On error, wake any waiter */ |
| 6261 | if (ctx->recv_wait) { |
| 6262 | ctx->recv_wait->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6263 | tasklet_wakeup(ctx->recv_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6264 | ctx->recv_wait = NULL; |
| 6265 | woke = 1; |
| 6266 | } |
| 6267 | if (ctx->send_wait) { |
| 6268 | ctx->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6269 | tasklet_wakeup(ctx->send_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6270 | ctx->send_wait = NULL; |
| 6271 | woke = 1; |
| 6272 | } |
| 6273 | /* If we're the first xprt for the connection, let the |
| 6274 | * upper layers know. If xprt_done_cb() is set, call it, |
| 6275 | * otherwise, we should have a mux, so call its wake |
| 6276 | * method if we didn't woke a tasklet already. |
| 6277 | */ |
| 6278 | if (ctx->conn->xprt_ctx == ctx) { |
| 6279 | if (ctx->conn->xprt_done_cb) |
| 6280 | ret = ctx->conn->xprt_done_cb(ctx->conn); |
| 6281 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 6282 | ctx->conn->mux->wake(ctx->conn); |
| 6283 | return NULL; |
| 6284 | } |
| 6285 | } |
| 6286 | return NULL; |
| 6287 | } |
| 6288 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6289 | /* 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] | 6290 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6291 | * buffer wraps, in which case a second call may be performed. The connection's |
| 6292 | * flags are updated with whatever special event is detected (error, read0, |
| 6293 | * empty). The caller is responsible for taking care of those events and |
| 6294 | * avoiding the call if inappropriate. The function does not call the |
| 6295 | * connection's polling update function, so the caller is responsible for this. |
| 6296 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6297 | 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] | 6298 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6299 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 6300 | ssize_t ret; |
| 6301 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6302 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6303 | conn_refresh_polling_flags(conn); |
| 6304 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6305 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6306 | goto out_error; |
| 6307 | |
| 6308 | if (conn->flags & CO_FL_HANDSHAKE) |
| 6309 | /* a handshake was requested */ |
| 6310 | return 0; |
| 6311 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6312 | /* read the largest possible block. For this, we perform only one call |
| 6313 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 6314 | * in which case we accept to do it once again. A new attempt is made on |
| 6315 | * EINTR too. |
| 6316 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 6317 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6318 | int need_out = 0; |
| 6319 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6320 | try = b_contig_space(buf); |
| 6321 | if (!try) |
| 6322 | break; |
| 6323 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6324 | if (try > count) |
| 6325 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 6326 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6327 | if (((conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_EARLY_DATA)) == CO_FL_EARLY_SSL_HS) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6328 | ctx->tmp_early_data != -1) { |
| 6329 | *b_tail(buf) = ctx->tmp_early_data; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6330 | done++; |
| 6331 | try--; |
| 6332 | count--; |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6333 | b_add(buf, 1); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6334 | ctx->tmp_early_data = -1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6335 | continue; |
| 6336 | } |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 6337 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6338 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6339 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 6340 | size_t read_length; |
| 6341 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6342 | ret = SSL_read_early_data(ctx->ssl, |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 6343 | b_tail(buf), try, &read_length); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6344 | if (ret == SSL_READ_EARLY_DATA_SUCCESS && |
| 6345 | read_length > 0) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6346 | conn->flags |= CO_FL_EARLY_DATA; |
| 6347 | if (ret == SSL_READ_EARLY_DATA_SUCCESS || |
| 6348 | ret == SSL_READ_EARLY_DATA_FINISH) { |
| 6349 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 6350 | /* |
| 6351 | * We're done reading the early data, |
| 6352 | * let's make the handshake |
| 6353 | */ |
| 6354 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 6355 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 6356 | need_out = 1; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6357 | /* Now initiate the handshake */ |
| 6358 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6359 | if (read_length == 0) |
| 6360 | break; |
| 6361 | } |
| 6362 | ret = read_length; |
| 6363 | } |
| 6364 | } else |
| 6365 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6366 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 6367 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6368 | if (conn->flags & CO_FL_ERROR) { |
| 6369 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6370 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6371 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6372 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 6373 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6374 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6375 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6376 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6377 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6378 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6379 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6380 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6381 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6382 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6383 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6384 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6385 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6386 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6387 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6388 | break; |
| 6389 | } |
| 6390 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6391 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6392 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6393 | SUB_RETRY_RECV, |
| 6394 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6395 | /* handshake is running, and it may need to re-enable read */ |
| 6396 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6397 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6398 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6399 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6400 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6401 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6402 | break; |
| 6403 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6404 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6405 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 6406 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6407 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 6408 | * stack before shutting down the connection for |
| 6409 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6410 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 6411 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6412 | /* otherwise it's a real error */ |
| 6413 | goto out_error; |
| 6414 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6415 | if (need_out) |
| 6416 | break; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6417 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6418 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6419 | return done; |
| 6420 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6421 | clear_ssl_error: |
| 6422 | /* Clear openssl global errors stack */ |
| 6423 | ssl_sock_dump_errors(conn); |
| 6424 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6425 | read0: |
| 6426 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6427 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 6428 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6429 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 6430 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6431 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6432 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6433 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6434 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6435 | } |
| 6436 | |
| 6437 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6438 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 6439 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 6440 | * 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] | 6441 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 6442 | * a second call may be performed. The connection's flags are updated with |
| 6443 | * whatever special event is detected (error, empty). The caller is responsible |
| 6444 | * for taking care of those events and avoiding the call if inappropriate. The |
| 6445 | * 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] | 6446 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 6447 | * caller to take care of this. It's up to the caller to update the buffer's |
| 6448 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6449 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6450 | 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] | 6451 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6452 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6453 | ssize_t ret; |
| 6454 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6455 | |
| 6456 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6457 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6458 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6459 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6460 | goto out_error; |
| 6461 | |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6462 | if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6463 | /* a handshake was requested */ |
| 6464 | return 0; |
| 6465 | |
| 6466 | /* send the largest possible block. For this we perform only one call |
| 6467 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 6468 | * in which case we accept to do it once again. |
| 6469 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6470 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6471 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6472 | size_t written_data; |
| 6473 | #endif |
| 6474 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6475 | try = b_contig_data(buf, done); |
| 6476 | if (try > count) |
| 6477 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6478 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 6479 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6480 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 6481 | global_ssl.max_record && try > global_ssl.max_record) { |
| 6482 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6483 | } |
| 6484 | else { |
| 6485 | /* we need to keep the information about the fact that |
| 6486 | * we're not limiting the upcoming send(), because if it |
| 6487 | * fails, we'll have to retry with at least as many data. |
| 6488 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6489 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6490 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 6491 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6492 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6493 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6494 | unsigned int max_early; |
| 6495 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6496 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6497 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6498 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6499 | if (SSL_get0_session(ctx->ssl)) |
| 6500 | 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] | 6501 | else |
| 6502 | max_early = 0; |
| 6503 | } |
| 6504 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6505 | if (try + ctx->sent_early_data > max_early) { |
| 6506 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6507 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 6508 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6509 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6510 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6511 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6512 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6513 | 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] | 6514 | if (ret == 1) { |
| 6515 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6516 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 6517 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6518 | 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] | 6519 | /* Initiate the handshake, now */ |
| 6520 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 6521 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 6522 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 6523 | } |
| 6524 | |
| 6525 | } else |
| 6526 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6527 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 6528 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6529 | if (conn->flags & CO_FL_ERROR) { |
| 6530 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6531 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 6532 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6533 | if (ret > 0) { |
Olivier Houchard | f24502b | 2019-01-17 19:09:11 +0100 | [diff] [blame] | 6534 | /* A send succeeded, so we can consier ourself connected */ |
| 6535 | conn->flags |= CO_FL_CONNECTED; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6536 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 6537 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6538 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6539 | } |
| 6540 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6541 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6542 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6543 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6544 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6545 | /* handshake is running, and it may need to re-enable write */ |
| 6546 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6547 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6548 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6549 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6550 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6551 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6552 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 6553 | break; |
| 6554 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6555 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6556 | break; |
| 6557 | } |
| 6558 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 6559 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6560 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6561 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 6562 | SUB_RETRY_RECV, |
| 6563 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6564 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6565 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 6566 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6567 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 6568 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6569 | break; |
| 6570 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6571 | goto out_error; |
| 6572 | } |
| 6573 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6574 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6575 | return done; |
| 6576 | |
| 6577 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6578 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6579 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6580 | ERR_clear_error(); |
| 6581 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6582 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 6583 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6584 | } |
| 6585 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6586 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6587 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6588 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6589 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6590 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6591 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6592 | if (ctx->wait_event.events != 0) |
| 6593 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 6594 | ctx->wait_event.events, |
| 6595 | &ctx->wait_event); |
| 6596 | if (ctx->send_wait) { |
| 6597 | ctx->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6598 | tasklet_wakeup(ctx->send_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6599 | } |
| 6600 | if (ctx->recv_wait) { |
| 6601 | ctx->recv_wait->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6602 | tasklet_wakeup(ctx->recv_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6603 | } |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 6604 | if (ctx->xprt->close) |
| 6605 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6606 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6607 | if (global_ssl.async) { |
| 6608 | OSSL_ASYNC_FD all_fd[32], afd; |
| 6609 | size_t num_all_fds = 0; |
| 6610 | int i; |
| 6611 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6612 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6613 | if (num_all_fds > 32) { |
| 6614 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 6615 | return; |
| 6616 | } |
| 6617 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6618 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6619 | |
| 6620 | /* If an async job is pending, we must try to |
| 6621 | to catch the end using polling before calling |
| 6622 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6623 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6624 | for (i=0 ; i < num_all_fds ; i++) { |
| 6625 | /* switch on an handler designed to |
| 6626 | * handle the SSL_free |
| 6627 | */ |
| 6628 | afd = all_fd[i]; |
| 6629 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6630 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6631 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 6632 | /* To ensure that the fd cache won't be used |
| 6633 | * and we'll catch a real RD event. |
| 6634 | */ |
| 6635 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6636 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6637 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6638 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6639 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6640 | return; |
| 6641 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6642 | /* Else we can remove the fds from the fdtab |
| 6643 | * and call SSL_free. |
| 6644 | * note: we do a fd_remove and not a delete |
| 6645 | * because the fd is owned by the engine. |
| 6646 | * the engine is responsible to close |
| 6647 | */ |
| 6648 | for (i=0 ; i < num_all_fds ; i++) |
| 6649 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6650 | } |
| 6651 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6652 | SSL_free(ctx->ssl); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6653 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6654 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6655 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6656 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6657 | } |
| 6658 | |
| 6659 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 6660 | * any case, flags the connection as reusable if no handshake was in progress. |
| 6661 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6662 | 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] | 6663 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6664 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6665 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6666 | if (conn->flags & CO_FL_HANDSHAKE) |
| 6667 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 6668 | if (!clean) |
| 6669 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6670 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6671 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6672 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6673 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6674 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6675 | ERR_clear_error(); |
| 6676 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6677 | } |
| 6678 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6679 | /* used for ppv2 pkey alog (can be used for logging) */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6680 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6681 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6682 | struct ssl_sock_ctx *ctx; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6683 | struct pkey_info *pkinfo; |
| 6684 | int bits = 0; |
| 6685 | int sig = TLSEXT_signature_anonymous; |
| 6686 | int len = -1; |
| 6687 | |
| 6688 | if (!ssl_sock_is_ssl(conn)) |
| 6689 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6690 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6691 | pkinfo = SSL_CTX_get_ex_data(SSL_get_SSL_CTX(ctx->ssl), ssl_pkey_info_index); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6692 | if (pkinfo) { |
| 6693 | sig = pkinfo->sig; |
| 6694 | bits = pkinfo->bits; |
| 6695 | } else { |
| 6696 | /* multicert and generated cert have no pkey info */ |
| 6697 | X509 *crt; |
| 6698 | EVP_PKEY *pkey; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6699 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6700 | if (!crt) |
| 6701 | return 0; |
| 6702 | pkey = X509_get_pubkey(crt); |
| 6703 | if (pkey) { |
| 6704 | bits = EVP_PKEY_bits(pkey); |
| 6705 | switch(EVP_PKEY_base_id(pkey)) { |
| 6706 | case EVP_PKEY_RSA: |
| 6707 | sig = TLSEXT_signature_rsa; |
| 6708 | break; |
| 6709 | case EVP_PKEY_EC: |
| 6710 | sig = TLSEXT_signature_ecdsa; |
| 6711 | break; |
| 6712 | case EVP_PKEY_DSA: |
| 6713 | sig = TLSEXT_signature_dsa; |
| 6714 | break; |
| 6715 | } |
| 6716 | EVP_PKEY_free(pkey); |
| 6717 | } |
| 6718 | } |
| 6719 | |
| 6720 | switch(sig) { |
| 6721 | case TLSEXT_signature_rsa: |
| 6722 | len = chunk_printf(out, "RSA%d", bits); |
| 6723 | break; |
| 6724 | case TLSEXT_signature_ecdsa: |
| 6725 | len = chunk_printf(out, "EC%d", bits); |
| 6726 | break; |
| 6727 | case TLSEXT_signature_dsa: |
| 6728 | len = chunk_printf(out, "DSA%d", bits); |
| 6729 | break; |
| 6730 | default: |
| 6731 | return 0; |
| 6732 | } |
| 6733 | if (len < 0) |
| 6734 | return 0; |
| 6735 | return 1; |
| 6736 | } |
| 6737 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6738 | /* used for ppv2 cert signature (can be used for logging) */ |
| 6739 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 6740 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6741 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6742 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6743 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 6744 | X509 *crt; |
| 6745 | |
| 6746 | if (!ssl_sock_is_ssl(conn)) |
| 6747 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6748 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6749 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6750 | if (!crt) |
| 6751 | return NULL; |
| 6752 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6753 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6754 | } |
| 6755 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6756 | /* used for ppv2 authority */ |
| 6757 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6758 | { |
| 6759 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6760 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6761 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6762 | if (!ssl_sock_is_ssl(conn)) |
| 6763 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6764 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6765 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6766 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6767 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6768 | #endif |
| 6769 | } |
| 6770 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6771 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6772 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6773 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6774 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6775 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6776 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6777 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6778 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6779 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6780 | } |
| 6781 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6782 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6783 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 6784 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6785 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6786 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6787 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6788 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6789 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6790 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6791 | } |
| 6792 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6793 | /* Extract a serial from a cert, and copy it to a chunk. |
| 6794 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 6795 | * -1 if output is not large enough. |
| 6796 | */ |
| 6797 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6798 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6799 | { |
| 6800 | ASN1_INTEGER *serial; |
| 6801 | |
| 6802 | serial = X509_get_serialNumber(crt); |
| 6803 | if (!serial) |
| 6804 | return 0; |
| 6805 | |
| 6806 | if (out->size < serial->length) |
| 6807 | return -1; |
| 6808 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6809 | memcpy(out->area, serial->data, serial->length); |
| 6810 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6811 | return 1; |
| 6812 | } |
| 6813 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6814 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 6815 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 6816 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6817 | */ |
| 6818 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6819 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6820 | { |
| 6821 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6822 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6823 | |
| 6824 | len =i2d_X509(crt, NULL); |
| 6825 | if (len <= 0) |
| 6826 | return 1; |
| 6827 | |
| 6828 | if (out->size < len) |
| 6829 | return -1; |
| 6830 | |
| 6831 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6832 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6833 | return 1; |
| 6834 | } |
| 6835 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6836 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6837 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6838 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 6839 | * and -1 if output is not large enough. |
| 6840 | */ |
| 6841 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6842 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6843 | { |
| 6844 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 6845 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 6846 | |
| 6847 | if (gentm->length < 12) |
| 6848 | return 0; |
| 6849 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 6850 | return 0; |
| 6851 | if (out->size < gentm->length-2) |
| 6852 | return -1; |
| 6853 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6854 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 6855 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6856 | return 1; |
| 6857 | } |
| 6858 | else if (tm->type == V_ASN1_UTCTIME) { |
| 6859 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 6860 | |
| 6861 | if (utctm->length < 10) |
| 6862 | return 0; |
| 6863 | if (utctm->data[0] >= 0x35) |
| 6864 | return 0; |
| 6865 | if (out->size < utctm->length) |
| 6866 | return -1; |
| 6867 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6868 | memcpy(out->area, utctm->data, utctm->length); |
| 6869 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6870 | return 1; |
| 6871 | } |
| 6872 | |
| 6873 | return 0; |
| 6874 | } |
| 6875 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6876 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 6877 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 6878 | */ |
| 6879 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6880 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 6881 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6882 | { |
| 6883 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6884 | ASN1_OBJECT *obj; |
| 6885 | ASN1_STRING *data; |
| 6886 | const unsigned char *data_ptr; |
| 6887 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6888 | int i, j, n; |
| 6889 | int cur = 0; |
| 6890 | const char *s; |
| 6891 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6892 | int name_count; |
| 6893 | |
| 6894 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6895 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6896 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6897 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6898 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6899 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6900 | else |
| 6901 | j = i; |
| 6902 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6903 | ne = X509_NAME_get_entry(a, j); |
| 6904 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6905 | data = X509_NAME_ENTRY_get_data(ne); |
| 6906 | data_ptr = ASN1_STRING_get0_data(data); |
| 6907 | data_len = ASN1_STRING_length(data); |
| 6908 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6909 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6910 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6911 | s = tmp; |
| 6912 | } |
| 6913 | |
| 6914 | if (chunk_strcasecmp(entry, s) != 0) |
| 6915 | continue; |
| 6916 | |
| 6917 | if (pos < 0) |
| 6918 | cur--; |
| 6919 | else |
| 6920 | cur++; |
| 6921 | |
| 6922 | if (cur != pos) |
| 6923 | continue; |
| 6924 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6925 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6926 | return -1; |
| 6927 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6928 | memcpy(out->area, data_ptr, data_len); |
| 6929 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6930 | return 1; |
| 6931 | } |
| 6932 | |
| 6933 | return 0; |
| 6934 | |
| 6935 | } |
| 6936 | |
| 6937 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 6938 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 6939 | */ |
| 6940 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6941 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6942 | { |
| 6943 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6944 | ASN1_OBJECT *obj; |
| 6945 | ASN1_STRING *data; |
| 6946 | const unsigned char *data_ptr; |
| 6947 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6948 | int i, n, ln; |
| 6949 | int l = 0; |
| 6950 | const char *s; |
| 6951 | char *p; |
| 6952 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6953 | int name_count; |
| 6954 | |
| 6955 | |
| 6956 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6957 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6958 | out->data = 0; |
| 6959 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6960 | for (i = 0; i < name_count; i++) { |
| 6961 | ne = X509_NAME_get_entry(a, i); |
| 6962 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6963 | data = X509_NAME_ENTRY_get_data(ne); |
| 6964 | data_ptr = ASN1_STRING_get0_data(data); |
| 6965 | data_len = ASN1_STRING_length(data); |
| 6966 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6967 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6968 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6969 | s = tmp; |
| 6970 | } |
| 6971 | ln = strlen(s); |
| 6972 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6973 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6974 | if (l > out->size) |
| 6975 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6976 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6977 | |
| 6978 | *(p++)='/'; |
| 6979 | memcpy(p, s, ln); |
| 6980 | p += ln; |
| 6981 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6982 | memcpy(p, data_ptr, data_len); |
| 6983 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6984 | } |
| 6985 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6986 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6987 | return 0; |
| 6988 | |
| 6989 | return 1; |
| 6990 | } |
| 6991 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6992 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6993 | { |
| 6994 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6995 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6996 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 6997 | if (!ssl_sock_is_ssl(conn)) |
| 6998 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6999 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7000 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 7001 | #endif |
| 7002 | } |
| 7003 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7004 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 7005 | * to disable SNI. |
| 7006 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7007 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 7008 | { |
| 7009 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7010 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7011 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7012 | char *prev_name; |
| 7013 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7014 | if (!ssl_sock_is_ssl(conn)) |
| 7015 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7016 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7017 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7018 | /* if the SNI changes, we must destroy the reusable context so that a |
| 7019 | * new connection will present a new SNI. As an optimization we could |
| 7020 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 7021 | * server. |
| 7022 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7023 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7024 | if ((!prev_name && hostname) || |
| 7025 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7026 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 7027 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7028 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 7029 | #endif |
| 7030 | } |
| 7031 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7032 | /* Extract peer certificate's common name into the chunk dest |
| 7033 | * Returns |
| 7034 | * the len of the extracted common name |
| 7035 | * or 0 if no CN found in DN |
| 7036 | * or -1 on error case (i.e. no peer certificate) |
| 7037 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7038 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 7039 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7040 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7041 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7042 | X509 *crt = NULL; |
| 7043 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7044 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7045 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7046 | .area = (char *)&find_cn, |
| 7047 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7048 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7049 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7050 | |
| 7051 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7052 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7053 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7054 | |
| 7055 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7056 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7057 | if (!crt) |
| 7058 | goto out; |
| 7059 | |
| 7060 | name = X509_get_subject_name(crt); |
| 7061 | if (!name) |
| 7062 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7063 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 7064 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 7065 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7066 | if (crt) |
| 7067 | X509_free(crt); |
| 7068 | |
| 7069 | return result; |
| 7070 | } |
| 7071 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7072 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 7073 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 7074 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7075 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7076 | X509 *crt = NULL; |
| 7077 | |
| 7078 | if (!ssl_sock_is_ssl(conn)) |
| 7079 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7080 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7081 | |
| 7082 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7083 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 7084 | if (!crt) |
| 7085 | return 0; |
| 7086 | |
| 7087 | X509_free(crt); |
| 7088 | return 1; |
| 7089 | } |
| 7090 | |
| 7091 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 7092 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7093 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7094 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7095 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7096 | if (!ssl_sock_is_ssl(conn)) |
| 7097 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7098 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7099 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7100 | } |
| 7101 | |
| 7102 | /* returns result from SSL verify */ |
| 7103 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 7104 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7105 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7106 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7107 | if (!ssl_sock_is_ssl(conn)) |
| 7108 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 7109 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7110 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 7111 | } |
| 7112 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7113 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 7114 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 7115 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 7116 | * freed by the caller. NPN is also checked if available since older versions |
| 7117 | * of openssl (1.0.1) which are more common in field only support this one. |
| 7118 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7119 | static int ssl_sock_get_alpn(const struct connection *conn, void *xprt_ctx, const char **str, int *len) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7120 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7121 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 7122 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7123 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 7124 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7125 | return 0; |
| 7126 | |
| 7127 | *str = NULL; |
| 7128 | |
| 7129 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7130 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7131 | if (*str) |
| 7132 | return 1; |
| 7133 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7134 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7135 | 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] | 7136 | if (*str) |
| 7137 | return 1; |
| 7138 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 7139 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 7140 | return 0; |
| 7141 | } |
| 7142 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7143 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 7144 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7145 | static int |
| 7146 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7147 | { |
| 7148 | struct connection *conn; |
| 7149 | |
| 7150 | conn = objt_conn(smp->sess->origin); |
| 7151 | if (!conn || conn->xprt != &ssl_sock) |
| 7152 | return 0; |
| 7153 | |
| 7154 | smp->flags = 0; |
| 7155 | smp->data.type = SMP_T_BOOL; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 7156 | #ifdef OPENSSL_IS_BORINGSSL |
| 7157 | { |
| 7158 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7159 | smp->data.u.sint = (SSL_in_early_data(ctx->ssl) && |
| 7160 | SSL_early_data_accepted(ctx->ssl)); |
| 7161 | } |
| 7162 | #else |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 7163 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
| 7164 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 7165 | #endif |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 7166 | return 1; |
| 7167 | } |
| 7168 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7169 | /* boolean, returns true if client cert was present */ |
| 7170 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7171 | smp_fetch_ssl_fc_has_crt(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7172 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7173 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7174 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7175 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7176 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7177 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7178 | return 0; |
| 7179 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7180 | ctx = conn->xprt_ctx; |
| 7181 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7182 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7183 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7184 | return 0; |
| 7185 | } |
| 7186 | |
| 7187 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7188 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7189 | smp->data.u.sint = SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7190 | |
| 7191 | return 1; |
| 7192 | } |
| 7193 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7194 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 7195 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7196 | * should be use. |
| 7197 | */ |
| 7198 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7199 | smp_fetch_ssl_x_der(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7200 | { |
| 7201 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 7202 | X509 *crt = NULL; |
| 7203 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7204 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7205 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7206 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7207 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7208 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7209 | if (!conn || conn->xprt != &ssl_sock) |
| 7210 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7211 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7212 | |
| 7213 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 7214 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7215 | return 0; |
| 7216 | } |
| 7217 | |
| 7218 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7219 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7220 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7221 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7222 | |
| 7223 | if (!crt) |
| 7224 | goto out; |
| 7225 | |
| 7226 | smp_trash = get_trash_chunk(); |
| 7227 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 7228 | goto out; |
| 7229 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7230 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7231 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 7232 | ret = 1; |
| 7233 | out: |
| 7234 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7235 | if (cert_peer && crt) |
| 7236 | X509_free(crt); |
| 7237 | return ret; |
| 7238 | } |
| 7239 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7240 | /* binary, returns serial of certificate in a binary chunk. |
| 7241 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7242 | * should be use. |
| 7243 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7244 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7245 | smp_fetch_ssl_x_serial(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7246 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7247 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7248 | X509 *crt = NULL; |
| 7249 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7250 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7251 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7252 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7253 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7254 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7255 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7256 | return 0; |
| 7257 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7258 | ctx = conn->xprt_ctx; |
| 7259 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7260 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7261 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7262 | return 0; |
| 7263 | } |
| 7264 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7265 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7266 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7267 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7268 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7269 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7270 | if (!crt) |
| 7271 | goto out; |
| 7272 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7273 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7274 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 7275 | goto out; |
| 7276 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7277 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7278 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7279 | ret = 1; |
| 7280 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7281 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7282 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 7283 | X509_free(crt); |
| 7284 | return ret; |
| 7285 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 7286 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7287 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 7288 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7289 | * should be use. |
| 7290 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7291 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7292 | smp_fetch_ssl_x_sha1(const struct arg *args, struct sample *smp, const char *kw, void *private) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7293 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7294 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7295 | X509 *crt = NULL; |
| 7296 | const EVP_MD *digest; |
| 7297 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7298 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7299 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7300 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7301 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7302 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7303 | if (!conn || conn->xprt != &ssl_sock) |
| 7304 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7305 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7306 | |
| 7307 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7308 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7309 | return 0; |
| 7310 | } |
| 7311 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7312 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7313 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7314 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7315 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7316 | if (!crt) |
| 7317 | goto out; |
| 7318 | |
| 7319 | smp_trash = get_trash_chunk(); |
| 7320 | digest = EVP_sha1(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7321 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, |
| 7322 | (unsigned int *)&smp_trash->data); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7323 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7324 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7325 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7326 | ret = 1; |
| 7327 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7328 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7329 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 7330 | X509_free(crt); |
| 7331 | return ret; |
| 7332 | } |
| 7333 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7334 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 7335 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7336 | * should be use. |
| 7337 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7338 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7339 | smp_fetch_ssl_x_notafter(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7340 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7341 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7342 | X509 *crt = NULL; |
| 7343 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7344 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7345 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7346 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7347 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7348 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7349 | if (!conn || conn->xprt != &ssl_sock) |
| 7350 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7351 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7352 | |
| 7353 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7354 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7355 | return 0; |
| 7356 | } |
| 7357 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7358 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7359 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7360 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7361 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7362 | if (!crt) |
| 7363 | goto out; |
| 7364 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7365 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7366 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7367 | goto out; |
| 7368 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7369 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7370 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7371 | ret = 1; |
| 7372 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7373 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7374 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7375 | X509_free(crt); |
| 7376 | return ret; |
| 7377 | } |
| 7378 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7379 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 7380 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7381 | * should be use. |
| 7382 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7383 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7384 | smp_fetch_ssl_x_i_dn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7385 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7386 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7387 | X509 *crt = NULL; |
| 7388 | X509_NAME *name; |
| 7389 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7390 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7391 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7392 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7393 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7394 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7395 | if (!conn || conn->xprt != &ssl_sock) |
| 7396 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7397 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7398 | |
| 7399 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7400 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7401 | return 0; |
| 7402 | } |
| 7403 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7404 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7405 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7406 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7407 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7408 | if (!crt) |
| 7409 | goto out; |
| 7410 | |
| 7411 | name = X509_get_issuer_name(crt); |
| 7412 | if (!name) |
| 7413 | goto out; |
| 7414 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7415 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7416 | if (args && args[0].type == ARGT_STR) { |
| 7417 | int pos = 1; |
| 7418 | |
| 7419 | if (args[1].type == ARGT_SINT) |
| 7420 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7421 | |
| 7422 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 7423 | goto out; |
| 7424 | } |
| 7425 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 7426 | goto out; |
| 7427 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7428 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7429 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7430 | ret = 1; |
| 7431 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7432 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7433 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7434 | X509_free(crt); |
| 7435 | return ret; |
| 7436 | } |
| 7437 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7438 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 7439 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7440 | * should be use. |
| 7441 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7442 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7443 | smp_fetch_ssl_x_notbefore(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7444 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7445 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7446 | X509 *crt = NULL; |
| 7447 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7448 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7449 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7450 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7451 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7452 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7453 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7454 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7455 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7456 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7457 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7458 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7459 | return 0; |
| 7460 | } |
| 7461 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7462 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7463 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7464 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7465 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7466 | if (!crt) |
| 7467 | goto out; |
| 7468 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7469 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 7470 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7471 | goto out; |
| 7472 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7473 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7474 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7475 | ret = 1; |
| 7476 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7477 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7478 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 7479 | X509_free(crt); |
| 7480 | return ret; |
| 7481 | } |
| 7482 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7483 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 7484 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7485 | * should be use. |
| 7486 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7487 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7488 | smp_fetch_ssl_x_s_dn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7489 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7490 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7491 | X509 *crt = NULL; |
| 7492 | X509_NAME *name; |
| 7493 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7494 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7495 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7496 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7497 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7498 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7499 | if (!conn || conn->xprt != &ssl_sock) |
| 7500 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7501 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7502 | |
| 7503 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7504 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7505 | return 0; |
| 7506 | } |
| 7507 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7508 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7509 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7510 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7511 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7512 | if (!crt) |
| 7513 | goto out; |
| 7514 | |
| 7515 | name = X509_get_subject_name(crt); |
| 7516 | if (!name) |
| 7517 | goto out; |
| 7518 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 7519 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7520 | if (args && args[0].type == ARGT_STR) { |
| 7521 | int pos = 1; |
| 7522 | |
| 7523 | if (args[1].type == ARGT_SINT) |
| 7524 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7525 | |
| 7526 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 7527 | goto out; |
| 7528 | } |
| 7529 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 7530 | goto out; |
| 7531 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7532 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7533 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7534 | ret = 1; |
| 7535 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7536 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 7537 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 7538 | X509_free(crt); |
| 7539 | return ret; |
| 7540 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7541 | |
| 7542 | /* integer, returns true if current session use a client certificate */ |
| 7543 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7544 | smp_fetch_ssl_c_used(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7545 | { |
| 7546 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7547 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7548 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7549 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7550 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7551 | if (!conn || conn->xprt != &ssl_sock) |
| 7552 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7553 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7554 | |
| 7555 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7556 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7557 | return 0; |
| 7558 | } |
| 7559 | |
| 7560 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7561 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7562 | if (crt) { |
| 7563 | X509_free(crt); |
| 7564 | } |
| 7565 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7566 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7567 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 7568 | return 1; |
| 7569 | } |
| 7570 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7571 | /* integer, returns the certificate version |
| 7572 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7573 | * should be use. |
| 7574 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7575 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7576 | smp_fetch_ssl_x_version(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7577 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7578 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7579 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7580 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7581 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7582 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7583 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7584 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7585 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7586 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7587 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7588 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7589 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7590 | return 0; |
| 7591 | } |
| 7592 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7593 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7594 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7595 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7596 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7597 | if (!crt) |
| 7598 | return 0; |
| 7599 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7600 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7601 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7602 | if (cert_peer) |
| 7603 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7604 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7605 | |
| 7606 | return 1; |
| 7607 | } |
| 7608 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7609 | /* string, returns the certificate's signature algorithm. |
| 7610 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7611 | * should be use. |
| 7612 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7613 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7614 | smp_fetch_ssl_x_sig_alg(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7615 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7616 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7617 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7618 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7619 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7620 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7621 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7622 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7623 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7624 | if (!conn || conn->xprt != &ssl_sock) |
| 7625 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7626 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7627 | |
| 7628 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7629 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7630 | return 0; |
| 7631 | } |
| 7632 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7633 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7634 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7635 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7636 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7637 | if (!crt) |
| 7638 | return 0; |
| 7639 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7640 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7641 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7642 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7643 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 7644 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7645 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7646 | if (cert_peer) |
| 7647 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7648 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 7649 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7650 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7651 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7652 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7653 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7654 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7655 | if (cert_peer) |
| 7656 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7657 | |
| 7658 | return 1; |
| 7659 | } |
| 7660 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7661 | /* string, returns the certificate's key algorithm. |
| 7662 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7663 | * should be use. |
| 7664 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7665 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7666 | smp_fetch_ssl_x_key_alg(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7667 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7668 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7669 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7670 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7671 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7672 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7673 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7674 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7675 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7676 | if (!conn || conn->xprt != &ssl_sock) |
| 7677 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7678 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7679 | |
| 7680 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7681 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7682 | return 0; |
| 7683 | } |
| 7684 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7685 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7686 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7687 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7688 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7689 | if (!crt) |
| 7690 | return 0; |
| 7691 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7692 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 7693 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7694 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7695 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 7696 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7697 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7698 | if (cert_peer) |
| 7699 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7700 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 7701 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7702 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7703 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7704 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7705 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7706 | if (cert_peer) |
| 7707 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7708 | |
| 7709 | return 1; |
| 7710 | } |
| 7711 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7712 | /* boolean, returns true if front conn. transport layer is SSL. |
| 7713 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7714 | * char is 'b'. |
| 7715 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7716 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7717 | smp_fetch_ssl_fc(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7718 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7719 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7720 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7721 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7722 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7723 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7724 | return 1; |
| 7725 | } |
| 7726 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7727 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7728 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7729 | smp_fetch_ssl_fc_has_sni(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7730 | { |
| 7731 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7732 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7733 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7734 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7735 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7736 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7737 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7738 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7739 | return 1; |
| 7740 | #else |
| 7741 | return 0; |
| 7742 | #endif |
| 7743 | } |
| 7744 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7745 | /* boolean, returns true if client session has been resumed. |
| 7746 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7747 | * char is 'b'. |
| 7748 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7749 | static int |
| 7750 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7751 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7752 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7753 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7754 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7755 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7756 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7757 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7758 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7759 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7760 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7761 | return 1; |
| 7762 | } |
| 7763 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7764 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 7765 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7766 | * char is 'b'. |
| 7767 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7768 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7769 | smp_fetch_ssl_fc_cipher(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7770 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7771 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7772 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7773 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7774 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7775 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7776 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7777 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7778 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7779 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7780 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7781 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7782 | return 0; |
| 7783 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7784 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7785 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7786 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7787 | |
| 7788 | return 1; |
| 7789 | } |
| 7790 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7791 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 7792 | * is SSL. |
| 7793 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7794 | * char is 'b'. |
| 7795 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7796 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7797 | smp_fetch_ssl_fc_alg_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7798 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7799 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7800 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7801 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7802 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7803 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7804 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7805 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7806 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7807 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7808 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7809 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7810 | return 0; |
| 7811 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7812 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7813 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7814 | |
| 7815 | return 1; |
| 7816 | } |
| 7817 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7818 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 7819 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7820 | * char is 'b'. |
| 7821 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7822 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7823 | smp_fetch_ssl_fc_use_keysize(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7824 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7825 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7826 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7827 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7828 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7829 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7830 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7831 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7832 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7833 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7834 | smp->data.u.sint = (unsigned int)SSL_get_cipher_bits(ctx->ssl, NULL); |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7835 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7836 | return 0; |
| 7837 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7838 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7839 | |
| 7840 | return 1; |
| 7841 | } |
| 7842 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7843 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7844 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7845 | smp_fetch_ssl_fc_npn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7846 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7847 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7848 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7849 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7850 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7851 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7852 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 7853 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 7854 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7855 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7856 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7857 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7858 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7859 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7860 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7861 | (const unsigned char **)&smp->data.u.str.area, |
| 7862 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7863 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7864 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7865 | return 0; |
| 7866 | |
| 7867 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7868 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7869 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7870 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7871 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7872 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7873 | smp_fetch_ssl_fc_alpn(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7874 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7875 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7876 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7877 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7878 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7879 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7880 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 7881 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 7882 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7883 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7884 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7885 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7886 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7887 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7888 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7889 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7890 | (const unsigned char **)&smp->data.u.str.area, |
| 7891 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7892 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7893 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7894 | return 0; |
| 7895 | |
| 7896 | return 1; |
| 7897 | } |
| 7898 | #endif |
| 7899 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7900 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 7901 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7902 | * char is 'b'. |
| 7903 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7904 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7905 | smp_fetch_ssl_fc_protocol(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7906 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7907 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7908 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7909 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7910 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7911 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7912 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7913 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7914 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7915 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7916 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7917 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7918 | return 0; |
| 7919 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7920 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7921 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7922 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7923 | |
| 7924 | return 1; |
| 7925 | } |
| 7926 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7927 | /* binary, returns the SSL stream id if front conn. transport layer is SSL. |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7928 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7929 | * char is 'b'. |
| 7930 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7931 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7932 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7933 | smp_fetch_ssl_fc_session_id(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7934 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7935 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7936 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7937 | SSL_SESSION *ssl_sess; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7938 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7939 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7940 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7941 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7942 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7943 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7944 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7945 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7946 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7947 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7948 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7949 | return 0; |
| 7950 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7951 | smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, |
| 7952 | (unsigned int *)&smp->data.u.str.data); |
| 7953 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7954 | return 0; |
| 7955 | |
| 7956 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7957 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7958 | #endif |
| 7959 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7960 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 7961 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7962 | static int |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 7963 | smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7964 | { |
| 7965 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7966 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7967 | struct buffer *data; |
| 7968 | struct ssl_sock_ctx *ctx; |
| 7969 | |
| 7970 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7971 | return 0; |
| 7972 | ctx = conn->xprt_ctx; |
| 7973 | |
| 7974 | data = get_trash_chunk(); |
| 7975 | if (kw[7] == 'c') |
| 7976 | data->data = SSL_get_client_random(ctx->ssl, |
| 7977 | (unsigned char *) data->area, |
| 7978 | data->size); |
| 7979 | else |
| 7980 | data->data = SSL_get_server_random(ctx->ssl, |
| 7981 | (unsigned char *) data->area, |
| 7982 | data->size); |
| 7983 | if (!data->data) |
| 7984 | return 0; |
| 7985 | |
| 7986 | smp->flags = 0; |
| 7987 | smp->data.type = SMP_T_BIN; |
| 7988 | smp->data.u.str = *data; |
| 7989 | |
| 7990 | return 1; |
| 7991 | } |
| 7992 | |
| 7993 | static int |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7994 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7995 | { |
| 7996 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7997 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7998 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7999 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8000 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8001 | |
| 8002 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8003 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8004 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8005 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8006 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8007 | if (!ssl_sess) |
| 8008 | return 0; |
| 8009 | |
| 8010 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8011 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 8012 | (unsigned char *) data->area, |
| 8013 | data->size); |
| 8014 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 8015 | return 0; |
| 8016 | |
| 8017 | smp->flags = 0; |
| 8018 | smp->data.type = SMP_T_BIN; |
| 8019 | smp->data.u.str = *data; |
| 8020 | |
| 8021 | return 1; |
| 8022 | } |
| 8023 | #endif |
| 8024 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8025 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 8026 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8027 | smp_fetch_ssl_fc_sni(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8028 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8029 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8030 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8031 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 8032 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8033 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8034 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8035 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8036 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8037 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8038 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8039 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8040 | smp->data.u.str.area = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8041 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 8042 | return 0; |
| 8043 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8044 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8045 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8046 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8047 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 8048 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8049 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8050 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8051 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8052 | struct connection *conn; |
| 8053 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8054 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8055 | |
| 8056 | conn = objt_conn(smp->sess->origin); |
| 8057 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8058 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8059 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8060 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8061 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8062 | if (!capture) |
| 8063 | return 0; |
| 8064 | |
| 8065 | smp->flags = SMP_F_CONST; |
| 8066 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8067 | smp->data.u.str.area = capture->ciphersuite; |
| 8068 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8069 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8070 | } |
| 8071 | |
| 8072 | static int |
| 8073 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8074 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8075 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8076 | |
| 8077 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8078 | return 0; |
| 8079 | |
| 8080 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8081 | dump_binary(data, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8082 | smp->data.type = SMP_T_BIN; |
| 8083 | smp->data.u.str = *data; |
| 8084 | return 1; |
| 8085 | } |
| 8086 | |
| 8087 | static int |
| 8088 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8089 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8090 | struct connection *conn; |
| 8091 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8092 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8093 | |
| 8094 | conn = objt_conn(smp->sess->origin); |
| 8095 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8096 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8097 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8098 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8099 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8100 | if (!capture) |
| 8101 | return 0; |
| 8102 | |
| 8103 | smp->data.type = SMP_T_SINT; |
| 8104 | smp->data.u.sint = capture->xxh64; |
| 8105 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8106 | } |
| 8107 | |
| 8108 | static int |
| 8109 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 8110 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8111 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8112 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8113 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8114 | |
| 8115 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 8116 | return 0; |
| 8117 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8118 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8119 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8120 | const char *str; |
| 8121 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8122 | const unsigned char *bin = (const unsigned char *) smp->data.u.str.area + i; |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8123 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 8124 | #if defined(OPENSSL_IS_BORINGSSL) |
| 8125 | cipher = SSL_get_cipher_by_value(id); |
| 8126 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 8127 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8128 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 8129 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 8130 | #endif |
| 8131 | str = SSL_CIPHER_get_name(cipher); |
| 8132 | if (!str || strcmp(str, "(NONE)") == 0) |
| 8133 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8134 | else |
| 8135 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 8136 | } |
| 8137 | smp->data.type = SMP_T_STR; |
| 8138 | smp->data.u.str = *data; |
| 8139 | return 1; |
| 8140 | #else |
| 8141 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 8142 | #endif |
| 8143 | } |
| 8144 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8145 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8146 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8147 | smp_fetch_ssl_fc_unique_id(const struct arg *args, struct sample *smp, const char *kw, void *private) |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8148 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 8149 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 8150 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8151 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 8152 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8153 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8154 | |
| 8155 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8156 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 8157 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8158 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8159 | |
| 8160 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 8161 | smp->flags |= SMP_F_MAY_CHANGE; |
| 8162 | return 0; |
| 8163 | } |
| 8164 | |
| 8165 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8166 | if (!SSL_session_reused(ctx->ssl)) |
| 8167 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8168 | finished_trash->area, |
| 8169 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8170 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8171 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8172 | finished_trash->area, |
| 8173 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8174 | |
| 8175 | if (!finished_len) |
| 8176 | return 0; |
| 8177 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8178 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 8179 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8180 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8181 | |
| 8182 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8183 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 8184 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 8185 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8186 | /* integer, returns the first verify error in CA chain of client certificate chain. */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8187 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8188 | smp_fetch_ssl_c_ca_err(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8189 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8190 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8191 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8192 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8193 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8194 | if (!conn || conn->xprt != &ssl_sock) |
| 8195 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8196 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8197 | |
| 8198 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8199 | smp->flags = SMP_F_MAY_CHANGE; |
| 8200 | return 0; |
| 8201 | } |
| 8202 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8203 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8204 | smp->data.u.sint = (unsigned long long int)SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8205 | smp->flags = 0; |
| 8206 | |
| 8207 | return 1; |
| 8208 | } |
| 8209 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8210 | /* integer, returns the depth of the first verify error in CA chain of client certificate chain. */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8211 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8212 | smp_fetch_ssl_c_ca_err_depth(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8213 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8214 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8215 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8216 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8217 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8218 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8219 | return 0; |
| 8220 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8221 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8222 | smp->flags = SMP_F_MAY_CHANGE; |
| 8223 | return 0; |
| 8224 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8225 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8226 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8227 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8228 | smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CAEDEPTH(ctx->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8229 | smp->flags = 0; |
| 8230 | |
| 8231 | return 1; |
| 8232 | } |
| 8233 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8234 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8235 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8236 | smp_fetch_ssl_c_err(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8237 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8238 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8239 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8240 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8241 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8242 | if (!conn || conn->xprt != &ssl_sock) |
| 8243 | return 0; |
| 8244 | |
| 8245 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8246 | smp->flags = SMP_F_MAY_CHANGE; |
| 8247 | return 0; |
| 8248 | } |
| 8249 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8250 | ctx = conn->xprt_ctx; |
| 8251 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8252 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 8253 | smp->data.u.sint = (long long int)SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 8254 | smp->flags = 0; |
| 8255 | |
| 8256 | return 1; |
| 8257 | } |
| 8258 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 8259 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8260 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8261 | smp_fetch_ssl_c_verify(const struct arg *args, struct sample *smp, const char *kw, void *private) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8262 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8263 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8264 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8265 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8266 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8267 | if (!conn || conn->xprt != &ssl_sock) |
| 8268 | return 0; |
| 8269 | |
| 8270 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8271 | smp->flags = SMP_F_MAY_CHANGE; |
| 8272 | return 0; |
| 8273 | } |
| 8274 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 8275 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8276 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8277 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8278 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 8279 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 8280 | smp->data.u.sint = (long long int)SSL_get_verify_result(ctx->ssl); |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 8281 | smp->flags = 0; |
| 8282 | |
| 8283 | return 1; |
| 8284 | } |
| 8285 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 8286 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8287 | static int ssl_bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8288 | { |
| 8289 | if (!*args[cur_arg + 1]) { |
| 8290 | if (err) |
| 8291 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 8292 | return ERR_ALERT | ERR_FATAL; |
| 8293 | } |
| 8294 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8295 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8296 | memprintf(&conf->ca_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8297 | else |
| 8298 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8299 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8300 | return 0; |
| 8301 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8302 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8303 | { |
| 8304 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 8305 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8306 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8307 | /* parse the "ca-sign-file" bind keyword */ |
| 8308 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8309 | { |
| 8310 | if (!*args[cur_arg + 1]) { |
| 8311 | if (err) |
| 8312 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 8313 | return ERR_ALERT | ERR_FATAL; |
| 8314 | } |
| 8315 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8316 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8317 | memprintf(&conf->ca_sign_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8318 | else |
| 8319 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 8320 | |
| 8321 | return 0; |
| 8322 | } |
| 8323 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 8324 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8325 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8326 | { |
| 8327 | if (!*args[cur_arg + 1]) { |
| 8328 | if (err) |
| 8329 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 8330 | return ERR_ALERT | ERR_FATAL; |
| 8331 | } |
| 8332 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 8333 | return 0; |
| 8334 | } |
| 8335 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8336 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8337 | static int ssl_bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8338 | { |
| 8339 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8340 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8341 | return ERR_ALERT | ERR_FATAL; |
| 8342 | } |
| 8343 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8344 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8345 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8346 | return 0; |
| 8347 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8348 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8349 | { |
| 8350 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 8351 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8352 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8353 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8354 | /* parse the "ciphersuites" bind keyword */ |
| 8355 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8356 | { |
| 8357 | if (!*args[cur_arg + 1]) { |
| 8358 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 8359 | return ERR_ALERT | ERR_FATAL; |
| 8360 | } |
| 8361 | |
| 8362 | free(conf->ciphersuites); |
| 8363 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 8364 | return 0; |
| 8365 | } |
| 8366 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8367 | { |
| 8368 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 8369 | } |
| 8370 | #endif |
| 8371 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8372 | /* parse the "crt" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8373 | static int bind_parse_crt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8374 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 8375 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 8376 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8377 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 8378 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8379 | return ERR_ALERT | ERR_FATAL; |
| 8380 | } |
| 8381 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8382 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 8383 | if ((strlen(global_ssl.crt_base) + 1 + strlen(args[cur_arg + 1]) + 1) > MAXPATHLEN) { |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8384 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 8385 | return ERR_ALERT | ERR_FATAL; |
| 8386 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8387 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]); |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8388 | return ssl_sock_load_cert(path, conf, err); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8389 | } |
| 8390 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8391 | return ssl_sock_load_cert(args[cur_arg + 1], conf, err); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8392 | } |
| 8393 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8394 | /* parse the "crt-list" bind keyword. Returns a set of ERR_* flags possibly with an error in <err>. */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 8395 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8396 | { |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8397 | int err_code; |
| 8398 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 8399 | if (!*args[cur_arg + 1]) { |
| 8400 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 8401 | return ERR_ALERT | ERR_FATAL; |
| 8402 | } |
| 8403 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8404 | err_code = ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err); |
| 8405 | if (err_code) |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 8406 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 8407 | |
Willy Tarreau | bbc9196 | 2019-10-16 16:42:19 +0200 | [diff] [blame] | 8408 | return err_code; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 8409 | } |
| 8410 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 8411 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8412 | static int ssl_bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8413 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 8414 | #ifndef X509_V_FLAG_CRL_CHECK |
| 8415 | if (err) |
| 8416 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 8417 | return ERR_ALERT | ERR_FATAL; |
| 8418 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8419 | if (!*args[cur_arg + 1]) { |
| 8420 | if (err) |
| 8421 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 8422 | return ERR_ALERT | ERR_FATAL; |
| 8423 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8424 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8425 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8426 | memprintf(&conf->crl_file, "%s/%s", global_ssl.ca_base, args[cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8427 | else |
| 8428 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 8429 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8430 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 8431 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8432 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8433 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8434 | { |
| 8435 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 8436 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8437 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 8438 | /* parse the "curves" bind keyword keyword */ |
| 8439 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8440 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8441 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 8442 | if (!*args[cur_arg + 1]) { |
| 8443 | if (err) |
| 8444 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 8445 | return ERR_ALERT | ERR_FATAL; |
| 8446 | } |
| 8447 | conf->curves = strdup(args[cur_arg + 1]); |
| 8448 | return 0; |
| 8449 | #else |
| 8450 | if (err) |
| 8451 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 8452 | return ERR_ALERT | ERR_FATAL; |
| 8453 | #endif |
| 8454 | } |
| 8455 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8456 | { |
| 8457 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 8458 | } |
| 8459 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 8460 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8461 | static int ssl_bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8462 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8463 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8464 | if (err) |
| 8465 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 8466 | return ERR_ALERT | ERR_FATAL; |
| 8467 | #elif defined(OPENSSL_NO_ECDH) |
| 8468 | if (err) |
| 8469 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 8470 | return ERR_ALERT | ERR_FATAL; |
| 8471 | #else |
| 8472 | if (!*args[cur_arg + 1]) { |
| 8473 | if (err) |
| 8474 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 8475 | return ERR_ALERT | ERR_FATAL; |
| 8476 | } |
| 8477 | |
| 8478 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8479 | |
| 8480 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 8481 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8482 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8483 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8484 | { |
| 8485 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 8486 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8487 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 8488 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8489 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8490 | { |
| 8491 | int code; |
| 8492 | char *p = args[cur_arg + 1]; |
| 8493 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 8494 | |
| 8495 | if (!*p) { |
| 8496 | if (err) |
| 8497 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 8498 | return ERR_ALERT | ERR_FATAL; |
| 8499 | } |
| 8500 | |
| 8501 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 8502 | ignerr = &conf->ca_ignerr; |
| 8503 | |
| 8504 | if (strcmp(p, "all") == 0) { |
| 8505 | *ignerr = ~0ULL; |
| 8506 | return 0; |
| 8507 | } |
| 8508 | |
| 8509 | while (p) { |
| 8510 | code = atoi(p); |
| 8511 | if ((code <= 0) || (code > 63)) { |
| 8512 | if (err) |
| 8513 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 8514 | args[cur_arg], code, args[cur_arg + 1]); |
| 8515 | return ERR_ALERT | ERR_FATAL; |
| 8516 | } |
| 8517 | *ignerr |= 1ULL << code; |
| 8518 | p = strchr(p, ','); |
| 8519 | if (p) |
| 8520 | p++; |
| 8521 | } |
| 8522 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8523 | return 0; |
| 8524 | } |
| 8525 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8526 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 8527 | static int parse_tls_method_options(char *arg, struct tls_version_filter *methods, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8528 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8529 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8530 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8531 | p = strchr(arg, '-'); |
| 8532 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8533 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8534 | p++; |
| 8535 | if (!strcmp(p, "sslv3")) |
| 8536 | v = CONF_SSLV3; |
| 8537 | else if (!strcmp(p, "tlsv10")) |
| 8538 | v = CONF_TLSV10; |
| 8539 | else if (!strcmp(p, "tlsv11")) |
| 8540 | v = CONF_TLSV11; |
| 8541 | else if (!strcmp(p, "tlsv12")) |
| 8542 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 8543 | else if (!strcmp(p, "tlsv13")) |
| 8544 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8545 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8546 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8547 | if (!strncmp(arg, "no-", 3)) |
| 8548 | methods->flags |= methodVersions[v].flag; |
| 8549 | else if (!strncmp(arg, "force-", 6)) |
| 8550 | methods->min = methods->max = v; |
| 8551 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8552 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8553 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8554 | fail: |
| 8555 | if (err) |
| 8556 | memprintf(err, "'%s' : option not implemented", arg); |
| 8557 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8558 | } |
| 8559 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8560 | static int bind_parse_tls_method_options(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8561 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8562 | return parse_tls_method_options(args[cur_arg], &conf->ssl_conf.ssl_methods, err); |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8563 | } |
| 8564 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8565 | static int srv_parse_tls_method_options(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8566 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8567 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 8568 | } |
| 8569 | |
| 8570 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 8571 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 8572 | { |
| 8573 | uint16_t i, v = 0; |
| 8574 | char *argv = args[cur_arg + 1]; |
| 8575 | if (!*argv) { |
| 8576 | if (err) |
| 8577 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 8578 | return ERR_ALERT | ERR_FATAL; |
| 8579 | } |
| 8580 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 8581 | if (!strcmp(argv, methodVersions[i].name)) |
| 8582 | v = i; |
| 8583 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8584 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8585 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8586 | return ERR_ALERT | ERR_FATAL; |
| 8587 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8588 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 8589 | methods->min = v; |
| 8590 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 8591 | methods->max = v; |
| 8592 | else { |
| 8593 | if (err) |
| 8594 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 8595 | return ERR_ALERT | ERR_FATAL; |
| 8596 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8597 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8598 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8599 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8600 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8601 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8602 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8603 | ha_warning("crt-list: ssl-min-ver and ssl-max-ver are not supported with this Openssl version (skipped).\n"); |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8604 | #endif |
| 8605 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 8606 | } |
| 8607 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8608 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8609 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8610 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_conf.ssl_methods, err); |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8611 | } |
| 8612 | |
| 8613 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8614 | { |
| 8615 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 8616 | } |
| 8617 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8618 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 8619 | static int bind_parse_no_tls_tickets(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8620 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 8621 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8622 | return 0; |
| 8623 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8624 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8625 | /* parse the "allow-0rtt" bind keyword */ |
| 8626 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8627 | { |
| 8628 | conf->early_data = 1; |
| 8629 | return 0; |
| 8630 | } |
| 8631 | |
| 8632 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8633 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 8634 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8635 | return 0; |
| 8636 | } |
| 8637 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8638 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8639 | static int ssl_bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8640 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8641 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8642 | char *p1, *p2; |
| 8643 | |
| 8644 | if (!*args[cur_arg + 1]) { |
| 8645 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 8646 | return ERR_ALERT | ERR_FATAL; |
| 8647 | } |
| 8648 | |
| 8649 | free(conf->npn_str); |
| 8650 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 8651 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 8652 | * so we reuse each comma to store the next <len> and need |
| 8653 | * one more for the end of the string. |
| 8654 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8655 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 8656 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8657 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 8658 | |
| 8659 | /* replace commas with the name length */ |
| 8660 | p1 = conf->npn_str; |
| 8661 | p2 = p1 + 1; |
| 8662 | while (1) { |
| 8663 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 8664 | if (!p2) |
| 8665 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8666 | |
| 8667 | if (p2 - (p1 + 1) > 255) { |
| 8668 | *p2 = '\0'; |
| 8669 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 8670 | return ERR_ALERT | ERR_FATAL; |
| 8671 | } |
| 8672 | |
| 8673 | *p1 = p2 - (p1 + 1); |
| 8674 | p1 = p2; |
| 8675 | |
| 8676 | if (!*p2) |
| 8677 | break; |
| 8678 | |
| 8679 | *(p2++) = '\0'; |
| 8680 | } |
| 8681 | return 0; |
| 8682 | #else |
| 8683 | if (err) |
| 8684 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 8685 | return ERR_ALERT | ERR_FATAL; |
| 8686 | #endif |
| 8687 | } |
| 8688 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8689 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8690 | { |
| 8691 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 8692 | } |
| 8693 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8694 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8695 | static int ssl_bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8696 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8697 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8698 | char *p1, *p2; |
| 8699 | |
| 8700 | if (!*args[cur_arg + 1]) { |
| 8701 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 8702 | return ERR_ALERT | ERR_FATAL; |
| 8703 | } |
| 8704 | |
| 8705 | free(conf->alpn_str); |
| 8706 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 8707 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 8708 | * so we reuse each comma to store the next <len> and need |
| 8709 | * one more for the end of the string. |
| 8710 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8711 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 8712 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8713 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 8714 | |
| 8715 | /* replace commas with the name length */ |
| 8716 | p1 = conf->alpn_str; |
| 8717 | p2 = p1 + 1; |
| 8718 | while (1) { |
| 8719 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 8720 | if (!p2) |
| 8721 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8722 | |
| 8723 | if (p2 - (p1 + 1) > 255) { |
| 8724 | *p2 = '\0'; |
| 8725 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 8726 | return ERR_ALERT | ERR_FATAL; |
| 8727 | } |
| 8728 | |
| 8729 | *p1 = p2 - (p1 + 1); |
| 8730 | p1 = p2; |
| 8731 | |
| 8732 | if (!*p2) |
| 8733 | break; |
| 8734 | |
| 8735 | *(p2++) = '\0'; |
| 8736 | } |
| 8737 | return 0; |
| 8738 | #else |
| 8739 | if (err) |
| 8740 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 8741 | return ERR_ALERT | ERR_FATAL; |
| 8742 | #endif |
| 8743 | } |
| 8744 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8745 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8746 | { |
| 8747 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 8748 | } |
| 8749 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8750 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8751 | static int bind_parse_ssl(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8752 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 8753 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8754 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8755 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8756 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 8757 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8758 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8759 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 8760 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 8761 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 8762 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8763 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 8764 | if (!conf->ssl_conf.ssl_methods.min) |
| 8765 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 8766 | if (!conf->ssl_conf.ssl_methods.max) |
| 8767 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8768 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8769 | return 0; |
| 8770 | } |
| 8771 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8772 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 8773 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8774 | { |
| 8775 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 8776 | return 0; |
| 8777 | } |
| 8778 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8779 | /* parse the "generate-certificates" bind keyword */ |
| 8780 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8781 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8782 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8783 | conf->generate_certs = 1; |
| 8784 | #else |
| 8785 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 8786 | err && *err ? *err : ""); |
| 8787 | #endif |
| 8788 | return 0; |
| 8789 | } |
| 8790 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 8791 | /* parse the "strict-sni" bind keyword */ |
| 8792 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8793 | { |
| 8794 | conf->strict_sni = 1; |
| 8795 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8796 | } |
| 8797 | |
| 8798 | /* parse the "tls-ticket-keys" bind keyword */ |
| 8799 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8800 | { |
| 8801 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8802 | FILE *f = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8803 | int i = 0; |
| 8804 | char thisline[LINESIZE]; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8805 | struct tls_keys_ref *keys_ref = NULL; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8806 | |
| 8807 | if (!*args[cur_arg + 1]) { |
| 8808 | if (err) |
| 8809 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8810 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8811 | } |
| 8812 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8813 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 8814 | if (keys_ref) { |
| 8815 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8816 | conf->keys_ref = keys_ref; |
| 8817 | return 0; |
| 8818 | } |
| 8819 | |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8820 | keys_ref = calloc(1, sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8821 | if (!keys_ref) { |
| 8822 | if (err) |
| 8823 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8824 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8825 | } |
| 8826 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8827 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8828 | if (!keys_ref->tlskeys) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8829 | if (err) |
| 8830 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8831 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8832 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8833 | |
| 8834 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
| 8835 | if (err) |
| 8836 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8837 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8838 | } |
| 8839 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8840 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8841 | if (!keys_ref->filename) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8842 | if (err) |
| 8843 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8844 | goto fail; |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8845 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8846 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8847 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8848 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 8849 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8850 | int dec_size; |
| 8851 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8852 | /* Strip newline characters from the end */ |
| 8853 | if(thisline[len - 1] == '\n') |
| 8854 | thisline[--len] = 0; |
| 8855 | |
| 8856 | if(thisline[len - 1] == '\r') |
| 8857 | thisline[--len] = 0; |
| 8858 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8859 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 8860 | if (dec_size < 0) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8861 | if (err) |
| 8862 | memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8863 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8864 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8865 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 8866 | keys_ref->key_size_bits = 128; |
| 8867 | } |
| 8868 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 8869 | keys_ref->key_size_bits = 256; |
| 8870 | } |
| 8871 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 8872 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 8873 | || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8874 | if (err) |
| 8875 | memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8876 | goto fail; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8877 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8878 | i++; |
| 8879 | } |
| 8880 | |
| 8881 | if (i < TLS_TICKETS_NO) { |
| 8882 | if (err) |
| 8883 | memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO); |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8884 | goto fail; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8885 | } |
| 8886 | |
| 8887 | fclose(f); |
| 8888 | |
| 8889 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 8890 | i -= 2; |
| 8891 | keys_ref->tls_ticket_enc_index = i < 0 ? 0 : i % TLS_TICKETS_NO; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8892 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 8893 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8894 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8895 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8896 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8897 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 8898 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8899 | return 0; |
Christopher Faulet | e566f3d | 2019-10-21 09:55:49 +0200 | [diff] [blame] | 8900 | |
| 8901 | fail: |
| 8902 | if (f) |
| 8903 | fclose(f); |
| 8904 | if (keys_ref) { |
| 8905 | free(keys_ref->filename); |
| 8906 | free(keys_ref->tlskeys); |
| 8907 | free(keys_ref); |
| 8908 | } |
| 8909 | return ERR_ALERT | ERR_FATAL; |
| 8910 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8911 | #else |
| 8912 | if (err) |
| 8913 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 8914 | return ERR_ALERT | ERR_FATAL; |
| 8915 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 8916 | } |
| 8917 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8918 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8919 | static int ssl_bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8920 | { |
| 8921 | if (!*args[cur_arg + 1]) { |
| 8922 | if (err) |
| 8923 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 8924 | return ERR_ALERT | ERR_FATAL; |
| 8925 | } |
| 8926 | |
| 8927 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8928 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8929 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8930 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8931 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8932 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8933 | else { |
| 8934 | if (err) |
| 8935 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 8936 | args[cur_arg], args[cur_arg + 1]); |
| 8937 | return ERR_ALERT | ERR_FATAL; |
| 8938 | } |
| 8939 | |
| 8940 | return 0; |
| 8941 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8942 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8943 | { |
| 8944 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 8945 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8946 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8947 | /* parse the "no-ca-names" bind keyword */ |
| 8948 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8949 | { |
| 8950 | conf->no_ca_names = 1; |
| 8951 | return 0; |
| 8952 | } |
| 8953 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8954 | { |
| 8955 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 8956 | } |
| 8957 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8958 | /************** "server" keywords ****************/ |
| 8959 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8960 | /* parse the "npn" bind keyword */ |
| 8961 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8962 | { |
| 8963 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 8964 | char *p1, *p2; |
| 8965 | |
| 8966 | if (!*args[*cur_arg + 1]) { |
| 8967 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 8968 | return ERR_ALERT | ERR_FATAL; |
| 8969 | } |
| 8970 | |
| 8971 | free(newsrv->ssl_ctx.npn_str); |
| 8972 | |
| 8973 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 8974 | * so we reuse each comma to store the next <len> and need |
| 8975 | * one more for the end of the string. |
| 8976 | */ |
| 8977 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8978 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 8979 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 8980 | newsrv->ssl_ctx.npn_len); |
| 8981 | |
| 8982 | /* replace commas with the name length */ |
| 8983 | p1 = newsrv->ssl_ctx.npn_str; |
| 8984 | p2 = p1 + 1; |
| 8985 | while (1) { |
| 8986 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 8987 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 8988 | if (!p2) |
| 8989 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8990 | |
| 8991 | if (p2 - (p1 + 1) > 255) { |
| 8992 | *p2 = '\0'; |
| 8993 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8994 | return ERR_ALERT | ERR_FATAL; |
| 8995 | } |
| 8996 | |
| 8997 | *p1 = p2 - (p1 + 1); |
| 8998 | p1 = p2; |
| 8999 | |
| 9000 | if (!*p2) |
| 9001 | break; |
| 9002 | |
| 9003 | *(p2++) = '\0'; |
| 9004 | } |
| 9005 | return 0; |
| 9006 | #else |
| 9007 | if (err) |
| 9008 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]); |
| 9009 | return ERR_ALERT | ERR_FATAL; |
| 9010 | #endif |
| 9011 | } |
| 9012 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9013 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9014 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9015 | { |
| 9016 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 9017 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9018 | char **alpn_str; |
| 9019 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9020 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9021 | if (*args[*cur_arg] == 'c') { |
| 9022 | alpn_str = &newsrv->check.alpn_str; |
| 9023 | alpn_len = &newsrv->check.alpn_len; |
| 9024 | } else { |
| 9025 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 9026 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 9027 | |
| 9028 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9029 | if (!*args[*cur_arg + 1]) { |
| 9030 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 9031 | return ERR_ALERT | ERR_FATAL; |
| 9032 | } |
| 9033 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9034 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9035 | |
| 9036 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 9037 | * so we reuse each comma to store the next <len> and need |
| 9038 | * one more for the end of the string. |
| 9039 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9040 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 9041 | *alpn_str = calloc(1, *alpn_len + 1); |
| 9042 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9043 | |
| 9044 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9045 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9046 | p2 = p1 + 1; |
| 9047 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 9048 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 9049 | if (!p2) |
| 9050 | p2 = p1 + 1 + strlen(p1 + 1); |
| 9051 | |
| 9052 | if (p2 - (p1 + 1) > 255) { |
| 9053 | *p2 = '\0'; |
| 9054 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 9055 | return ERR_ALERT | ERR_FATAL; |
| 9056 | } |
| 9057 | |
| 9058 | *p1 = p2 - (p1 + 1); |
| 9059 | p1 = p2; |
| 9060 | |
| 9061 | if (!*p2) |
| 9062 | break; |
| 9063 | |
| 9064 | *(p2++) = '\0'; |
| 9065 | } |
| 9066 | return 0; |
| 9067 | #else |
| 9068 | if (err) |
| 9069 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]); |
| 9070 | return ERR_ALERT | ERR_FATAL; |
| 9071 | #endif |
| 9072 | } |
| 9073 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9074 | /* parse the "ca-file" server keyword */ |
| 9075 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9076 | { |
| 9077 | if (!*args[*cur_arg + 1]) { |
| 9078 | if (err) |
| 9079 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 9080 | return ERR_ALERT | ERR_FATAL; |
| 9081 | } |
| 9082 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9083 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9084 | memprintf(&newsrv->ssl_ctx.ca_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9085 | else |
| 9086 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 9087 | |
| 9088 | return 0; |
| 9089 | } |
| 9090 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9091 | /* parse the "check-sni" server keyword */ |
| 9092 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9093 | { |
| 9094 | if (!*args[*cur_arg + 1]) { |
| 9095 | if (err) |
| 9096 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 9097 | return ERR_ALERT | ERR_FATAL; |
| 9098 | } |
| 9099 | |
| 9100 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 9101 | if (!newsrv->check.sni) { |
| 9102 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 9103 | return ERR_ALERT | ERR_FATAL; |
| 9104 | } |
| 9105 | return 0; |
| 9106 | |
| 9107 | } |
| 9108 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9109 | /* parse the "check-ssl" server keyword */ |
| 9110 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9111 | { |
| 9112 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9113 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9114 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9115 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9116 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9117 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9118 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9119 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9120 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 9121 | if (!newsrv->ssl_ctx.methods.min) |
| 9122 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 9123 | if (!newsrv->ssl_ctx.methods.max) |
| 9124 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 9125 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9126 | return 0; |
| 9127 | } |
| 9128 | |
| 9129 | /* parse the "ciphers" server keyword */ |
| 9130 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9131 | { |
| 9132 | if (!*args[*cur_arg + 1]) { |
| 9133 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9134 | return ERR_ALERT | ERR_FATAL; |
| 9135 | } |
| 9136 | |
| 9137 | free(newsrv->ssl_ctx.ciphers); |
| 9138 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 9139 | return 0; |
| 9140 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9141 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9142 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9143 | /* parse the "ciphersuites" server keyword */ |
| 9144 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9145 | { |
| 9146 | if (!*args[*cur_arg + 1]) { |
| 9147 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 9148 | return ERR_ALERT | ERR_FATAL; |
| 9149 | } |
| 9150 | |
| 9151 | free(newsrv->ssl_ctx.ciphersuites); |
| 9152 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 9153 | return 0; |
| 9154 | } |
| 9155 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9156 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9157 | /* parse the "crl-file" server keyword */ |
| 9158 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9159 | { |
| 9160 | #ifndef X509_V_FLAG_CRL_CHECK |
| 9161 | if (err) |
| 9162 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 9163 | return ERR_ALERT | ERR_FATAL; |
| 9164 | #else |
| 9165 | if (!*args[*cur_arg + 1]) { |
| 9166 | if (err) |
| 9167 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 9168 | return ERR_ALERT | ERR_FATAL; |
| 9169 | } |
| 9170 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9171 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 9172 | memprintf(&newsrv->ssl_ctx.crl_file, "%s/%s", global_ssl.ca_base, args[*cur_arg + 1]); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9173 | else |
| 9174 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 9175 | |
| 9176 | return 0; |
| 9177 | #endif |
| 9178 | } |
| 9179 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9180 | /* parse the "crt" server keyword */ |
| 9181 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9182 | { |
| 9183 | if (!*args[*cur_arg + 1]) { |
| 9184 | if (err) |
| 9185 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 9186 | return ERR_ALERT | ERR_FATAL; |
| 9187 | } |
| 9188 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9189 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 9190 | memprintf(&newsrv->ssl_ctx.client_crt, "%s/%s", global_ssl.crt_base, args[*cur_arg + 1]); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 9191 | else |
| 9192 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 9193 | |
| 9194 | return 0; |
| 9195 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9196 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 9197 | /* parse the "no-check-ssl" server keyword */ |
| 9198 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9199 | { |
| 9200 | newsrv->check.use_ssl = 0; |
| 9201 | free(newsrv->ssl_ctx.ciphers); |
| 9202 | newsrv->ssl_ctx.ciphers = NULL; |
| 9203 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 9204 | return 0; |
| 9205 | } |
| 9206 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 9207 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 9208 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9209 | { |
| 9210 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9211 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9212 | return 0; |
| 9213 | } |
| 9214 | |
| 9215 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 9216 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9217 | { |
| 9218 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 9219 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 9220 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 9221 | return 0; |
| 9222 | } |
| 9223 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 9224 | /* parse the "no-ssl" server keyword */ |
| 9225 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9226 | { |
| 9227 | newsrv->use_ssl = 0; |
| 9228 | free(newsrv->ssl_ctx.ciphers); |
| 9229 | newsrv->ssl_ctx.ciphers = NULL; |
| 9230 | return 0; |
| 9231 | } |
| 9232 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9233 | /* parse the "allow-0rtt" server keyword */ |
| 9234 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9235 | { |
| 9236 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 9237 | return 0; |
| 9238 | } |
| 9239 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 9240 | /* parse the "no-ssl-reuse" server keyword */ |
| 9241 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9242 | { |
| 9243 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 9244 | return 0; |
| 9245 | } |
| 9246 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9247 | /* parse the "no-tls-tickets" server keyword */ |
| 9248 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9249 | { |
| 9250 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 9251 | return 0; |
| 9252 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 9253 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 9254 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9255 | { |
| 9256 | newsrv->pp_opts |= SRV_PP_V2; |
| 9257 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 9258 | return 0; |
| 9259 | } |
| 9260 | |
| 9261 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 9262 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9263 | { |
| 9264 | newsrv->pp_opts |= SRV_PP_V2; |
| 9265 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 9266 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 9267 | return 0; |
| 9268 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 9269 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9270 | /* parse the "sni" server keyword */ |
| 9271 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9272 | { |
| 9273 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9274 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 9275 | return ERR_ALERT | ERR_FATAL; |
| 9276 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9277 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9278 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9279 | arg = args[*cur_arg + 1]; |
| 9280 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9281 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 9282 | return ERR_ALERT | ERR_FATAL; |
| 9283 | } |
| 9284 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 9285 | free(newsrv->sni_expr); |
| 9286 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9287 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 9288 | return 0; |
| 9289 | #endif |
| 9290 | } |
| 9291 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9292 | /* parse the "ssl" server keyword */ |
| 9293 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9294 | { |
| 9295 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9296 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 9297 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9298 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9299 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 9300 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9301 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9302 | return 0; |
| 9303 | } |
| 9304 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 9305 | /* parse the "ssl-reuse" server keyword */ |
| 9306 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9307 | { |
| 9308 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 9309 | return 0; |
| 9310 | } |
| 9311 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 9312 | /* parse the "tls-tickets" server keyword */ |
| 9313 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9314 | { |
| 9315 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 9316 | return 0; |
| 9317 | } |
| 9318 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9319 | /* parse the "verify" server keyword */ |
| 9320 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9321 | { |
| 9322 | if (!*args[*cur_arg + 1]) { |
| 9323 | if (err) |
| 9324 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 9325 | return ERR_ALERT | ERR_FATAL; |
| 9326 | } |
| 9327 | |
| 9328 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9329 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9330 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 9331 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9332 | else { |
| 9333 | if (err) |
| 9334 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 9335 | args[*cur_arg], args[*cur_arg + 1]); |
| 9336 | return ERR_ALERT | ERR_FATAL; |
| 9337 | } |
| 9338 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 9339 | return 0; |
| 9340 | } |
| 9341 | |
| 9342 | /* parse the "verifyhost" server keyword */ |
| 9343 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 9344 | { |
| 9345 | if (!*args[*cur_arg + 1]) { |
| 9346 | if (err) |
| 9347 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 9348 | return ERR_ALERT | ERR_FATAL; |
| 9349 | } |
| 9350 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 9351 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 9352 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 9353 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 9354 | return 0; |
| 9355 | } |
| 9356 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9357 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 9358 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 9359 | struct proxy *defpx, const char *file, int line, |
| 9360 | char **err) { |
| 9361 | int i = 1; |
| 9362 | |
| 9363 | if (*(args[i]) == 0) { |
| 9364 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 9365 | return -1; |
| 9366 | } |
| 9367 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9368 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9369 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 9370 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 9371 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9372 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 9373 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 9374 | i++; |
| 9375 | else { |
| 9376 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 9377 | return -1; |
| 9378 | } |
| 9379 | } |
| 9380 | else if (parse_tls_method_options(args[i], &global_ssl.listen_default_sslmethods, err)) { |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9381 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 9382 | return -1; |
| 9383 | } |
| 9384 | i++; |
| 9385 | } |
| 9386 | return 0; |
| 9387 | } |
| 9388 | |
| 9389 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 9390 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 9391 | struct proxy *defpx, const char *file, int line, |
| 9392 | char **err) { |
| 9393 | int i = 1; |
| 9394 | |
| 9395 | if (*(args[i]) == 0) { |
| 9396 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 9397 | return -1; |
| 9398 | } |
| 9399 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9400 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9401 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9402 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 9403 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 9404 | i++; |
| 9405 | else { |
| 9406 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 9407 | return -1; |
| 9408 | } |
| 9409 | } |
| 9410 | else if (parse_tls_method_options(args[i], &global_ssl.connect_default_sslmethods, err)) { |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9411 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 9412 | return -1; |
| 9413 | } |
| 9414 | i++; |
| 9415 | } |
| 9416 | return 0; |
| 9417 | } |
| 9418 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9419 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 9420 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9421 | */ |
| 9422 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 9423 | struct proxy *defpx, const char *file, int line, |
| 9424 | char **err) |
| 9425 | { |
| 9426 | char **target; |
| 9427 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9428 | target = (args[0][1] == 'a') ? &global_ssl.ca_base : &global_ssl.crt_base; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9429 | |
| 9430 | if (too_many_args(1, args, err, NULL)) |
| 9431 | return -1; |
| 9432 | |
| 9433 | if (*target) { |
| 9434 | memprintf(err, "'%s' already specified.", args[0]); |
| 9435 | return -1; |
| 9436 | } |
| 9437 | |
| 9438 | if (*(args[1]) == 0) { |
| 9439 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 9440 | return -1; |
| 9441 | } |
| 9442 | *target = strdup(args[1]); |
| 9443 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9444 | } |
| 9445 | |
| 9446 | /* parse the "ssl-mode-async" keyword in global section. |
| 9447 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9448 | */ |
| 9449 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 9450 | struct proxy *defpx, const char *file, int line, |
| 9451 | char **err) |
| 9452 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9453 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9454 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 9455 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9456 | return 0; |
| 9457 | #else |
| 9458 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 9459 | return -1; |
| 9460 | #endif |
| 9461 | } |
| 9462 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9463 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9464 | static int ssl_check_async_engine_count(void) { |
| 9465 | int err_code = 0; |
| 9466 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 9467 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 9468 | 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] | 9469 | err_code = ERR_ABORT; |
| 9470 | } |
| 9471 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9472 | } |
| 9473 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9474 | /* parse the "ssl-engine" keyword in global section. |
| 9475 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9476 | */ |
| 9477 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 9478 | struct proxy *defpx, const char *file, int line, |
| 9479 | char **err) |
| 9480 | { |
| 9481 | char *algo; |
| 9482 | int ret = -1; |
| 9483 | |
| 9484 | if (*(args[1]) == 0) { |
| 9485 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 9486 | return ret; |
| 9487 | } |
| 9488 | |
| 9489 | if (*(args[2]) == 0) { |
| 9490 | /* if no list of algorithms is given, it defaults to ALL */ |
| 9491 | algo = strdup("ALL"); |
| 9492 | goto add_engine; |
| 9493 | } |
| 9494 | |
| 9495 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 9496 | if (strcmp(args[2], "algo") != 0) { |
| 9497 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 9498 | return ret; |
| 9499 | } |
| 9500 | |
| 9501 | if (*(args[3]) == 0) { |
| 9502 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 9503 | return ret; |
| 9504 | } |
| 9505 | algo = strdup(args[3]); |
| 9506 | |
| 9507 | add_engine: |
| 9508 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 9509 | openssl_engines_initialized++; |
| 9510 | ret = 0; |
| 9511 | } |
| 9512 | free(algo); |
| 9513 | return ret; |
| 9514 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9515 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9516 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9517 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 9518 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 9519 | */ |
| 9520 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 9521 | struct proxy *defpx, const char *file, int line, |
| 9522 | char **err) |
| 9523 | { |
| 9524 | char **target; |
| 9525 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9526 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphers : &global_ssl.connect_default_ciphers; |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9527 | |
| 9528 | if (too_many_args(1, args, err, NULL)) |
| 9529 | return -1; |
| 9530 | |
| 9531 | if (*(args[1]) == 0) { |
| 9532 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 9533 | return -1; |
| 9534 | } |
| 9535 | |
| 9536 | free(*target); |
| 9537 | *target = strdup(args[1]); |
| 9538 | return 0; |
| 9539 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9540 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9541 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9542 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 9543 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 9544 | */ |
| 9545 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 9546 | struct proxy *defpx, const char *file, int line, |
| 9547 | char **err) |
| 9548 | { |
| 9549 | char **target; |
| 9550 | |
| 9551 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 9552 | |
| 9553 | if (too_many_args(1, args, err, NULL)) |
| 9554 | return -1; |
| 9555 | |
| 9556 | if (*(args[1]) == 0) { |
| 9557 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 9558 | return -1; |
| 9559 | } |
| 9560 | |
| 9561 | free(*target); |
| 9562 | *target = strdup(args[1]); |
| 9563 | return 0; |
| 9564 | } |
| 9565 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9566 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9567 | /* parse various global tune.ssl settings consisting in positive integers. |
| 9568 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9569 | */ |
| 9570 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 9571 | struct proxy *defpx, const char *file, int line, |
| 9572 | char **err) |
| 9573 | { |
| 9574 | int *target; |
| 9575 | |
| 9576 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 9577 | target = &global.tune.sslcachesize; |
| 9578 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9579 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9580 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9581 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 9582 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 9583 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9584 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 9585 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9586 | else { |
| 9587 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 9588 | return -1; |
| 9589 | } |
| 9590 | |
| 9591 | if (too_many_args(1, args, err, NULL)) |
| 9592 | return -1; |
| 9593 | |
| 9594 | if (*(args[1]) == 0) { |
| 9595 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 9596 | return -1; |
| 9597 | } |
| 9598 | |
| 9599 | *target = atoi(args[1]); |
| 9600 | if (*target < 0) { |
| 9601 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 9602 | return -1; |
| 9603 | } |
| 9604 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9605 | } |
| 9606 | |
| 9607 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 9608 | struct proxy *defpx, const char *file, int line, |
| 9609 | char **err) |
| 9610 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9611 | int ret; |
| 9612 | |
| 9613 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 9614 | if (ret != 0) |
| 9615 | return ret; |
| 9616 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9617 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9618 | memprintf(err, "'%s' is already configured.", args[0]); |
| 9619 | return -1; |
| 9620 | } |
| 9621 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9622 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 9623 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9624 | memprintf(err, "Out of memory error."); |
| 9625 | return -1; |
| 9626 | } |
| 9627 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9628 | } |
| 9629 | |
| 9630 | /* parse "ssl.force-private-cache". |
| 9631 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9632 | */ |
| 9633 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 9634 | struct proxy *defpx, const char *file, int line, |
| 9635 | char **err) |
| 9636 | { |
| 9637 | if (too_many_args(0, args, err, NULL)) |
| 9638 | return -1; |
| 9639 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9640 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9641 | return 0; |
| 9642 | } |
| 9643 | |
| 9644 | /* parse "ssl.lifetime". |
| 9645 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9646 | */ |
| 9647 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 9648 | struct proxy *defpx, const char *file, int line, |
| 9649 | char **err) |
| 9650 | { |
| 9651 | const char *res; |
| 9652 | |
| 9653 | if (too_many_args(1, args, err, NULL)) |
| 9654 | return -1; |
| 9655 | |
| 9656 | if (*(args[1]) == 0) { |
| 9657 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 9658 | return -1; |
| 9659 | } |
| 9660 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9661 | res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 9662 | if (res == PARSE_TIME_OVER) { |
| 9663 | memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).", |
| 9664 | args[1], args[0]); |
| 9665 | return -1; |
| 9666 | } |
| 9667 | else if (res == PARSE_TIME_UNDER) { |
| 9668 | memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).", |
| 9669 | args[1], args[0]); |
| 9670 | return -1; |
| 9671 | } |
| 9672 | else if (res) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9673 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 9674 | return -1; |
| 9675 | } |
| 9676 | return 0; |
| 9677 | } |
| 9678 | |
| 9679 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 9680 | /* parse "ssl-dh-param-file". |
| 9681 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9682 | */ |
| 9683 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 9684 | struct proxy *defpx, const char *file, int line, |
| 9685 | char **err) |
| 9686 | { |
| 9687 | if (too_many_args(1, args, err, NULL)) |
| 9688 | return -1; |
| 9689 | |
| 9690 | if (*(args[1]) == 0) { |
| 9691 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 9692 | return -1; |
| 9693 | } |
| 9694 | |
| 9695 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 9696 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 9697 | return -1; |
| 9698 | } |
| 9699 | return 0; |
| 9700 | } |
| 9701 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9702 | /* parse "ssl.default-dh-param". |
| 9703 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9704 | */ |
| 9705 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 9706 | struct proxy *defpx, const char *file, int line, |
| 9707 | char **err) |
| 9708 | { |
| 9709 | if (too_many_args(1, args, err, NULL)) |
| 9710 | return -1; |
| 9711 | |
| 9712 | if (*(args[1]) == 0) { |
| 9713 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 9714 | return -1; |
| 9715 | } |
| 9716 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9717 | global_ssl.default_dh_param = atoi(args[1]); |
| 9718 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9719 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 9720 | return -1; |
| 9721 | } |
| 9722 | return 0; |
| 9723 | } |
| 9724 | #endif |
| 9725 | |
| 9726 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9727 | /* This function is used with TLS ticket keys management. It permits to browse |
| 9728 | * each reference. The variable <getnext> must contain the current node, |
| 9729 | * <end> point to the root node. |
| 9730 | */ |
| 9731 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9732 | static inline |
| 9733 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 9734 | { |
| 9735 | struct tls_keys_ref *ref = getnext; |
| 9736 | |
| 9737 | while (1) { |
| 9738 | |
| 9739 | /* Get next list entry. */ |
| 9740 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 9741 | |
| 9742 | /* If the entry is the last of the list, return NULL. */ |
| 9743 | if (&ref->list == end) |
| 9744 | return NULL; |
| 9745 | |
| 9746 | return ref; |
| 9747 | } |
| 9748 | } |
| 9749 | |
| 9750 | static inline |
| 9751 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 9752 | { |
| 9753 | int id; |
| 9754 | char *error; |
| 9755 | |
| 9756 | /* If the reference starts by a '#', this is numeric id. */ |
| 9757 | if (reference[0] == '#') { |
| 9758 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 9759 | id = strtol(reference + 1, &error, 10); |
| 9760 | if (*error != '\0') |
| 9761 | return NULL; |
| 9762 | |
| 9763 | /* Perform the unique id lookup. */ |
| 9764 | return tlskeys_ref_lookupid(id); |
| 9765 | } |
| 9766 | |
| 9767 | /* Perform the string lookup. */ |
| 9768 | return tlskeys_ref_lookup(reference); |
| 9769 | } |
| 9770 | #endif |
| 9771 | |
| 9772 | |
| 9773 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9774 | |
| 9775 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 9776 | |
| 9777 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 9778 | return cli_io_handler_tlskeys_files(appctx); |
| 9779 | } |
| 9780 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9781 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 9782 | * (next index to be dumped), and cli.p0 (next key reference). |
| 9783 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9784 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 9785 | |
| 9786 | struct stream_interface *si = appctx->owner; |
| 9787 | |
| 9788 | switch (appctx->st2) { |
| 9789 | case STAT_ST_INIT: |
| 9790 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 9791 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9792 | * later and restart at the state "STAT_ST_INIT". |
| 9793 | */ |
| 9794 | chunk_reset(&trash); |
| 9795 | |
| 9796 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 9797 | chunk_appendf(&trash, "# id secret\n"); |
| 9798 | else |
| 9799 | chunk_appendf(&trash, "# id (file)\n"); |
| 9800 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9801 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9802 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9803 | return 0; |
| 9804 | } |
| 9805 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9806 | /* Now, we start the browsing of the references lists. |
| 9807 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 9808 | * available field of this pointer is <list>. It is used with the function |
| 9809 | * tlskeys_list_get_next() for retruning the first available entry |
| 9810 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9811 | if (appctx->ctx.cli.p0 == NULL) { |
| 9812 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 9813 | appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9814 | } |
| 9815 | |
| 9816 | appctx->st2 = STAT_ST_LIST; |
| 9817 | /* fall through */ |
| 9818 | |
| 9819 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9820 | while (appctx->ctx.cli.p0) { |
| 9821 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9822 | |
| 9823 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9824 | if (appctx->io_handler == cli_io_handler_tlskeys_entries && appctx->ctx.cli.i1 == 0) |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9825 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9826 | |
| 9827 | if (appctx->ctx.cli.i1 == 0) |
| 9828 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 9829 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9830 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9831 | int head; |
| 9832 | |
| 9833 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 9834 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9835 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9836 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9837 | |
| 9838 | chunk_reset(t2); |
| 9839 | /* 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] | 9840 | if (ref->key_size_bits == 128) { |
| 9841 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 9842 | sizeof(struct tls_sess_key_128), |
| 9843 | t2->area, t2->size); |
| 9844 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 9845 | t2->area); |
| 9846 | } |
| 9847 | else if (ref->key_size_bits == 256) { |
| 9848 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 9849 | sizeof(struct tls_sess_key_256), |
| 9850 | t2->area, t2->size); |
| 9851 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 9852 | t2->area); |
| 9853 | } |
| 9854 | else { |
| 9855 | /* This case should never happen */ |
| 9856 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 9857 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9858 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9859 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9860 | /* let's try again later from this stream. We add ourselves into |
| 9861 | * this stream's users so that it can remove us upon termination. |
| 9862 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9863 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9864 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9865 | return 0; |
| 9866 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9867 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9868 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9869 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9870 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9871 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9872 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9873 | /* let's try again later from this stream. We add ourselves into |
| 9874 | * this stream's users so that it can remove us upon termination. |
| 9875 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9876 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9877 | return 0; |
| 9878 | } |
| 9879 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9880 | if (appctx->ctx.cli.i0 == 0) /* don't display everything if not necessary */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9881 | break; |
| 9882 | |
| 9883 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9884 | appctx->ctx.cli.p0 = tlskeys_list_get_next(appctx->ctx.cli.p0, &tlskeys_reference); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9885 | } |
| 9886 | |
| 9887 | appctx->st2 = STAT_ST_FIN; |
| 9888 | /* fall through */ |
| 9889 | |
| 9890 | default: |
| 9891 | appctx->st2 = STAT_ST_FIN; |
| 9892 | return 1; |
| 9893 | } |
| 9894 | return 0; |
| 9895 | } |
| 9896 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9897 | /* sets cli.i0 to non-zero if only file lists should be dumped */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9898 | 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] | 9899 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9900 | /* no parameter, shows only file list */ |
| 9901 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9902 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9903 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 9904 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9905 | } |
| 9906 | |
| 9907 | if (args[2][0] == '*') { |
| 9908 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9909 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9910 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9911 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9912 | if (!appctx->ctx.cli.p0) |
| 9913 | 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] | 9914 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9915 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 9916 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9917 | } |
| 9918 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9919 | 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] | 9920 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9921 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9922 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9923 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9924 | /* 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] | 9925 | if (!*args[3] || !*args[4]) |
| 9926 | 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] | 9927 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9928 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9929 | if (!ref) |
| 9930 | 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] | 9931 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9932 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9933 | if (ret < 0) |
| 9934 | 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] | 9935 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9936 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9937 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 9938 | 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] | 9939 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9940 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9941 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9942 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9943 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9944 | |
| 9945 | /* Type of SSL payloads that can be updated over the CLI */ |
| 9946 | |
| 9947 | enum { |
| 9948 | CERT_TYPE_PEM = 0, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 9949 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9950 | CERT_TYPE_OCSP, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 9951 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9952 | CERT_TYPE_ISSUER, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 9953 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9954 | CERT_TYPE_SCTL, |
Emmanuel Hocdet | f6ac4fa | 2019-10-30 17:41:27 +0100 | [diff] [blame] | 9955 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9956 | CERT_TYPE_MAX, |
| 9957 | }; |
| 9958 | |
| 9959 | struct { |
| 9960 | const char *ext; |
| 9961 | int type; |
| 9962 | int (*load)(const char *path, char *payload, struct cert_key_and_chain *ckch, char **err); |
| 9963 | /* add a parsing callback */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 9964 | } cert_exts[CERT_TYPE_MAX+1] = { |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9965 | [CERT_TYPE_PEM] = { "", CERT_TYPE_PEM, &ssl_sock_load_pem_into_ckch }, /* default mode, no extensions */ |
William Lallemand | 541a534 | 2019-10-23 14:11:54 +0200 | [diff] [blame] | 9966 | #if ((defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) || defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9967 | [CERT_TYPE_OCSP] = { "ocsp", CERT_TYPE_OCSP, &ssl_sock_load_ocsp_response_from_file }, |
William Lallemand | 541a534 | 2019-10-23 14:11:54 +0200 | [diff] [blame] | 9968 | #endif |
| 9969 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9970 | [CERT_TYPE_SCTL] = { "sctl", CERT_TYPE_SCTL, &ssl_sock_load_sctl_from_file }, |
William Lallemand | 541a534 | 2019-10-23 14:11:54 +0200 | [diff] [blame] | 9971 | #endif |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9972 | [CERT_TYPE_ISSUER] = { "issuer", CERT_TYPE_ISSUER, &ssl_sock_load_issuer_file_into_ckch }, |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 9973 | [CERT_TYPE_MAX] = { NULL, CERT_TYPE_MAX, NULL }, |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 9974 | }; |
| 9975 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 9976 | /* states of the CLI IO handler for 'set ssl cert' */ |
| 9977 | enum { |
| 9978 | SETCERT_ST_INIT = 0, |
| 9979 | SETCERT_ST_GEN, |
| 9980 | SETCERT_ST_INSERT, |
| 9981 | SETCERT_ST_FIN, |
| 9982 | }; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 9983 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 9984 | /* release function of the `set ssl cert' command, free things and unlock the spinlock */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 9985 | static void cli_release_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 9986 | { |
| 9987 | struct ckch_store *new_ckchs; |
| 9988 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 9989 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 9990 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 9991 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 9992 | if (appctx->st2 != SETCERT_ST_FIN) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 9993 | /* free every new sni_ctx and the new store, which are not in the trees so no spinlock there */ |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 9994 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 9995 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 9996 | if (!new_ckchs) |
| 9997 | return; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 9998 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 9999 | /* if the allocation failed, we need to free everything from the temporary list */ |
| 10000 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 10001 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10002 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10003 | list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) { |
| 10004 | if (sc0->order == 0) /* we only free if it's the first inserted */ |
| 10005 | SSL_CTX_free(sc0->ctx); |
| 10006 | LIST_DEL(&sc0->by_ckch_inst); |
| 10007 | free(sc0); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10008 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10009 | LIST_DEL(&ckchi->by_ckchs); |
| 10010 | free(ckchi); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10011 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10012 | ckchs_free(new_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10013 | } |
| 10014 | } |
| 10015 | |
| 10016 | |
| 10017 | /* |
| 10018 | * This function tries to create the new ckch_inst and their SNIs |
| 10019 | */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10020 | static int cli_io_handler_commit_cert(struct appctx *appctx) |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10021 | { |
| 10022 | struct stream_interface *si = appctx->owner; |
| 10023 | int y = 0; |
| 10024 | char *err = NULL; |
| 10025 | int errcode = 0; |
| 10026 | struct ckch_store *old_ckchs, *new_ckchs = NULL; |
| 10027 | struct ckch_inst *ckchi, *ckchis; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10028 | struct buffer *trash = alloc_trash_chunk(); |
| 10029 | |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 10030 | if (trash == NULL) |
| 10031 | goto error; |
| 10032 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10033 | if (unlikely(si_ic(si)->flags & (CF_WRITE_ERROR|CF_SHUTW))) |
| 10034 | goto error; |
| 10035 | |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10036 | while (1) { |
| 10037 | switch (appctx->st2) { |
| 10038 | case SETCERT_ST_INIT: |
| 10039 | /* This state just print the update message */ |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10040 | chunk_printf(trash, "Committing %s", ckchs_transaction.path); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10041 | if (ci_putchk(si_ic(si), trash) == -1) { |
| 10042 | si_rx_room_blk(si); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10043 | goto yield; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10044 | } |
| 10045 | appctx->st2 = SETCERT_ST_GEN; |
| 10046 | /* fallthrough */ |
| 10047 | case SETCERT_ST_GEN: |
| 10048 | /* |
| 10049 | * This state generates the ckch instances with their |
| 10050 | * sni_ctxs and SSL_CTX. |
| 10051 | * |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10052 | * Since the SSL_CTX generation can be CPU consumer, we |
| 10053 | * yield every 10 instances. |
| 10054 | */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10055 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10056 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 10057 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10058 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10059 | if (!new_ckchs) |
| 10060 | continue; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10061 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10062 | /* get the next ckchi to regenerate */ |
| 10063 | ckchi = appctx->ctx.ssl.next_ckchi; |
| 10064 | /* we didn't start yet, set it to the first elem */ |
| 10065 | if (ckchi == NULL) |
| 10066 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10067 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10068 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 10069 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 10070 | struct ckch_inst *new_inst; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10071 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10072 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 10073 | if (y >= 10) { |
| 10074 | /* save the next ckchi to compute */ |
| 10075 | appctx->ctx.ssl.next_ckchi = ckchi; |
| 10076 | goto yield; |
| 10077 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10078 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10079 | if (new_ckchs->multi) |
| 10080 | errcode |= ckch_inst_new_load_multi_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err); |
| 10081 | else |
| 10082 | errcode |= ckch_inst_new_load_store(new_ckchs->path, new_ckchs, ckchi->bind_conf, ckchi->ssl_conf, NULL, 0, &new_inst, &err); |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10083 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10084 | if (errcode & ERR_CODE) |
| 10085 | goto error; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10086 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10087 | /* display one dot per new instance */ |
| 10088 | chunk_appendf(trash, "."); |
| 10089 | /* link the new ckch_inst to the duplicate */ |
| 10090 | LIST_ADDQ(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 10091 | y++; |
| 10092 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10093 | appctx->st2 = SETCERT_ST_INSERT; |
| 10094 | /* fallthrough */ |
| 10095 | case SETCERT_ST_INSERT: |
| 10096 | /* The generation is finished, we can insert everything */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10097 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10098 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 10099 | new_ckchs = appctx->ctx.ssl.new_ckchs; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10100 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10101 | if (!new_ckchs) |
| 10102 | continue; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10103 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10104 | /* First, we insert every new SNIs in the trees */ |
| 10105 | list_for_each_entry_safe(ckchi, ckchis, &new_ckchs->ckch_inst, by_ckchs) { |
| 10106 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 10107 | ssl_sock_load_cert_sni(ckchi, ckchi->bind_conf); |
| 10108 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 10109 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10110 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10111 | /* delete the old sni_ctx, the old ckch_insts and the ckch_store */ |
| 10112 | list_for_each_entry_safe(ckchi, ckchis, &old_ckchs->ckch_inst, by_ckchs) { |
| 10113 | struct sni_ctx *sc0, *sc0s; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10114 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10115 | HA_RWLOCK_WRLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 10116 | list_for_each_entry_safe(sc0, sc0s, &ckchi->sni_ctx, by_ckch_inst) { |
| 10117 | ebmb_delete(&sc0->name); |
| 10118 | LIST_DEL(&sc0->by_ckch_inst); |
| 10119 | free(sc0); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10120 | } |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10121 | HA_RWLOCK_WRUNLOCK(SNI_LOCK, &ckchi->bind_conf->sni_lock); |
| 10122 | LIST_DEL(&ckchi->by_ckchs); |
| 10123 | free(ckchi); |
| 10124 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10125 | |
William Lallemand | beea2a4 | 2019-10-30 17:45:33 +0100 | [diff] [blame] | 10126 | /* Replace the old ckchs by the new one */ |
| 10127 | ebmb_delete(&old_ckchs->node); |
| 10128 | ckchs_free(old_ckchs); |
| 10129 | ebst_insert(&ckchs_tree, &new_ckchs->node); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10130 | appctx->st2 = SETCERT_ST_FIN; |
| 10131 | /* fallthrough */ |
| 10132 | case SETCERT_ST_FIN: |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10133 | /* we achieved the transaction, we can set everything to NULL */ |
| 10134 | free(ckchs_transaction.path); |
| 10135 | ckchs_transaction.path = NULL; |
| 10136 | ckchs_transaction.new_ckchs = NULL; |
| 10137 | ckchs_transaction.old_ckchs = NULL; |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10138 | goto end; |
| 10139 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10140 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10141 | end: |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10142 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10143 | chunk_appendf(trash, "\nSuccess!\n"); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10144 | if (ci_putchk(si_ic(si), trash) == -1) |
| 10145 | si_rx_room_blk(si); |
| 10146 | free_trash_chunk(trash); |
| 10147 | /* success: call the release function and don't come back */ |
| 10148 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10149 | yield: |
| 10150 | /* store the state */ |
| 10151 | if (ci_putchk(si_ic(si), trash) == -1) |
| 10152 | si_rx_room_blk(si); |
| 10153 | free_trash_chunk(trash); |
| 10154 | si_rx_endp_more(si); /* let's come back later */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10155 | return 0; /* should come back */ |
| 10156 | |
| 10157 | error: |
| 10158 | /* spin unlock and free are done in the release function */ |
William Lallemand | 33cc76f | 2019-10-31 11:43:45 +0100 | [diff] [blame] | 10159 | if (trash) { |
| 10160 | chunk_appendf(trash, "\n%sFailed!\n", err); |
| 10161 | if (ci_putchk(si_ic(si), trash) == -1) |
| 10162 | si_rx_room_blk(si); |
| 10163 | free_trash_chunk(trash); |
| 10164 | } |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10165 | /* error: call the release function and don't come back */ |
| 10166 | return 1; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10167 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10168 | |
| 10169 | /* |
| 10170 | * Parsing function of 'commit ssl cert' |
| 10171 | */ |
| 10172 | static int cli_parse_commit_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 10173 | { |
| 10174 | char *err = NULL; |
| 10175 | |
| 10176 | if (!*args[3]) |
| 10177 | return cli_err(appctx, "'commit ssl cert expects a filename\n"); |
| 10178 | |
| 10179 | /* The operations on the CKCH architecture are locked so we can |
| 10180 | * manipulate ckch_store and ckch_inst */ |
| 10181 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 10182 | return cli_err(appctx, "Can't commit the certificate!\nOperations on certificates are currently locked!\n"); |
| 10183 | |
| 10184 | if (!ckchs_transaction.path) { |
| 10185 | memprintf(&err, "No ongoing transaction! !\n"); |
| 10186 | goto error; |
| 10187 | } |
| 10188 | |
| 10189 | if (strcmp(ckchs_transaction.path, args[3]) != 0) { |
| 10190 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, args[3]); |
| 10191 | goto error; |
| 10192 | } |
| 10193 | |
| 10194 | /* init the appctx structure */ |
| 10195 | appctx->st2 = SETCERT_ST_INIT; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10196 | appctx->ctx.ssl.next_ckchi = NULL; |
| 10197 | appctx->ctx.ssl.new_ckchs = ckchs_transaction.new_ckchs; |
| 10198 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.old_ckchs; |
| 10199 | |
| 10200 | /* we don't unlock there, it will be unlock after the IO handler, in the release handler */ |
| 10201 | return 0; |
| 10202 | |
| 10203 | error: |
| 10204 | |
| 10205 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 10206 | err = memprintf(&err, "%sCan't commit %s!\n", err ? err : "", args[3]); |
| 10207 | |
| 10208 | return cli_dynerr(appctx, err); |
| 10209 | } |
| 10210 | |
| 10211 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10212 | /* |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10213 | * Parsing function of `set ssl cert`, it updates or creates a temporary ckch. |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10214 | */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10215 | static int cli_parse_set_cert(char **args, char *payload, struct appctx *appctx, void *private) |
| 10216 | { |
William Lallemand | 0c3b7d9 | 2019-10-18 11:27:07 +0200 | [diff] [blame] | 10217 | struct ckch_store *new_ckchs = NULL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10218 | struct ckch_store *old_ckchs = NULL; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10219 | char *err = NULL; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 10220 | int i; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 10221 | int bundle = -1; /* TRUE if >= 0 (ckch index) */ |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 10222 | int errcode = 0; |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10223 | char *end; |
| 10224 | int type = CERT_TYPE_PEM; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10225 | struct cert_key_and_chain *ckch; |
| 10226 | struct buffer *buf; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10227 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10228 | if ((buf = alloc_trash_chunk()) == NULL) |
| 10229 | return cli_err(appctx, "Can't allocate memory\n"); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10230 | |
| 10231 | if (!*args[3] || !payload) |
| 10232 | return cli_err(appctx, "'set ssl cert expects a filename and a certificat as a payload\n"); |
| 10233 | |
| 10234 | /* The operations on the CKCH architecture are locked so we can |
| 10235 | * manipulate ckch_store and ckch_inst */ |
| 10236 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 10237 | return cli_err(appctx, "Can't update the certificate!\nOperations on certificates are currently locked!\n"); |
| 10238 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10239 | if (!chunk_strcpy(buf, args[3])) { |
| 10240 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 10241 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10242 | goto end; |
| 10243 | } |
| 10244 | |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10245 | /* check which type of file we want to update */ |
William Lallemand | f29cdef | 2019-10-23 15:00:52 +0200 | [diff] [blame] | 10246 | for (i = 0; cert_exts[i].type < CERT_TYPE_MAX; i++) { |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10247 | end = strrchr(buf->area, '.'); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10248 | if (end && *cert_exts[i].ext && (!strcmp(end + 1, cert_exts[i].ext))) { |
| 10249 | *end = '\0'; |
| 10250 | type = cert_exts[i].type; |
| 10251 | break; |
| 10252 | } |
| 10253 | } |
| 10254 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10255 | appctx->ctx.ssl.old_ckchs = NULL; |
| 10256 | appctx->ctx.ssl.new_ckchs = NULL; |
William Lallemand | 849eed6 | 2019-10-17 16:23:50 +0200 | [diff] [blame] | 10257 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10258 | /* if there is an ongoing transaction */ |
| 10259 | if (ckchs_transaction.path) { |
| 10260 | /* if the ongoing transaction is a bundle, we need to find which part of the bundle need to be updated */ |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 10261 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10262 | if (ckchs_transaction.new_ckchs->multi) { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 10263 | char *end; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 10264 | int j; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10265 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10266 | /* check if it was used in a bundle by removing the |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 10267 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10268 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 10269 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 10270 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 10271 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 10272 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 10273 | break; |
| 10274 | } |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10275 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10276 | if (bundle < 0) { |
| 10277 | memprintf(&err, "The ongoing transaction is the '%s' bundle. You need to specify which part of the bundle you want to update ('%s.{rsa,ecdsa,dsa}')\n", ckchs_transaction.path, buf->area); |
| 10278 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10279 | goto end; |
| 10280 | } |
| 10281 | } |
| 10282 | #endif |
| 10283 | |
| 10284 | /* if there is an ongoing transaction, check if this is the same file */ |
| 10285 | if (strcmp(ckchs_transaction.path, buf->area) != 0) { |
| 10286 | memprintf(&err, "The ongoing transaction is about '%s' but you are trying to set '%s'\n", ckchs_transaction.path, buf->area); |
| 10287 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10288 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10289 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10290 | |
| 10291 | appctx->ctx.ssl.old_ckchs = ckchs_transaction.new_ckchs; |
| 10292 | |
| 10293 | } else { |
| 10294 | struct ckch_store *find_ckchs[2] = { NULL, NULL }; |
| 10295 | |
| 10296 | /* lookup for the certificate in the tree: |
| 10297 | * check if this is used as a bundle AND as a unique certificate */ |
| 10298 | for (i = 0; i < 2; i++) { |
| 10299 | |
| 10300 | if ((find_ckchs[i] = ckchs_lookup(buf->area)) != NULL) { |
| 10301 | /* only the bundle name is in the tree and you should |
| 10302 | * never update a bundle name, only a filename */ |
| 10303 | if (bundle < 0 && find_ckchs[i]->multi) { |
| 10304 | /* we tried to look for a non-bundle and we found a bundle */ |
| 10305 | memprintf(&err, "%s%s is a multi-cert bundle. Try updating %s.{dsa,rsa,ecdsa}\n", |
| 10306 | err ? err : "", args[3], args[3]); |
| 10307 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10308 | goto end; |
| 10309 | } |
| 10310 | /* If we want a bundle but this is not a bundle */ |
| 10311 | /* note that it should never happen */ |
| 10312 | if (bundle >= 0 && find_ckchs[i]->multi == 0) |
| 10313 | goto end; |
| 10314 | } |
| 10315 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 10316 | { |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 10317 | char *end; |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10318 | int j; |
| 10319 | |
| 10320 | /* check if it was used in a bundle by removing the |
| 10321 | * .dsa/.rsa/.ecdsa at the end of the filename */ |
| 10322 | end = strrchr(buf->area, '.'); |
Emmanuel Hocdet | 40f2f1e | 2019-10-30 17:31:28 +0100 | [diff] [blame] | 10323 | for (j = 0; end && j < SSL_SOCK_NUM_KEYTYPES; j++) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10324 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 10325 | bundle = j; /* keep the type of certificate so we insert it at the right place */ |
| 10326 | *end = '\0'; /* it's a bundle let's end the string*/ |
| 10327 | break; |
| 10328 | } |
| 10329 | } |
| 10330 | } |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 10331 | #else |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10332 | /* bundles are not supported here, so we don't need to lookup again */ |
| 10333 | break; |
William Lallemand | 963b2e7 | 2019-10-14 11:38:36 +0200 | [diff] [blame] | 10334 | #endif |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10335 | } |
| 10336 | |
| 10337 | if (find_ckchs[0] && find_ckchs[1]) { |
| 10338 | memprintf(&err, "%sUpdating a certificate which is used in the HAProxy configuration as a bundle and as a unique certificate is not supported. ('%s' and '%s')\n", |
| 10339 | err ? err : "", find_ckchs[0]->path, find_ckchs[1]->path); |
| 10340 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10341 | goto end; |
| 10342 | } |
| 10343 | |
| 10344 | appctx->ctx.ssl.old_ckchs = find_ckchs[0] ? find_ckchs[0] : find_ckchs[1]; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10345 | } |
| 10346 | |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10347 | if (!appctx->ctx.ssl.old_ckchs) { |
| 10348 | memprintf(&err, "%sCan't replace a certificate which is not referenced by the configuration!\n", |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10349 | err ? err : ""); |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 10350 | errcode |= ERR_ALERT | ERR_FATAL; |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10351 | goto end; |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10352 | } |
| 10353 | |
William Lallemand | 8a7fdf0 | 2019-11-04 10:59:32 +0100 | [diff] [blame^] | 10354 | if (!appctx->ctx.ssl.path) { |
| 10355 | /* this is a new transaction, set the path of the transaction */ |
| 10356 | appctx->ctx.ssl.path = strdup(appctx->ctx.ssl.old_ckchs->path); |
| 10357 | if (!appctx->ctx.ssl.path) { |
| 10358 | memprintf(&err, "%sCan't allocate memory\n", err ? err : ""); |
| 10359 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10360 | goto end; |
| 10361 | } |
| 10362 | } |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10363 | |
| 10364 | old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 10365 | |
| 10366 | /* TODO: handle filters */ |
| 10367 | if (old_ckchs->filters) { |
| 10368 | memprintf(&err, "%sCertificates used in crt-list with filters are not supported!\n", |
| 10369 | err ? err : ""); |
| 10370 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10371 | goto end; |
| 10372 | } |
| 10373 | |
| 10374 | /* duplicate the ckch store */ |
| 10375 | new_ckchs = ckchs_dup(old_ckchs); |
| 10376 | if (!new_ckchs) { |
| 10377 | memprintf(&err, "%sCannot allocate memory!\n", |
| 10378 | err ? err : ""); |
| 10379 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10380 | goto end; |
| 10381 | } |
| 10382 | |
| 10383 | if (!new_ckchs->multi) |
| 10384 | ckch = new_ckchs->ckch; |
| 10385 | else |
| 10386 | ckch = &new_ckchs->ckch[bundle]; |
| 10387 | |
| 10388 | /* appply the change on the duplicate */ |
| 10389 | if (cert_exts[type].load(buf->area, payload, ckch, &err) != 0) { |
| 10390 | memprintf(&err, "%sCan't load the payload\n", err ? err : ""); |
| 10391 | errcode |= ERR_ALERT | ERR_FATAL; |
| 10392 | goto end; |
| 10393 | } |
| 10394 | |
| 10395 | appctx->ctx.ssl.new_ckchs = new_ckchs; |
| 10396 | |
| 10397 | /* we succeed, we can save the ckchs in the transaction */ |
| 10398 | |
| 10399 | /* if there wasn't a transaction, update the old ckchs */ |
| 10400 | if (!ckchs_transaction.old_ckchs && !ckchs_transaction.old_ckchs) { |
| 10401 | ckchs_transaction.old_ckchs = appctx->ctx.ssl.old_ckchs; |
| 10402 | ckchs_transaction.path = appctx->ctx.ssl.path; |
| 10403 | err = memprintf(&err, "Transaction created for certificate %s!\n", ckchs_transaction.path); |
| 10404 | } else { |
| 10405 | err = memprintf(&err, "Transaction updated for certificate %s!\n", ckchs_transaction.path); |
| 10406 | |
| 10407 | } |
| 10408 | |
| 10409 | /* free the previous ckchs if there was a transaction */ |
| 10410 | ckchs_free(ckchs_transaction.new_ckchs); |
| 10411 | |
| 10412 | ckchs_transaction.new_ckchs = appctx->ctx.ssl.new_ckchs; |
| 10413 | |
| 10414 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10415 | /* creates the SNI ctxs later in the IO handler */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10416 | |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10417 | end: |
| 10418 | free_trash_chunk(buf); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10419 | |
Emeric Brun | f69ed1d | 2019-10-17 11:56:56 +0200 | [diff] [blame] | 10420 | if (errcode & ERR_CODE) { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10421 | |
| 10422 | ckchs_free(appctx->ctx.ssl.new_ckchs); |
| 10423 | appctx->ctx.ssl.new_ckchs = NULL; |
| 10424 | |
| 10425 | appctx->ctx.ssl.old_ckchs = NULL; |
| 10426 | |
| 10427 | free(appctx->ctx.ssl.path); |
| 10428 | appctx->ctx.ssl.path = NULL; |
| 10429 | |
| 10430 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
William Lallemand | 44b3532 | 2019-10-17 16:28:40 +0200 | [diff] [blame] | 10431 | return cli_dynerr(appctx, memprintf(&err, "%sCan't update %s!\n", err ? err : "", args[3])); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10432 | } else { |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10433 | |
| 10434 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 10435 | return cli_dynmsg(appctx, LOG_NOTICE, err); |
William Lallemand | 430413e | 2019-10-28 14:30:47 +0100 | [diff] [blame] | 10436 | } |
William Lallemand | 8f840d7 | 2019-10-23 10:53:05 +0200 | [diff] [blame] | 10437 | /* TODO: handle the ERR_WARN which are not handled because of the io_handler */ |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 10438 | } |
| 10439 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10440 | 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] | 10441 | { |
| 10442 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 10443 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10444 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 10445 | |
| 10446 | if (!payload) |
| 10447 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10448 | |
| 10449 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10450 | if (!*payload) |
| 10451 | 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] | 10452 | |
| 10453 | /* remove \r and \n from the payload */ |
| 10454 | for (i = 0, j = 0; payload[i]; i++) { |
| 10455 | if (payload[i] == '\r' || payload[i] == '\n') |
| 10456 | continue; |
| 10457 | payload[j++] = payload[i]; |
| 10458 | } |
| 10459 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10460 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10461 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10462 | if (ret < 0) |
| 10463 | 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] | 10464 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 10465 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10466 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10467 | if (err) |
| 10468 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 10469 | else |
| 10470 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10471 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10472 | |
| 10473 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10474 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 10475 | 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] | 10476 | #endif |
| 10477 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 10478 | } |
| 10479 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 10480 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 10481 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 10482 | { |
| 10483 | switch (arg->type) { |
| 10484 | case ARGT_STR: |
| 10485 | smp->data.type = SMP_T_STR; |
| 10486 | smp->data.u.str = arg->data.str; |
| 10487 | return 1; |
| 10488 | case ARGT_VAR: |
| 10489 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 10490 | return 0; |
| 10491 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 10492 | return 0; |
| 10493 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 10494 | return 0; |
| 10495 | return 1; |
| 10496 | default: |
| 10497 | return 0; |
| 10498 | } |
| 10499 | } |
| 10500 | |
| 10501 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 10502 | const char *file, int line, char **err) |
| 10503 | { |
| 10504 | switch(args[0].data.sint) { |
| 10505 | case 128: |
| 10506 | case 192: |
| 10507 | case 256: |
| 10508 | break; |
| 10509 | default: |
| 10510 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 10511 | return 0; |
| 10512 | } |
| 10513 | /* Try to decode a variable. */ |
| 10514 | vars_check_arg(&args[1], NULL); |
| 10515 | vars_check_arg(&args[2], NULL); |
| 10516 | vars_check_arg(&args[3], NULL); |
| 10517 | return 1; |
| 10518 | } |
| 10519 | |
| 10520 | /* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */ |
| 10521 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 10522 | { |
| 10523 | struct sample nonce, key, aead_tag; |
| 10524 | struct buffer *smp_trash, *smp_trash_alloc; |
| 10525 | EVP_CIPHER_CTX *ctx; |
| 10526 | int dec_size, ret; |
| 10527 | |
| 10528 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 10529 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 10530 | return 0; |
| 10531 | |
| 10532 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 10533 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 10534 | return 0; |
| 10535 | |
| 10536 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 10537 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 10538 | return 0; |
| 10539 | |
| 10540 | smp_trash = get_trash_chunk(); |
| 10541 | smp_trash_alloc = alloc_trash_chunk(); |
| 10542 | if (!smp_trash_alloc) |
| 10543 | return 0; |
| 10544 | |
| 10545 | ctx = EVP_CIPHER_CTX_new(); |
| 10546 | |
| 10547 | if (!ctx) |
| 10548 | goto err; |
| 10549 | |
| 10550 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 10551 | if (dec_size < 0) |
| 10552 | goto err; |
| 10553 | smp_trash->data = dec_size; |
| 10554 | |
| 10555 | /* Set cipher type and mode */ |
| 10556 | switch(arg_p[0].data.sint) { |
| 10557 | case 128: |
| 10558 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 10559 | break; |
| 10560 | case 192: |
| 10561 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 10562 | break; |
| 10563 | case 256: |
| 10564 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 10565 | break; |
| 10566 | } |
| 10567 | |
| 10568 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 10569 | |
| 10570 | /* Initialise IV */ |
| 10571 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 10572 | goto err; |
| 10573 | |
| 10574 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 10575 | if (dec_size < 0) |
| 10576 | goto err; |
| 10577 | smp_trash->data = dec_size; |
| 10578 | |
| 10579 | /* Initialise key */ |
| 10580 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 10581 | goto err; |
| 10582 | |
| 10583 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 10584 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 10585 | goto err; |
| 10586 | |
| 10587 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 10588 | if (dec_size < 0) |
| 10589 | goto err; |
| 10590 | smp_trash_alloc->data = dec_size; |
| 10591 | dec_size = smp_trash->data; |
| 10592 | |
| 10593 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 10594 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 10595 | |
| 10596 | if (ret <= 0) |
| 10597 | goto err; |
| 10598 | |
| 10599 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 10600 | smp->data.u.str.area = smp_trash->area; |
| 10601 | smp->data.type = SMP_T_BIN; |
| 10602 | smp->flags &= ~SMP_F_CONST; |
| 10603 | free_trash_chunk(smp_trash_alloc); |
| 10604 | return 1; |
| 10605 | |
| 10606 | err: |
| 10607 | free_trash_chunk(smp_trash_alloc); |
| 10608 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10609 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 10610 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10611 | |
| 10612 | /* register cli keywords */ |
| 10613 | static struct cli_kw_list cli_kws = {{ },{ |
| 10614 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 10615 | { { "show", "tls-keys", NULL }, "show tls-keys [id|*]: show tls keys references or dump tls ticket keys when id specified", cli_parse_show_tlskeys, NULL }, |
Lukas Tribus | f4bbc43 | 2017-10-24 12:26:31 +0200 | [diff] [blame] | 10616 | { { "set", "ssl", "tls-key", NULL }, "set ssl tls-key [id|keyfile] <tlskey>: set the next TLS key for the <id> or <keyfile> listener to <tlskey>", cli_parse_set_tlskeys, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10617 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 10618 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | bc6ca7c | 2019-10-29 23:48:19 +0100 | [diff] [blame] | 10619 | { { "set", "ssl", "cert", NULL }, "set ssl cert <certfile> <payload> : replace a certificate file", cli_parse_set_cert, NULL, NULL }, |
| 10620 | { { "commit", "ssl", "cert", NULL }, "commit ssl cert <certfile> : commit a certificate file", cli_parse_commit_cert, cli_io_handler_commit_cert, cli_release_commit_cert }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10621 | { { NULL }, NULL, NULL, NULL } |
| 10622 | }}; |
| 10623 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10624 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 10625 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 10626 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10627 | * Please take care of keeping this list alphabetically sorted. |
| 10628 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10629 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 10630 | { "ssl_bc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10631 | { "ssl_bc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 10632 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 10633 | { "ssl_bc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 10634 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 10635 | { "ssl_bc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 10636 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 10637 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 10638 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 10639 | { "ssl_bc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5SRV }, |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 10640 | { "ssl_bc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 10641 | { "ssl_bc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10642 | { "ssl_bc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5SRV }, |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 10643 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 10644 | { "ssl_bc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 10645 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10646 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 10647 | { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 10648 | { "ssl_bc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 10649 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 10650 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10651 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 10652 | { "ssl_c_ca_err_depth", smp_fetch_ssl_c_ca_err_depth, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 10653 | { "ssl_c_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10654 | { "ssl_c_err", smp_fetch_ssl_c_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 10655 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10656 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10657 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10658 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10659 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10660 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10661 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 10662 | { "ssl_c_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 10663 | { "ssl_c_used", smp_fetch_ssl_c_used, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10664 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 10665 | { "ssl_c_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 10666 | { "ssl_f_der", smp_fetch_ssl_x_der, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 10667 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10668 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10669 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10670 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10671 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10672 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10673 | { "ssl_f_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Emeric Brun | 55f4fa8 | 2014-04-30 17:11:25 +0200 | [diff] [blame] | 10674 | { "ssl_f_sha1", smp_fetch_ssl_x_sha1, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10675 | { "ssl_f_version", smp_fetch_ssl_x_version, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 10676 | { "ssl_fc", smp_fetch_ssl_fc, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10677 | { "ssl_fc_alg_keysize", smp_fetch_ssl_fc_alg_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10678 | { "ssl_fc_cipher", smp_fetch_ssl_fc_cipher, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 10679 | { "ssl_fc_has_crt", smp_fetch_ssl_fc_has_crt, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10680 | { "ssl_fc_has_early", smp_fetch_ssl_fc_has_early, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Willy Tarreau | 80aca90 | 2013-01-07 15:42:20 +0100 | [diff] [blame] | 10681 | { "ssl_fc_has_sni", smp_fetch_ssl_fc_has_sni, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 10682 | { "ssl_fc_is_resumed", smp_fetch_ssl_fc_is_resumed, 0, NULL, SMP_T_BOOL, SMP_USE_L5CLI }, |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 10683 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10684 | { "ssl_fc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 10685 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 10686 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10687 | { "ssl_fc_alpn", smp_fetch_ssl_fc_alpn, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 10688 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10689 | { "ssl_fc_protocol", smp_fetch_ssl_fc_protocol, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 10690 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 10691 | { "ssl_fc_unique_id", smp_fetch_ssl_fc_unique_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 10692 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 10693 | { "ssl_fc_use_keysize", smp_fetch_ssl_fc_use_keysize, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 10694 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10695 | { "ssl_fc_session_id", smp_fetch_ssl_fc_session_id, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 10696 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10697 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 10698 | { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 10699 | { "ssl_fc_server_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 10700 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 10701 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 10702 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 10703 | { "ssl_fc_sni", smp_fetch_ssl_fc_sni, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 10704 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10705 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10706 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 10707 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 10708 | { "ssl_fc_cipherlist_xxh", smp_fetch_ssl_fc_cl_xxh64, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 10709 | { NULL, NULL, 0, 0, 0 }, |
| 10710 | }}; |
| 10711 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10712 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 10713 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 10714 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10715 | * Please take care of keeping this list alphabetically sorted. |
| 10716 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 10717 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 10718 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 10719 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 10720 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 10721 | }}; |
| 10722 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10723 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 10724 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 10725 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10726 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 10727 | * all code contributors. |
| 10728 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 10729 | * the config parser can report an appropriate error when a known keyword was |
| 10730 | * not enabled. |
| 10731 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 10732 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 10733 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 10734 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 10735 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 10736 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10737 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10738 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 10739 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 10740 | { "crl-file", ssl_bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 10741 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 10742 | { "ecdhe", ssl_bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 10743 | { "no-ca-names", ssl_bind_parse_no_ca_names, 0 }, /* do not send ca names to clients (ca_file related) */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 10744 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 10745 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 10746 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 10747 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 10748 | { NULL, NULL, 0 }, |
| 10749 | }; |
| 10750 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10751 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 10752 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 10753 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 10754 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10755 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 10756 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 10757 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 10758 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 10759 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 10760 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10761 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10762 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 10763 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10764 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 10765 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 10766 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 10767 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 10768 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 10769 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 10770 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 10771 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 10772 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 10773 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 10774 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10775 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 10776 | { "no-ca-names", bind_parse_no_ca_names, 0 }, /* do not send ca names to clients (ca_file related) */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10777 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 10778 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 10779 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 10780 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 10781 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10782 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 10783 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10784 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 10785 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 10786 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 10787 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 10788 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 10789 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 10790 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 10791 | { NULL, NULL, 0 }, |
| 10792 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10793 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10794 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 10795 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 10796 | /* Note: must not be declared <const> as its list will be overwritten. |
| 10797 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 10798 | * all code contributors. |
| 10799 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 10800 | * the config parser can report an appropriate error when a known keyword was |
| 10801 | * not enabled. |
| 10802 | */ |
| 10803 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 10804 | { "allow-0rtt", srv_parse_allow_0rtt, 0, 1 }, /* Allow using early data on this server */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 10805 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10806 | { "ca-file", srv_parse_ca_file, 1, 1 }, /* set CAfile to process verify server cert */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 10807 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 10808 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10809 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 10810 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10811 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10812 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 10813 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10814 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 10815 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 10816 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 10817 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 10818 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 10819 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 10820 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 10821 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 10822 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 10823 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 10824 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 10825 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 10826 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 10827 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 10828 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 10829 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 10830 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 10831 | { "no-tls-tickets", srv_parse_no_tls_tickets, 0, 1 }, /* disable session resumption tickets */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 10832 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 10833 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 10834 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 10835 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 10836 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 10837 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 10838 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 10839 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 10840 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 10841 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 10842 | { "verifyhost", srv_parse_verifyhost, 1, 1 }, /* require that SSL cert verifies for hostname */ |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 10843 | { NULL, NULL, 0, 0 }, |
| 10844 | }}; |
| 10845 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10846 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 10847 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 10848 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 10849 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 10850 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 10851 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 10852 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 10853 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 10854 | #ifndef OPENSSL_NO_DH |
| 10855 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 10856 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 10857 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10858 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10859 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10860 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 10861 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 10862 | #ifndef OPENSSL_NO_DH |
| 10863 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 10864 | #endif |
| 10865 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 10866 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 10867 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 10868 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 10869 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 10870 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 10871 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10872 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10873 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 10874 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 10875 | #endif |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 10876 | { 0, NULL, NULL }, |
| 10877 | }}; |
| 10878 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10879 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 10880 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 10881 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 10882 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 10883 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 10884 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 10885 | #endif |
| 10886 | { NULL, NULL, 0, 0, 0 }, |
| 10887 | }}; |
| 10888 | |
| 10889 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 10890 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 10891 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 10892 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10893 | .snd_buf = ssl_sock_from_buf, |
| 10894 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 10895 | .subscribe = ssl_subscribe, |
| 10896 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 10897 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 10898 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10899 | .rcv_pipe = NULL, |
| 10900 | .snd_pipe = NULL, |
| 10901 | .shutr = NULL, |
| 10902 | .shutw = ssl_sock_shutw, |
| 10903 | .close = ssl_sock_close, |
| 10904 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 10905 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 10906 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 10907 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 10908 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 10909 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 10910 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10911 | }; |
| 10912 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10913 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 10914 | struct session *sess, struct stream *s, int flags) |
| 10915 | { |
| 10916 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 10917 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10918 | |
| 10919 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 10920 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10921 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 10922 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10923 | if (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_SSL_WAIT_HS)) { |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 10924 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 10925 | s->req.flags |= CF_READ_NULL; |
| 10926 | return ACT_RET_YIELD; |
| 10927 | } |
| 10928 | } |
| 10929 | return (ACT_RET_CONT); |
| 10930 | } |
| 10931 | |
| 10932 | 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) |
| 10933 | { |
| 10934 | rule->action_ptr = ssl_action_wait_for_hs; |
| 10935 | |
| 10936 | return ACT_RET_PRS_OK; |
| 10937 | } |
| 10938 | |
| 10939 | static struct action_kw_list http_req_actions = {ILH, { |
| 10940 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 10941 | { /* END */ } |
| 10942 | }}; |
| 10943 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 10944 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 10945 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10946 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 10947 | |
| 10948 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 10949 | { |
| 10950 | if (ptr) { |
| 10951 | chunk_destroy(ptr); |
| 10952 | free(ptr); |
| 10953 | } |
| 10954 | } |
| 10955 | |
| 10956 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 10957 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 10958 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10959 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 10960 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 10961 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10962 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 10963 | static void __ssl_sock_init(void) |
| 10964 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 10965 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10966 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 10967 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 10968 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10969 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 10970 | if (global_ssl.listen_default_ciphers) |
| 10971 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 10972 | if (global_ssl.connect_default_ciphers) |
| 10973 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 10974 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 10975 | if (global_ssl.listen_default_ciphersuites) |
| 10976 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 10977 | if (global_ssl.connect_default_ciphersuites) |
| 10978 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 10979 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 10980 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 10981 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 10982 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10983 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 10984 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 10985 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10986 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 10987 | n = sk_SSL_COMP_num(cm); |
| 10988 | while (n--) { |
| 10989 | (void) sk_SSL_COMP_pop(cm); |
| 10990 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 10991 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 10992 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10993 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 10994 | ssl_locking_init(); |
| 10995 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10996 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL && !defined OPENSSL_NO_TLSEXT && !defined OPENSSL_IS_BORINGSSL) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 10997 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 10998 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 10999 | 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] | 11000 | ssl_capture_ptr_index = SSL_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_capture_free_func); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 11001 | ssl_pkey_info_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 11002 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 11003 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 11004 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 11005 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 11006 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 11007 | hap_register_post_check(tlskeys_finalize_config); |
| 11008 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11009 | |
| 11010 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 11011 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 11012 | |
| 11013 | #ifndef OPENSSL_NO_DH |
| 11014 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 11015 | hap_register_post_deinit(ssl_free_dh); |
| 11016 | #endif |
| 11017 | #ifndef OPENSSL_NO_ENGINE |
| 11018 | hap_register_post_deinit(ssl_free_engines); |
| 11019 | #endif |
| 11020 | /* Load SSL string for the verbose & debug mode. */ |
| 11021 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 11022 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 11023 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 11024 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 11025 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 11026 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 11027 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 11028 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 11029 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
William Lallemand | 150bfa8 | 2019-09-19 17:12:49 +0200 | [diff] [blame] | 11030 | |
| 11031 | HA_SPIN_INIT(&ckch_lock); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11032 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 11033 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11034 | /* Compute and register the version string */ |
| 11035 | static void ssl_register_build_options() |
| 11036 | { |
| 11037 | char *ptr = NULL; |
| 11038 | int i; |
| 11039 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11040 | memprintf(&ptr, "Built with OpenSSL version : " |
| 11041 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 11042 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11043 | #else /* OPENSSL_IS_BORINGSSL */ |
| 11044 | OPENSSL_VERSION_TEXT |
| 11045 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 11046 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 11047 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11048 | #endif |
| 11049 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 11050 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11051 | "no (library version too old)" |
| 11052 | #elif defined(OPENSSL_NO_TLSEXT) |
| 11053 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 11054 | #else |
| 11055 | "yes" |
| 11056 | #endif |
| 11057 | "", ptr); |
| 11058 | |
| 11059 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 11060 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 11061 | "yes" |
| 11062 | #else |
| 11063 | #ifdef OPENSSL_NO_TLSEXT |
| 11064 | "no (because of OPENSSL_NO_TLSEXT)" |
| 11065 | #else |
| 11066 | "no (version might be too old, 0.9.8f min needed)" |
| 11067 | #endif |
| 11068 | #endif |
| 11069 | "", ptr); |
| 11070 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 11071 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 11072 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 11073 | if (methodVersions[i].option) |
| 11074 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 11075 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11076 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11077 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 11078 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 11079 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 11080 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11081 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 11082 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 11083 | void ssl_free_engines(void) { |
| 11084 | struct ssl_engine_list *wl, *wlb; |
| 11085 | /* free up engine list */ |
| 11086 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 11087 | ENGINE_finish(wl->e); |
| 11088 | ENGINE_free(wl->e); |
| 11089 | LIST_DEL(&wl->list); |
| 11090 | free(wl); |
| 11091 | } |
| 11092 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 11093 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 11094 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 11095 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 11096 | void ssl_free_dh(void) { |
| 11097 | if (local_dh_1024) { |
| 11098 | DH_free(local_dh_1024); |
| 11099 | local_dh_1024 = NULL; |
| 11100 | } |
| 11101 | if (local_dh_2048) { |
| 11102 | DH_free(local_dh_2048); |
| 11103 | local_dh_2048 = NULL; |
| 11104 | } |
| 11105 | if (local_dh_4096) { |
| 11106 | DH_free(local_dh_4096); |
| 11107 | local_dh_4096 = NULL; |
| 11108 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 11109 | if (global_dh) { |
| 11110 | DH_free(global_dh); |
| 11111 | global_dh = NULL; |
| 11112 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 11113 | } |
| 11114 | #endif |
| 11115 | |
| 11116 | __attribute__((destructor)) |
| 11117 | static void __ssl_sock_deinit(void) |
| 11118 | { |
| 11119 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 11120 | if (ssl_ctx_lru_tree) { |
| 11121 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 11122 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 11123 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 11124 | #endif |
| 11125 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 11126 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 11127 | ERR_remove_state(0); |
| 11128 | ERR_free_strings(); |
| 11129 | |
| 11130 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 11131 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 11132 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 11133 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 11134 | CRYPTO_cleanup_all_ex_data(); |
| 11135 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 11136 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 11137 | } |
| 11138 | |
| 11139 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 11140 | /* |
| 11141 | * Local variables: |
| 11142 | * c-indent-level: 8 |
| 11143 | * c-basic-offset: 8 |
| 11144 | * End: |
| 11145 | */ |