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 | |
| 356 | |
| 357 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 358 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 359 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 360 | unsigned long long int xxh64; |
| 361 | unsigned char ciphersuite_len; |
| 362 | char ciphersuite[0]; |
| 363 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 364 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 365 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 366 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 367 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 368 | static int ssl_pkey_info_index = -1; |
| 369 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 370 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 371 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 372 | #endif |
| 373 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 374 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 375 | static unsigned int openssl_engines_initialized; |
| 376 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 377 | struct ssl_engine_list { |
| 378 | struct list list; |
| 379 | ENGINE *e; |
| 380 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 381 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 382 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 383 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 384 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 385 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 386 | static DH *local_dh_1024 = NULL; |
| 387 | static DH *local_dh_2048 = NULL; |
| 388 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 389 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 390 | #endif /* OPENSSL_NO_DH */ |
| 391 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 392 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 393 | /* X509V3 Extensions that will be added on generated certificates */ |
| 394 | #define X509V3_EXT_SIZE 5 |
| 395 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 396 | "basicConstraints", |
| 397 | "nsComment", |
| 398 | "subjectKeyIdentifier", |
| 399 | "authorityKeyIdentifier", |
| 400 | "keyUsage", |
| 401 | }; |
| 402 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 403 | "CA:FALSE", |
| 404 | "\"OpenSSL Generated Certificate\"", |
| 405 | "hash", |
| 406 | "keyid,issuer:always", |
| 407 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 408 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 409 | /* LRU cache to store generated certificate */ |
| 410 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 411 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 412 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 413 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 414 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 415 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 416 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 417 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 418 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 419 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 420 | /* The order here matters for picking a default context, |
| 421 | * keep the most common keytype at the bottom of the list |
| 422 | */ |
| 423 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 424 | "dsa", |
| 425 | "ecdsa", |
| 426 | "rsa" |
| 427 | }; |
| 428 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 429 | #else |
| 430 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 431 | #endif |
| 432 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 433 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 434 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 435 | |
| 436 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 437 | |
| 438 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 439 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 440 | |
| 441 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 442 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 443 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 444 | /* |
| 445 | * This function gives the detail of the SSL error. It is used only |
| 446 | * if the debug mode and the verbose mode are activated. It dump all |
| 447 | * the SSL error until the stack was empty. |
| 448 | */ |
| 449 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 450 | { |
| 451 | unsigned long ret; |
| 452 | |
| 453 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 454 | while(1) { |
| 455 | ret = ERR_get_error(); |
| 456 | if (ret == 0) |
| 457 | return; |
| 458 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 459 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 460 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 465 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 466 | /* |
| 467 | * struct alignment works here such that the key.key is the same as key_data |
| 468 | * Do not change the placement of key_data |
| 469 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 470 | struct certificate_ocsp { |
| 471 | struct ebmb_node key; |
| 472 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 473 | struct buffer response; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 474 | long expire; |
| 475 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 476 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 477 | struct ocsp_cbk_arg { |
| 478 | int is_single; |
| 479 | int single_kt; |
| 480 | union { |
| 481 | struct certificate_ocsp *s_ocsp; |
| 482 | /* |
| 483 | * m_ocsp will have multiple entries dependent on key type |
| 484 | * Entry 0 - DSA |
| 485 | * Entry 1 - ECDSA |
| 486 | * Entry 2 - RSA |
| 487 | */ |
| 488 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 489 | }; |
| 490 | }; |
| 491 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 492 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 493 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 494 | { |
| 495 | int err_code = ERR_ABORT; |
| 496 | ENGINE *engine; |
| 497 | struct ssl_engine_list *el; |
| 498 | |
| 499 | /* grab the structural reference to the engine */ |
| 500 | engine = ENGINE_by_id(engine_id); |
| 501 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 502 | 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] | 503 | goto fail_get; |
| 504 | } |
| 505 | |
| 506 | if (!ENGINE_init(engine)) { |
| 507 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 508 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 509 | goto fail_init; |
| 510 | } |
| 511 | |
| 512 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 513 | 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] | 514 | goto fail_set_method; |
| 515 | } |
| 516 | |
| 517 | el = calloc(1, sizeof(*el)); |
| 518 | el->e = engine; |
| 519 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 520 | nb_engines++; |
| 521 | if (global_ssl.async) |
| 522 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 523 | return 0; |
| 524 | |
| 525 | fail_set_method: |
| 526 | /* release the functional reference from ENGINE_init() */ |
| 527 | ENGINE_finish(engine); |
| 528 | |
| 529 | fail_init: |
| 530 | /* release the structural reference from ENGINE_by_id() */ |
| 531 | ENGINE_free(engine); |
| 532 | |
| 533 | fail_get: |
| 534 | return err_code; |
| 535 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 536 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 537 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 538 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 539 | /* |
| 540 | * openssl async fd handler |
| 541 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 542 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 543 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 544 | struct ssl_sock_ctx *ctx = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 545 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 546 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 547 | * to poll this fd until it is requested |
| 548 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 549 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 550 | fd_cant_recv(fd); |
| 551 | |
| 552 | /* crypto engine is available, let's notify the associated |
| 553 | * connection that it can pursue its processing. |
| 554 | */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 555 | ssl_sock_io_cb(NULL, ctx, 0); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 558 | /* |
| 559 | * openssl async delayed SSL_free handler |
| 560 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 561 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 562 | { |
| 563 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 564 | OSSL_ASYNC_FD all_fd[32]; |
| 565 | size_t num_all_fds = 0; |
| 566 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 567 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 568 | /* We suppose that the async job for a same SSL * |
| 569 | * are serialized. So if we are awake it is |
| 570 | * because the running job has just finished |
| 571 | * and we can remove all async fds safely |
| 572 | */ |
| 573 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 574 | if (num_all_fds > 32) { |
| 575 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 580 | for (i=0 ; i < num_all_fds ; i++) |
| 581 | fd_remove(all_fd[i]); |
| 582 | |
| 583 | /* 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] | 584 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 585 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 586 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 587 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 588 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 589 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 590 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 591 | */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 592 | static inline void ssl_async_process_fds(struct ssl_sock_ctx *ctx) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 593 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 594 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 595 | OSSL_ASYNC_FD del_fd[32]; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 596 | SSL *ssl = ctx->ssl; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 597 | size_t num_add_fds = 0; |
| 598 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 599 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 600 | |
| 601 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 602 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 603 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 604 | 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] | 605 | return; |
| 606 | } |
| 607 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 608 | 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] | 609 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 610 | /* We remove unused fds from the fdtab */ |
| 611 | for (i=0 ; i < num_del_fds ; i++) |
| 612 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 613 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 614 | /* We add new fds to the fdtab */ |
| 615 | for (i=0 ; i < num_add_fds ; i++) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 616 | fd_insert(add_fd[i], ctx, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 617 | } |
| 618 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 619 | num_add_fds = 0; |
| 620 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 621 | if (num_add_fds > 32) { |
| 622 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 623 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 624 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 625 | |
| 626 | /* We activate the polling for all known async fds */ |
| 627 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 628 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 629 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 630 | /* To ensure that the fd cache won't be used |
| 631 | * We'll prefer to catch a real RD event |
| 632 | * because handling an EAGAIN on this fd will |
| 633 | * result in a context switch and also |
| 634 | * some engines uses a fd in blocking mode. |
| 635 | */ |
| 636 | fd_cant_recv(add_fd[i]); |
| 637 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 638 | |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 639 | } |
| 640 | #endif |
| 641 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 642 | /* |
| 643 | * This function returns the number of seconds elapsed |
| 644 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 645 | * date presented un ASN1_GENERALIZEDTIME. |
| 646 | * |
| 647 | * In parsing error case, it returns -1. |
| 648 | */ |
| 649 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 650 | { |
| 651 | long epoch; |
| 652 | char *p, *end; |
| 653 | const unsigned short month_offset[12] = { |
| 654 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 655 | }; |
| 656 | int year, month; |
| 657 | |
| 658 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 659 | |
| 660 | p = (char *)d->data; |
| 661 | end = p + d->length; |
| 662 | |
| 663 | if (end - p < 4) return -1; |
| 664 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 665 | p += 4; |
| 666 | if (end - p < 2) return -1; |
| 667 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 668 | if (month < 1 || month > 12) return -1; |
| 669 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 670 | We consider leap years and the current month (<marsh or not) */ |
| 671 | epoch = ( ((year - 1970) * 365) |
| 672 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 673 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 674 | + month_offset[month-1] |
| 675 | ) * 24 * 60 * 60; |
| 676 | p += 2; |
| 677 | if (end - p < 2) return -1; |
| 678 | /* Add the number of seconds of completed days of current month */ |
| 679 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 680 | p += 2; |
| 681 | if (end - p < 2) return -1; |
| 682 | /* Add the completed hours of the current day */ |
| 683 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 684 | p += 2; |
| 685 | if (end - p < 2) return -1; |
| 686 | /* Add the completed minutes of the current hour */ |
| 687 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 688 | p += 2; |
| 689 | if (p == end) return -1; |
| 690 | /* Test if there is available seconds */ |
| 691 | if (p[0] < '0' || p[0] > '9') |
| 692 | goto nosec; |
| 693 | if (end - p < 2) return -1; |
| 694 | /* Add the seconds of the current minute */ |
| 695 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 696 | p += 2; |
| 697 | if (p == end) return -1; |
| 698 | /* Ignore seconds float part if present */ |
| 699 | if (p[0] == '.') { |
| 700 | do { |
| 701 | if (++p == end) return -1; |
| 702 | } while (p[0] >= '0' && p[0] <= '9'); |
| 703 | } |
| 704 | |
| 705 | nosec: |
| 706 | if (p[0] == 'Z') { |
| 707 | if (end - p != 1) return -1; |
| 708 | return epoch; |
| 709 | } |
| 710 | else if (p[0] == '+') { |
| 711 | if (end - p != 5) return -1; |
| 712 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 713 | 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] | 714 | } |
| 715 | else if (p[0] == '-') { |
| 716 | if (end - p != 5) return -1; |
| 717 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 718 | 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] | 719 | } |
| 720 | |
| 721 | return -1; |
| 722 | } |
| 723 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 724 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 725 | |
| 726 | /* This function starts to check if the OCSP response (in DER format) contained |
| 727 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 728 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 729 | * contained in the OCSP Response and exits on error if no match. |
| 730 | * If it's a valid OCSP Response: |
| 731 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 732 | * pointed by 'ocsp'. |
| 733 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 734 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 735 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 736 | * already present in the container, it will be overwritten. |
| 737 | * |
| 738 | * Note: OCSP response containing more than one OCSP Single response is not |
| 739 | * considered valid. |
| 740 | * |
| 741 | * Returns 0 on success, 1 in error case. |
| 742 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 743 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 744 | struct certificate_ocsp *ocsp, |
| 745 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 746 | { |
| 747 | OCSP_RESPONSE *resp; |
| 748 | OCSP_BASICRESP *bs = NULL; |
| 749 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 750 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 751 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 752 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 753 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 754 | int reason; |
| 755 | int ret = 1; |
| 756 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 757 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 758 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 759 | if (!resp) { |
| 760 | memprintf(err, "Unable to parse OCSP response"); |
| 761 | goto out; |
| 762 | } |
| 763 | |
| 764 | rc = OCSP_response_status(resp); |
| 765 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 766 | memprintf(err, "OCSP response status not successful"); |
| 767 | goto out; |
| 768 | } |
| 769 | |
| 770 | bs = OCSP_response_get1_basic(resp); |
| 771 | if (!bs) { |
| 772 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 773 | goto out; |
| 774 | } |
| 775 | |
| 776 | count_sr = OCSP_resp_count(bs); |
| 777 | if (count_sr > 1) { |
| 778 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 779 | goto out; |
| 780 | } |
| 781 | |
| 782 | sr = OCSP_resp_get0(bs, 0); |
| 783 | if (!sr) { |
| 784 | memprintf(err, "Failed to get OCSP single response"); |
| 785 | goto out; |
| 786 | } |
| 787 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 788 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 789 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 790 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 791 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 792 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 793 | goto out; |
| 794 | } |
| 795 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 796 | if (!nextupd) { |
| 797 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 798 | goto out; |
| 799 | } |
| 800 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 801 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 802 | if (!rc) { |
| 803 | memprintf(err, "OCSP single response: no longer valid."); |
| 804 | goto out; |
| 805 | } |
| 806 | |
| 807 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 808 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 809 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 810 | goto out; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | if (!ocsp) { |
| 815 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 816 | unsigned char *p; |
| 817 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 818 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 819 | if (!rc) { |
| 820 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 821 | goto out; |
| 822 | } |
| 823 | |
| 824 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 825 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 826 | goto out; |
| 827 | } |
| 828 | |
| 829 | p = key; |
| 830 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 831 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 832 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 833 | if (!ocsp) { |
| 834 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 835 | goto out; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | /* According to comments on "chunk_dup", the |
| 840 | previous chunk buffer will be freed */ |
| 841 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 842 | memprintf(err, "OCSP response: Memory allocation error"); |
| 843 | goto out; |
| 844 | } |
| 845 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 846 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 847 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 848 | ret = 0; |
| 849 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 850 | ERR_clear_error(); |
| 851 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 852 | if (bs) |
| 853 | OCSP_BASICRESP_free(bs); |
| 854 | |
| 855 | if (resp) |
| 856 | OCSP_RESPONSE_free(resp); |
| 857 | |
| 858 | return ret; |
| 859 | } |
| 860 | /* |
| 861 | * External function use to update the OCSP response in the OCSP response's |
| 862 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 863 | * to update in DER format. |
| 864 | * |
| 865 | * Returns 0 on success, 1 in error case. |
| 866 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 867 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 868 | { |
| 869 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * This function load the OCSP Resonse in DER format contained in file at |
| 874 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 875 | * |
| 876 | * Returns 0 on success, 1 in error case. |
| 877 | */ |
| 878 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 879 | { |
| 880 | int fd = -1; |
| 881 | int r = 0; |
| 882 | int ret = 1; |
| 883 | |
| 884 | fd = open(ocsp_path, O_RDONLY); |
| 885 | if (fd == -1) { |
| 886 | memprintf(err, "Error opening OCSP response file"); |
| 887 | goto end; |
| 888 | } |
| 889 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 890 | trash.data = 0; |
| 891 | while (trash.data < trash.size) { |
| 892 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 893 | if (r < 0) { |
| 894 | if (errno == EINTR) |
| 895 | continue; |
| 896 | |
| 897 | memprintf(err, "Error reading OCSP response from file"); |
| 898 | goto end; |
| 899 | } |
| 900 | else if (r == 0) { |
| 901 | break; |
| 902 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 903 | trash.data += r; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | close(fd); |
| 907 | fd = -1; |
| 908 | |
| 909 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 910 | end: |
| 911 | if (fd != -1) |
| 912 | close(fd); |
| 913 | |
| 914 | return ret; |
| 915 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 916 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 917 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 918 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 919 | 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) |
| 920 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 921 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 922 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 923 | struct connection *conn; |
| 924 | int head; |
| 925 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 926 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 927 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 928 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 929 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 930 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 931 | |
| 932 | keys = ref->tlskeys; |
| 933 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 934 | |
| 935 | if (enc) { |
| 936 | memcpy(key_name, keys[head].name, 16); |
| 937 | |
| 938 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 939 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 940 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 941 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 942 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 943 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 944 | goto end; |
| 945 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 946 | 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] | 947 | ret = 1; |
| 948 | } |
| 949 | else if (ref->key_size_bits == 256 ) { |
| 950 | |
| 951 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 952 | goto end; |
| 953 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 954 | 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] | 955 | ret = 1; |
| 956 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 957 | } else { |
| 958 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 959 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 960 | goto found; |
| 961 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 962 | ret = 0; |
| 963 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 964 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 965 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 966 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 967 | 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] | 968 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 969 | goto end; |
| 970 | /* 2 for key renewal, 1 if current key is still valid */ |
| 971 | ret = i ? 2 : 1; |
| 972 | } |
| 973 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 974 | 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] | 975 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 976 | goto end; |
| 977 | /* 2 for key renewal, 1 if current key is still valid */ |
| 978 | ret = i ? 2 : 1; |
| 979 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 980 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 981 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 982 | end: |
| 983 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 984 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 985 | } |
| 986 | |
| 987 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 988 | { |
| 989 | struct tls_keys_ref *ref; |
| 990 | |
| 991 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 992 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 993 | return ref; |
| 994 | return NULL; |
| 995 | } |
| 996 | |
| 997 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 998 | { |
| 999 | struct tls_keys_ref *ref; |
| 1000 | |
| 1001 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1002 | if (ref->unique_id == unique_id) |
| 1003 | return ref; |
| 1004 | return NULL; |
| 1005 | } |
| 1006 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1007 | /* Update the key into ref: if keysize doesnt |
| 1008 | * match existing ones, this function returns -1 |
| 1009 | * else it returns 0 on success. |
| 1010 | */ |
| 1011 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1012 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1013 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1014 | if (ref->key_size_bits == 128) { |
| 1015 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1016 | return -1; |
| 1017 | } |
| 1018 | else if (ref->key_size_bits == 256) { |
| 1019 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1020 | return -1; |
| 1021 | } |
| 1022 | else |
| 1023 | return -1; |
| 1024 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1025 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1026 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1027 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1028 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1029 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1030 | |
| 1031 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1032 | } |
| 1033 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1034 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1035 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1036 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1037 | |
| 1038 | if(!ref) { |
| 1039 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1040 | return 1; |
| 1041 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1042 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1043 | memprintf(err, "Invalid key size"); |
| 1044 | return 1; |
| 1045 | } |
| 1046 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1047 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1048 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1049 | |
| 1050 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1051 | * automatic ids. It's called just after the basic checks. It returns |
| 1052 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1053 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1054 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1055 | { |
| 1056 | int i = 0; |
| 1057 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1058 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1059 | |
| 1060 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1061 | if (ref->unique_id == -1) { |
| 1062 | /* Look for the first free id. */ |
| 1063 | while (1) { |
| 1064 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1065 | if (ref2->unique_id == i) { |
| 1066 | i++; |
| 1067 | break; |
| 1068 | } |
| 1069 | } |
| 1070 | if (&ref2->list == &tlskeys_reference) |
| 1071 | break; |
| 1072 | } |
| 1073 | |
| 1074 | /* Uses the unique id and increment it for the next entry. */ |
| 1075 | ref->unique_id = i; |
| 1076 | i++; |
| 1077 | } |
| 1078 | } |
| 1079 | |
| 1080 | /* This sort the reference list by id. */ |
| 1081 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1082 | LIST_DEL(&ref->list); |
| 1083 | list_for_each_entry(ref3, &tkr, list) { |
| 1084 | if (ref->unique_id < ref3->unique_id) { |
| 1085 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1086 | break; |
| 1087 | } |
| 1088 | } |
| 1089 | if (&ref3->list == &tkr) |
| 1090 | LIST_ADDQ(&tkr, &ref->list); |
| 1091 | } |
| 1092 | |
| 1093 | /* swap root */ |
| 1094 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1095 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1096 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1097 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1098 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1099 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1100 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1101 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1102 | { |
| 1103 | switch (evp_keytype) { |
| 1104 | case EVP_PKEY_RSA: |
| 1105 | return 2; |
| 1106 | case EVP_PKEY_DSA: |
| 1107 | return 0; |
| 1108 | case EVP_PKEY_EC: |
| 1109 | return 1; |
| 1110 | } |
| 1111 | |
| 1112 | return -1; |
| 1113 | } |
| 1114 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1115 | /* |
| 1116 | * Callback used to set OCSP status extension content in server hello. |
| 1117 | */ |
| 1118 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1119 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1120 | struct certificate_ocsp *ocsp; |
| 1121 | struct ocsp_cbk_arg *ocsp_arg; |
| 1122 | char *ssl_buf; |
| 1123 | EVP_PKEY *ssl_pkey; |
| 1124 | int key_type; |
| 1125 | int index; |
| 1126 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1127 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1128 | |
| 1129 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1130 | if (!ssl_pkey) |
| 1131 | return SSL_TLSEXT_ERR_NOACK; |
| 1132 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1133 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1134 | |
| 1135 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1136 | ocsp = ocsp_arg->s_ocsp; |
| 1137 | else { |
| 1138 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1139 | * the certificate type |
| 1140 | */ |
| 1141 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1142 | |
| 1143 | if (index < 0) |
| 1144 | return SSL_TLSEXT_ERR_NOACK; |
| 1145 | |
| 1146 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1147 | |
| 1148 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1149 | |
| 1150 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1151 | !ocsp->response.area || |
| 1152 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1153 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1154 | return SSL_TLSEXT_ERR_NOACK; |
| 1155 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1156 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1157 | if (!ssl_buf) |
| 1158 | return SSL_TLSEXT_ERR_NOACK; |
| 1159 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1160 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1161 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1162 | |
| 1163 | return SSL_TLSEXT_ERR_OK; |
| 1164 | } |
| 1165 | |
| 1166 | /* |
| 1167 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 1168 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 1169 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 1170 | * It should be present in the certificate's extra chain builded from file |
| 1171 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 1172 | * named 'cert_path' suffixed using '.issuer'. |
| 1173 | * |
| 1174 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 1175 | * response. If file is empty or content is not a valid OCSP response, |
| 1176 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 1177 | * is displayed). |
| 1178 | * |
| 1179 | * 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] | 1180 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1181 | */ |
| 1182 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 1183 | { |
| 1184 | |
| 1185 | BIO *in = NULL; |
| 1186 | X509 *x, *xi = NULL, *issuer = NULL; |
| 1187 | STACK_OF(X509) *chain = NULL; |
| 1188 | OCSP_CERTID *cid = NULL; |
| 1189 | SSL *ssl; |
| 1190 | char ocsp_path[MAXPATHLEN+1]; |
| 1191 | int i, ret = -1; |
| 1192 | struct stat st; |
| 1193 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1194 | char *warn = NULL; |
| 1195 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1196 | pem_password_cb *passwd_cb; |
| 1197 | void *passwd_cb_userdata; |
| 1198 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1199 | |
| 1200 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1201 | |
| 1202 | if (stat(ocsp_path, &st)) |
| 1203 | return 1; |
| 1204 | |
| 1205 | ssl = SSL_new(ctx); |
| 1206 | if (!ssl) |
| 1207 | goto out; |
| 1208 | |
| 1209 | x = SSL_get_certificate(ssl); |
| 1210 | if (!x) |
| 1211 | goto out; |
| 1212 | |
| 1213 | /* Try to lookup for issuer in certificate extra chain */ |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1214 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1215 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1216 | issuer = sk_X509_value(chain, i); |
| 1217 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 1218 | break; |
| 1219 | else |
| 1220 | issuer = NULL; |
| 1221 | } |
| 1222 | |
| 1223 | /* If not found try to load issuer from a suffixed file */ |
| 1224 | if (!issuer) { |
| 1225 | char issuer_path[MAXPATHLEN+1]; |
| 1226 | |
| 1227 | in = BIO_new(BIO_s_file()); |
| 1228 | if (!in) |
| 1229 | goto out; |
| 1230 | |
| 1231 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1232 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1233 | goto out; |
| 1234 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1235 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1236 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1237 | |
| 1238 | xi = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1239 | if (!xi) |
| 1240 | goto out; |
| 1241 | |
| 1242 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1243 | goto out; |
| 1244 | |
| 1245 | issuer = xi; |
| 1246 | } |
| 1247 | |
| 1248 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1249 | if (!cid) |
| 1250 | goto out; |
| 1251 | |
| 1252 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1253 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1254 | goto out; |
| 1255 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1256 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1257 | if (!ocsp) |
| 1258 | goto out; |
| 1259 | |
| 1260 | p = ocsp->key_data; |
| 1261 | i2d_OCSP_CERTID(cid, &p); |
| 1262 | |
| 1263 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1264 | if (iocsp == ocsp) |
| 1265 | ocsp = NULL; |
| 1266 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1267 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1268 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1269 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1270 | #endif |
| 1271 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1272 | |
| 1273 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1274 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1275 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1276 | |
| 1277 | cb_arg->is_single = 1; |
| 1278 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1279 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1280 | pkey = X509_get_pubkey(x); |
| 1281 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1282 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1283 | |
| 1284 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1285 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1286 | } else { |
| 1287 | /* |
| 1288 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1289 | * Update that cb_arg with the new cert's staple |
| 1290 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1291 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1292 | struct certificate_ocsp *tmp_ocsp; |
| 1293 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1294 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1295 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1296 | |
| 1297 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1298 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1299 | #else |
| 1300 | cb_arg = ctx->tlsext_status_arg; |
| 1301 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1302 | |
| 1303 | /* |
| 1304 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1305 | * the order of operations below matter, take care when changing it |
| 1306 | */ |
| 1307 | tmp_ocsp = cb_arg->s_ocsp; |
| 1308 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1309 | cb_arg->s_ocsp = NULL; |
| 1310 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1311 | cb_arg->is_single = 0; |
| 1312 | cb_arg->single_kt = 0; |
| 1313 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1314 | pkey = X509_get_pubkey(x); |
| 1315 | key_type = EVP_PKEY_base_id(pkey); |
| 1316 | EVP_PKEY_free(pkey); |
| 1317 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1318 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1319 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1320 | cb_arg->m_ocsp[index] = iocsp; |
| 1321 | |
| 1322 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1323 | |
| 1324 | ret = 0; |
| 1325 | |
| 1326 | warn = NULL; |
| 1327 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1328 | memprintf(&warn, "Loading '%s': %s. Content will be ignored", ocsp_path, warn ? warn : "failure"); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1329 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1330 | } |
| 1331 | |
| 1332 | out: |
| 1333 | if (ssl) |
| 1334 | SSL_free(ssl); |
| 1335 | |
| 1336 | if (in) |
| 1337 | BIO_free(in); |
| 1338 | |
| 1339 | if (xi) |
| 1340 | X509_free(xi); |
| 1341 | |
| 1342 | if (cid) |
| 1343 | OCSP_CERTID_free(cid); |
| 1344 | |
| 1345 | if (ocsp) |
| 1346 | free(ocsp); |
| 1347 | |
| 1348 | if (warn) |
| 1349 | free(warn); |
| 1350 | |
| 1351 | |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
| 1355 | #endif |
| 1356 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1357 | #ifdef OPENSSL_IS_BORINGSSL |
| 1358 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1359 | { |
| 1360 | char ocsp_path[MAXPATHLEN+1]; |
| 1361 | struct stat st; |
| 1362 | int fd = -1, r = 0; |
| 1363 | |
| 1364 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1365 | if (stat(ocsp_path, &st)) |
| 1366 | return 0; |
| 1367 | |
| 1368 | fd = open(ocsp_path, O_RDONLY); |
| 1369 | if (fd == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1370 | ha_warning("Error opening OCSP response file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1371 | return -1; |
| 1372 | } |
| 1373 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1374 | trash.data = 0; |
| 1375 | while (trash.data < trash.size) { |
| 1376 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1377 | if (r < 0) { |
| 1378 | if (errno == EINTR) |
| 1379 | continue; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1380 | ha_warning("Error reading OCSP response from file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1381 | close(fd); |
| 1382 | return -1; |
| 1383 | } |
| 1384 | else if (r == 0) { |
| 1385 | break; |
| 1386 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1387 | trash.data += r; |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1388 | } |
| 1389 | close(fd); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1390 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area, |
| 1391 | trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1392 | } |
| 1393 | #endif |
| 1394 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1395 | #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] | 1396 | |
| 1397 | #define CT_EXTENSION_TYPE 18 |
| 1398 | |
| 1399 | static int sctl_ex_index = -1; |
| 1400 | |
| 1401 | /* |
| 1402 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1403 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1404 | * is performed. |
| 1405 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1406 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1407 | { |
| 1408 | int ret = 1; |
| 1409 | int len, pos, sct_len; |
| 1410 | unsigned char *data; |
| 1411 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1412 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1413 | goto out; |
| 1414 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1415 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1416 | len = (data[0] << 8) | data[1]; |
| 1417 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1418 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1419 | goto out; |
| 1420 | |
| 1421 | data = data + 2; |
| 1422 | pos = 0; |
| 1423 | while (pos < len) { |
| 1424 | if (len - pos < 2) |
| 1425 | goto out; |
| 1426 | |
| 1427 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1428 | if (pos + sct_len + 2 > len) |
| 1429 | goto out; |
| 1430 | |
| 1431 | pos += sct_len + 2; |
| 1432 | } |
| 1433 | |
| 1434 | ret = 0; |
| 1435 | |
| 1436 | out: |
| 1437 | return ret; |
| 1438 | } |
| 1439 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1440 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, |
| 1441 | struct buffer **sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1442 | { |
| 1443 | int fd = -1; |
| 1444 | int r = 0; |
| 1445 | int ret = 1; |
| 1446 | |
| 1447 | *sctl = NULL; |
| 1448 | |
| 1449 | fd = open(sctl_path, O_RDONLY); |
| 1450 | if (fd == -1) |
| 1451 | goto end; |
| 1452 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1453 | trash.data = 0; |
| 1454 | while (trash.data < trash.size) { |
| 1455 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1456 | if (r < 0) { |
| 1457 | if (errno == EINTR) |
| 1458 | continue; |
| 1459 | |
| 1460 | goto end; |
| 1461 | } |
| 1462 | else if (r == 0) { |
| 1463 | break; |
| 1464 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1465 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1466 | } |
| 1467 | |
| 1468 | ret = ssl_sock_parse_sctl(&trash); |
| 1469 | if (ret) |
| 1470 | goto end; |
| 1471 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1472 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1473 | if (!chunk_dup(*sctl, &trash)) { |
| 1474 | free(*sctl); |
| 1475 | *sctl = NULL; |
| 1476 | goto end; |
| 1477 | } |
| 1478 | |
| 1479 | end: |
| 1480 | if (fd != -1) |
| 1481 | close(fd); |
| 1482 | |
| 1483 | return ret; |
| 1484 | } |
| 1485 | |
| 1486 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1487 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1488 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1489 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1490 | *out = (unsigned char *) sctl->area; |
| 1491 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1492 | |
| 1493 | return 1; |
| 1494 | } |
| 1495 | |
| 1496 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1497 | { |
| 1498 | return 1; |
| 1499 | } |
| 1500 | |
| 1501 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1502 | { |
| 1503 | char sctl_path[MAXPATHLEN+1]; |
| 1504 | int ret = -1; |
| 1505 | struct stat st; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1506 | struct buffer *sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1507 | |
| 1508 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1509 | |
| 1510 | if (stat(sctl_path, &st)) |
| 1511 | return 1; |
| 1512 | |
| 1513 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1514 | goto out; |
| 1515 | |
| 1516 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1517 | free(sctl); |
| 1518 | goto out; |
| 1519 | } |
| 1520 | |
| 1521 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1522 | |
| 1523 | ret = 0; |
| 1524 | |
| 1525 | out: |
| 1526 | return ret; |
| 1527 | } |
| 1528 | |
| 1529 | #endif |
| 1530 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1531 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1532 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1533 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1534 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1535 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1536 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1537 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1538 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1539 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1540 | * change this to #if and do not assign a default value to this macro! |
| 1541 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1542 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1543 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 1544 | 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] | 1545 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1546 | conn->err_code = CO_ER_SSL_RENEG; |
| 1547 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1548 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1549 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1550 | |
| 1551 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1552 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1553 | /* Long certificate chains optimz |
| 1554 | If write and read bios are differents, we |
| 1555 | consider that the buffering was activated, |
| 1556 | so we rise the output buffer size from 4k |
| 1557 | to 16k */ |
| 1558 | write_bio = SSL_get_wbio(ssl); |
| 1559 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1560 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1561 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1562 | } |
| 1563 | } |
| 1564 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1565 | } |
| 1566 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1567 | /* Callback is called for each certificate of the chain during a verify |
| 1568 | ok is set to 1 if preverify detect no error on current certificate. |
| 1569 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1570 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1571 | { |
| 1572 | SSL *ssl; |
| 1573 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1574 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1575 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1576 | |
| 1577 | 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] | 1578 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1579 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1580 | ctx = conn->xprt_ctx; |
| 1581 | |
| 1582 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1583 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1584 | if (ok) /* no errors */ |
| 1585 | return ok; |
| 1586 | |
| 1587 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1588 | err = X509_STORE_CTX_get_error(x_store); |
| 1589 | |
| 1590 | /* check if CA error needs to be ignored */ |
| 1591 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1592 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1593 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1594 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1595 | } |
| 1596 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1597 | 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] | 1598 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1599 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1600 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1601 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1602 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1603 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1604 | return 0; |
| 1605 | } |
| 1606 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1607 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1608 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1609 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1610 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1611 | 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] | 1612 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1613 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1614 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1615 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1616 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1617 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1618 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1619 | } |
| 1620 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1621 | static inline |
| 1622 | 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] | 1623 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1624 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1625 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1626 | unsigned char *msg; |
| 1627 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1628 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1629 | |
| 1630 | /* This function is called for "from client" and "to server" |
| 1631 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1632 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1633 | */ |
| 1634 | |
| 1635 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1636 | * otherwise it is set to 1. |
| 1637 | */ |
| 1638 | if (write_p != 0) |
| 1639 | return; |
| 1640 | |
| 1641 | /* content_type contains the type of message received or sent |
| 1642 | * according with the SSL/TLS protocol spec. This message is |
| 1643 | * encoded with one byte. The value 256 (two bytes) is used |
| 1644 | * for designing the SSL/TLS record layer. According with the |
| 1645 | * rfc6101, the expected message (other than 256) are: |
| 1646 | * - change_cipher_spec(20) |
| 1647 | * - alert(21) |
| 1648 | * - handshake(22) |
| 1649 | * - application_data(23) |
| 1650 | * - (255) |
| 1651 | * We are interessed by the handshake and specially the client |
| 1652 | * hello. |
| 1653 | */ |
| 1654 | if (content_type != 22) |
| 1655 | return; |
| 1656 | |
| 1657 | /* The message length is at least 4 bytes, containing the |
| 1658 | * message type and the message length. |
| 1659 | */ |
| 1660 | if (len < 4) |
| 1661 | return; |
| 1662 | |
| 1663 | /* First byte of the handshake message id the type of |
| 1664 | * message. The konwn types are: |
| 1665 | * - hello_request(0) |
| 1666 | * - client_hello(1) |
| 1667 | * - server_hello(2) |
| 1668 | * - certificate(11) |
| 1669 | * - server_key_exchange (12) |
| 1670 | * - certificate_request(13) |
| 1671 | * - server_hello_done(14) |
| 1672 | * We are interested by the client hello. |
| 1673 | */ |
| 1674 | msg = (unsigned char *)buf; |
| 1675 | if (msg[0] != 1) |
| 1676 | return; |
| 1677 | |
| 1678 | /* Next three bytes are the length of the message. The total length |
| 1679 | * must be this decoded length + 4. If the length given as argument |
| 1680 | * is not the same, we abort the protocol dissector. |
| 1681 | */ |
| 1682 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1683 | if (len < rec_len + 4) |
| 1684 | return; |
| 1685 | msg += 4; |
| 1686 | end = msg + rec_len; |
| 1687 | if (end < msg) |
| 1688 | return; |
| 1689 | |
| 1690 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1691 | * 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] | 1692 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1693 | */ |
| 1694 | msg += 1 + 1 + 4 + 28; |
| 1695 | if (msg > end) |
| 1696 | return; |
| 1697 | |
| 1698 | /* Next, is session id: |
| 1699 | * if present, we have to jump by length + 1 for the size information |
| 1700 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1701 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1702 | if (msg[0] > 0) |
| 1703 | msg += msg[0]; |
| 1704 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1705 | if (msg > end) |
| 1706 | return; |
| 1707 | |
| 1708 | /* Next two bytes are the ciphersuite length. */ |
| 1709 | if (msg + 2 > end) |
| 1710 | return; |
| 1711 | rec_len = (msg[0] << 8) + msg[1]; |
| 1712 | msg += 2; |
| 1713 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1714 | return; |
| 1715 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1716 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1717 | if (!capture) |
| 1718 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1719 | /* Compute the xxh64 of the ciphersuite. */ |
| 1720 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1721 | |
| 1722 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1723 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1724 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1725 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1726 | |
| 1727 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1728 | } |
| 1729 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1730 | /* Callback is called for ssl protocol analyse */ |
| 1731 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1732 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1733 | #ifdef TLS1_RT_HEARTBEAT |
| 1734 | /* test heartbeat received (write_p is set to 0 |
| 1735 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1736 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1737 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
William Lallemand | 7e1770b | 2019-05-13 14:31:34 +0200 | [diff] [blame] | 1738 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1739 | const unsigned char *p = buf; |
| 1740 | unsigned int payload; |
| 1741 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1742 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1743 | |
| 1744 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1745 | if (*p != TLS1_HB_REQUEST) |
| 1746 | return; |
| 1747 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1748 | 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] | 1749 | goto kill_it; |
| 1750 | |
| 1751 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1752 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1753 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1754 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1755 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1756 | * advertised payload is larger than the advertised packet |
| 1757 | * length, so we have garbage in the buffer between the |
| 1758 | * payload and the end of the buffer (p+len). We can't know |
| 1759 | * if the SSL stack is patched, and we don't know if we can |
| 1760 | * safely wipe out the area between p+3+len and payload. |
| 1761 | * So instead, we prevent the response from being sent by |
| 1762 | * setting the max_send_fragment to 0 and we report an SSL |
| 1763 | * error, which will kill this connection. It will be reported |
| 1764 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1765 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1766 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1767 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1768 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1769 | return; |
| 1770 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1771 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1772 | if (global_ssl.capture_cipherlist > 0) |
| 1773 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1774 | } |
| 1775 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1776 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1777 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1778 | const unsigned char *in, unsigned int inlen, |
| 1779 | void *arg) |
| 1780 | { |
| 1781 | struct server *srv = arg; |
| 1782 | |
| 1783 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1784 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1785 | return SSL_TLSEXT_ERR_OK; |
| 1786 | return SSL_TLSEXT_ERR_NOACK; |
| 1787 | } |
| 1788 | #endif |
| 1789 | |
| 1790 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1791 | /* This callback is used so that the server advertises the list of |
| 1792 | * negociable protocols for NPN. |
| 1793 | */ |
| 1794 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1795 | unsigned int *len, void *arg) |
| 1796 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1797 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1798 | |
| 1799 | *data = (const unsigned char *)conf->npn_str; |
| 1800 | *len = conf->npn_len; |
| 1801 | return SSL_TLSEXT_ERR_OK; |
| 1802 | } |
| 1803 | #endif |
| 1804 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1805 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1806 | /* This callback is used so that the server advertises the list of |
| 1807 | * negociable protocols for ALPN. |
| 1808 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1809 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1810 | unsigned char *outlen, |
| 1811 | const unsigned char *server, |
| 1812 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1813 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1814 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1815 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1816 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1817 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1818 | return SSL_TLSEXT_ERR_NOACK; |
| 1819 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1820 | return SSL_TLSEXT_ERR_OK; |
| 1821 | } |
| 1822 | #endif |
| 1823 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1824 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1825 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1826 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1827 | /* Create a X509 certificate with the specified servername and serial. This |
| 1828 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1829 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1830 | 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] | 1831 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1832 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1833 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1834 | SSL_CTX *ssl_ctx = NULL; |
| 1835 | X509 *newcrt = NULL; |
| 1836 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1837 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1838 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1839 | X509_NAME *name; |
| 1840 | const EVP_MD *digest; |
| 1841 | X509V3_CTX ctx; |
| 1842 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1843 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1844 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1845 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1846 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1847 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1848 | #else |
| 1849 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1850 | if (tmp_ssl) |
| 1851 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1852 | #endif |
| 1853 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1854 | goto mkcert_error; |
| 1855 | |
| 1856 | /* Create the certificate */ |
| 1857 | if (!(newcrt = X509_new())) |
| 1858 | goto mkcert_error; |
| 1859 | |
| 1860 | /* Set version number for the certificate (X509v3) and the serial |
| 1861 | * number */ |
| 1862 | if (X509_set_version(newcrt, 2L) != 1) |
| 1863 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 1864 | 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] | 1865 | |
| 1866 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1867 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1868 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1869 | goto mkcert_error; |
| 1870 | |
| 1871 | /* set public key in the certificate */ |
| 1872 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1873 | goto mkcert_error; |
| 1874 | |
| 1875 | /* Set issuer name from the CA */ |
| 1876 | if (!(name = X509_get_subject_name(cacert))) |
| 1877 | goto mkcert_error; |
| 1878 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1879 | goto mkcert_error; |
| 1880 | |
| 1881 | /* Set the subject name using the same, but the CN */ |
| 1882 | name = X509_NAME_dup(name); |
| 1883 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1884 | (const unsigned char *)servername, |
| 1885 | -1, -1, 0) != 1) { |
| 1886 | X509_NAME_free(name); |
| 1887 | goto mkcert_error; |
| 1888 | } |
| 1889 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1890 | X509_NAME_free(name); |
| 1891 | goto mkcert_error; |
| 1892 | } |
| 1893 | X509_NAME_free(name); |
| 1894 | |
| 1895 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1896 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1897 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1898 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1899 | X509_EXTENSION *ext; |
| 1900 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1901 | 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] | 1902 | goto mkcert_error; |
| 1903 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1904 | X509_EXTENSION_free(ext); |
| 1905 | goto mkcert_error; |
| 1906 | } |
| 1907 | X509_EXTENSION_free(ext); |
| 1908 | } |
| 1909 | |
| 1910 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1911 | |
| 1912 | key_type = EVP_PKEY_base_id(capkey); |
| 1913 | |
| 1914 | if (key_type == EVP_PKEY_DSA) |
| 1915 | digest = EVP_sha1(); |
| 1916 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1917 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1918 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1919 | digest = EVP_sha256(); |
| 1920 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 1921 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1922 | int nid; |
| 1923 | |
| 1924 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1925 | goto mkcert_error; |
| 1926 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1927 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1928 | #else |
| 1929 | goto mkcert_error; |
| 1930 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1931 | } |
| 1932 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1933 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1934 | goto mkcert_error; |
| 1935 | |
| 1936 | /* Create and set the new SSL_CTX */ |
| 1937 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1938 | goto mkcert_error; |
| 1939 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1940 | goto mkcert_error; |
| 1941 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1942 | goto mkcert_error; |
| 1943 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1944 | goto mkcert_error; |
| 1945 | |
| 1946 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1947 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1948 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1949 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1950 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1951 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1952 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1953 | 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] | 1954 | EC_KEY *ecc; |
| 1955 | int nid; |
| 1956 | |
| 1957 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1958 | goto end; |
| 1959 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1960 | goto end; |
| 1961 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1962 | EC_KEY_free(ecc); |
| 1963 | } |
| 1964 | #endif |
| 1965 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1966 | return ssl_ctx; |
| 1967 | |
| 1968 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1969 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1970 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1971 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1972 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1973 | return NULL; |
| 1974 | } |
| 1975 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1976 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1977 | 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] | 1978 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1979 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1980 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1981 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1982 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1983 | } |
| 1984 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1985 | /* 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] | 1986 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1987 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 1988 | 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] | 1989 | { |
| 1990 | struct lru64 *lru = NULL; |
| 1991 | |
| 1992 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1993 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1994 | 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] | 1995 | if (lru && lru->domain) { |
| 1996 | if (ssl) |
| 1997 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 1998 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1999 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2000 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2001 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2002 | } |
| 2003 | return NULL; |
| 2004 | } |
| 2005 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2006 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2007 | * function is not thread-safe, it should only be used to check if a certificate |
| 2008 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2009 | * thread). It is kept for backward compatibility. */ |
| 2010 | SSL_CTX * |
| 2011 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2012 | { |
| 2013 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2014 | } |
| 2015 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2016 | /* Set a certificate int the LRU cache used to store generated |
| 2017 | * certificate. Return 0 on success, otherwise -1 */ |
| 2018 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2019 | 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] | 2020 | { |
| 2021 | struct lru64 *lru = NULL; |
| 2022 | |
| 2023 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2024 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2025 | 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] | 2026 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2027 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2028 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2029 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2030 | if (lru->domain && lru->data) |
| 2031 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2032 | 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] | 2033 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2034 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2035 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2036 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2037 | } |
| 2038 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2039 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2040 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2041 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2042 | { |
| 2043 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2044 | } |
| 2045 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2046 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2047 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2048 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2049 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2050 | 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] | 2051 | { |
| 2052 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2053 | SSL_CTX *ssl_ctx = NULL; |
| 2054 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2055 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2056 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2057 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2058 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2059 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2060 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2061 | if (lru && lru->domain) |
| 2062 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2063 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2064 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2065 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2066 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2067 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2068 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2069 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2070 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2071 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2072 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2073 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2074 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2075 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2076 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2077 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2078 | return 0; |
| 2079 | } |
| 2080 | static int |
| 2081 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2082 | { |
| 2083 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2084 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2085 | |
Willy Tarreau | f5bdb64 | 2019-07-17 11:29:32 +0200 | [diff] [blame] | 2086 | if (conn_get_dst(conn)) { |
Willy Tarreau | 085a151 | 2019-07-17 14:47:35 +0200 | [diff] [blame] | 2087 | 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] | 2088 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2089 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2090 | } |
| 2091 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2092 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2093 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2094 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2095 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2096 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2097 | |
| 2098 | 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] | 2099 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2100 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2101 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2102 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2103 | #endif |
| 2104 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2105 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2106 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2107 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2108 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2109 | 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] | 2110 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2111 | 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] | 2112 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2113 | #endif |
| 2114 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2115 | 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] | 2116 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2117 | 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] | 2118 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2119 | #endif |
| 2120 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2121 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2122 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2123 | /* Unusable in this context. */ |
| 2124 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2125 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2126 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2127 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2128 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2129 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2130 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2131 | |
| 2132 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2133 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2134 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2135 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2136 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2137 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2138 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2139 | } |
| 2140 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2141 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2142 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2143 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2144 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2145 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2146 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2147 | } |
| 2148 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2149 | 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] | 2150 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2151 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2152 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2153 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2154 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2155 | } |
| 2156 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2157 | 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] | 2158 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2159 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2160 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2161 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2162 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2163 | } |
| 2164 | 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] | 2165 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2166 | 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] | 2167 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2168 | #endif |
| 2169 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2170 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2171 | #if SSL_OP_NO_TLSv1_3 |
| 2172 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2173 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2174 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2175 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2176 | #endif |
| 2177 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2178 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2179 | |
| 2180 | static struct { |
| 2181 | int option; |
| 2182 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2183 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2184 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2185 | const char *name; |
| 2186 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2187 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2188 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2189 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2190 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2191 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2192 | {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] | 2193 | }; |
| 2194 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2195 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2196 | { |
| 2197 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2198 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2199 | SSL_set_SSL_CTX(ssl, ctx); |
| 2200 | } |
| 2201 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2202 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2203 | |
| 2204 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2205 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2206 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2207 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2208 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2209 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2210 | return SSL_TLSEXT_ERR_OK; |
| 2211 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2212 | } |
| 2213 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2214 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2215 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2216 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2217 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2218 | #else |
| 2219 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2220 | { |
| 2221 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2222 | struct connection *conn; |
| 2223 | struct bind_conf *s; |
| 2224 | const uint8_t *extension_data; |
| 2225 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2226 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2227 | |
| 2228 | char *wildp = NULL; |
| 2229 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2230 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2231 | 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] | 2232 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2233 | int i; |
| 2234 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2235 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2236 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2237 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2238 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2239 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2240 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2241 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2242 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2243 | #else |
| 2244 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2245 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2246 | /* |
| 2247 | * The server_name extension was given too much extensibility when it |
| 2248 | * was written, so parsing the normal case is a bit complex. |
| 2249 | */ |
| 2250 | size_t len; |
| 2251 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2252 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2253 | /* Extract the length of the supplied list of names. */ |
| 2254 | len = (*extension_data++) << 8; |
| 2255 | len |= *extension_data++; |
| 2256 | if (len + 2 != extension_len) |
| 2257 | goto abort; |
| 2258 | /* |
| 2259 | * The list in practice only has a single element, so we only consider |
| 2260 | * the first one. |
| 2261 | */ |
| 2262 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2263 | goto abort; |
| 2264 | extension_len = len - 1; |
| 2265 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2266 | if (extension_len <= 2) |
| 2267 | goto abort; |
| 2268 | len = (*extension_data++) << 8; |
| 2269 | len |= *extension_data++; |
| 2270 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2271 | || memchr(extension_data, 0, len) != NULL) |
| 2272 | goto abort; |
| 2273 | servername = extension_data; |
| 2274 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2275 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2276 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2277 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2278 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2279 | } |
| 2280 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2281 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2282 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2283 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2284 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2285 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2286 | goto abort; |
| 2287 | } |
| 2288 | |
| 2289 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2290 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2291 | 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] | 2292 | #else |
| 2293 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2294 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2295 | uint8_t sign; |
| 2296 | size_t len; |
| 2297 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2298 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2299 | len = (*extension_data++) << 8; |
| 2300 | len |= *extension_data++; |
| 2301 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2302 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2303 | if (len % 2 != 0) |
| 2304 | goto abort; |
| 2305 | for (; len > 0; len -= 2) { |
| 2306 | extension_data++; /* hash */ |
| 2307 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2308 | switch (sign) { |
| 2309 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2310 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2311 | break; |
| 2312 | case TLSEXT_signature_ecdsa: |
| 2313 | has_ecdsa_sig = 1; |
| 2314 | break; |
| 2315 | default: |
| 2316 | continue; |
| 2317 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2318 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2319 | break; |
| 2320 | } |
| 2321 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2322 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2323 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2324 | } |
| 2325 | 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] | 2326 | const SSL_CIPHER *cipher; |
| 2327 | size_t len; |
| 2328 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2329 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2330 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2331 | len = ctx->cipher_suites_len; |
| 2332 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2333 | #else |
| 2334 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2335 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2336 | if (len % 2 != 0) |
| 2337 | goto abort; |
| 2338 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2339 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2340 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2341 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2342 | #else |
| 2343 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2344 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2345 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2346 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2347 | break; |
| 2348 | } |
| 2349 | } |
| 2350 | } |
| 2351 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2352 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2353 | trash.area[i] = tolower(servername[i]); |
| 2354 | if (!wildp && (trash.area[i] == '.')) |
| 2355 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2356 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2357 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2358 | |
| 2359 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2360 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2361 | |
| 2362 | /* lookup a not neg filter */ |
| 2363 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2364 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2365 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2366 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2367 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2368 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2369 | break; |
| 2370 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2371 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2372 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2373 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2374 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2375 | if (!node_anonymous) |
| 2376 | node_anonymous = n; |
| 2377 | break; |
| 2378 | } |
| 2379 | } |
| 2380 | } |
| 2381 | if (wildp) { |
| 2382 | /* lookup in wildcards names */ |
| 2383 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2384 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2385 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2386 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2387 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2388 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2389 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2390 | break; |
| 2391 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2392 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2393 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2394 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2395 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2396 | if (!node_anonymous) |
| 2397 | node_anonymous = n; |
| 2398 | break; |
| 2399 | } |
| 2400 | } |
| 2401 | } |
| 2402 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2403 | /* select by key_signature priority order */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2404 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2405 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2406 | : (node_anonymous ? node_anonymous |
| 2407 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2408 | : node_rsa /* no rsa signature case (far far away) */ |
| 2409 | ))); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2410 | if (node) { |
| 2411 | /* switch ctx */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 2412 | 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] | 2413 | 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] | 2414 | if (conf) { |
| 2415 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2416 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2417 | if (conf->early_data) |
| 2418 | allow_early = 1; |
| 2419 | } |
| 2420 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2421 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2422 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2423 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2424 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2425 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2426 | } |
| 2427 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2428 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2429 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2430 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2431 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2432 | allow_early: |
| 2433 | #ifdef OPENSSL_IS_BORINGSSL |
| 2434 | if (allow_early) |
| 2435 | SSL_set_early_data_enabled(ssl, 1); |
| 2436 | #else |
| 2437 | if (!allow_early) |
| 2438 | SSL_set_max_early_data(ssl, 0); |
| 2439 | #endif |
| 2440 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2441 | abort: |
| 2442 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2443 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2444 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2445 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2446 | #else |
| 2447 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2448 | return 0; |
| 2449 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2453 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2454 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2455 | * warning when no match is found, which implies the default (first) cert |
| 2456 | * will keep being used. |
| 2457 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2458 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2459 | { |
| 2460 | const char *servername; |
| 2461 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2462 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2463 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2464 | int i; |
| 2465 | (void)al; /* shut gcc stupid warning */ |
| 2466 | |
| 2467 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2468 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2469 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2470 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2471 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2472 | #endif |
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_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2477 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2478 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2479 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2480 | if (!servername[i]) |
| 2481 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2482 | trash.area[i] = tolower(servername[i]); |
| 2483 | if (!wildp && (trash.area[i] == '.')) |
| 2484 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2485 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2486 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2487 | |
| 2488 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2489 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2490 | |
| 2491 | /* lookup a not neg filter */ |
| 2492 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2493 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2494 | node = n; |
| 2495 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2496 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2497 | } |
| 2498 | if (!node && wildp) { |
| 2499 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2500 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2501 | } |
| 2502 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2503 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2504 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2505 | /* switch ctx done in ssl_sock_generate_certificate */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2506 | return SSL_TLSEXT_ERR_OK; |
| 2507 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2508 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2509 | if (s->strict_sni) |
| 2510 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2511 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2512 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2513 | } |
| 2514 | |
| 2515 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2516 | ssl_sock_switchctx_set(ssl, container_of(node, struct sni_ctx, name)->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2517 | return SSL_TLSEXT_ERR_OK; |
| 2518 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2519 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2520 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2521 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2522 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2523 | |
| 2524 | static DH * ssl_get_dh_1024(void) |
| 2525 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2526 | static unsigned char dh1024_p[]={ |
| 2527 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2528 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2529 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2530 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2531 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2532 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2533 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2534 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2535 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2536 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2537 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2538 | }; |
| 2539 | static unsigned char dh1024_g[]={ |
| 2540 | 0x02, |
| 2541 | }; |
| 2542 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2543 | BIGNUM *p; |
| 2544 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2545 | DH *dh = DH_new(); |
| 2546 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2547 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2548 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2549 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2550 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2551 | DH_free(dh); |
| 2552 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2553 | } else { |
| 2554 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2555 | } |
| 2556 | } |
| 2557 | return dh; |
| 2558 | } |
| 2559 | |
| 2560 | static DH *ssl_get_dh_2048(void) |
| 2561 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2562 | static unsigned char dh2048_p[]={ |
| 2563 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2564 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2565 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2566 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2567 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2568 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2569 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2570 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2571 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2572 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2573 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2574 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2575 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2576 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2577 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2578 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2579 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2580 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2581 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2582 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2583 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2584 | 0xB7,0x1F,0x77,0xF3, |
| 2585 | }; |
| 2586 | static unsigned char dh2048_g[]={ |
| 2587 | 0x02, |
| 2588 | }; |
| 2589 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2590 | BIGNUM *p; |
| 2591 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2592 | DH *dh = DH_new(); |
| 2593 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2594 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2595 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2596 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2597 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2598 | DH_free(dh); |
| 2599 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2600 | } else { |
| 2601 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2602 | } |
| 2603 | } |
| 2604 | return dh; |
| 2605 | } |
| 2606 | |
| 2607 | static DH *ssl_get_dh_4096(void) |
| 2608 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2609 | static unsigned char dh4096_p[]={ |
| 2610 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2611 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2612 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2613 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2614 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2615 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2616 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2617 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2618 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2619 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2620 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2621 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2622 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2623 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2624 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2625 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2626 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2627 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2628 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2629 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2630 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2631 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2632 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2633 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2634 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2635 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2636 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2637 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2638 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2639 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2640 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2641 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2642 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2643 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2644 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2645 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2646 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2647 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2648 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2649 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2650 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2651 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2652 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2653 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2654 | static unsigned char dh4096_g[]={ |
| 2655 | 0x02, |
| 2656 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2657 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2658 | BIGNUM *p; |
| 2659 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2660 | DH *dh = DH_new(); |
| 2661 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2662 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2663 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2664 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2665 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2666 | DH_free(dh); |
| 2667 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2668 | } else { |
| 2669 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2670 | } |
| 2671 | } |
| 2672 | return dh; |
| 2673 | } |
| 2674 | |
| 2675 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2676 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2677 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2678 | { |
| 2679 | DH *dh = NULL; |
| 2680 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2681 | int type; |
| 2682 | |
| 2683 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2684 | |
| 2685 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2686 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2687 | */ |
| 2688 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2689 | keylen = EVP_PKEY_bits(pkey); |
| 2690 | } |
| 2691 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2692 | if (keylen > global_ssl.default_dh_param) { |
| 2693 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2694 | } |
| 2695 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2696 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2697 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2698 | } |
| 2699 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2700 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2701 | } |
| 2702 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2703 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2704 | } |
| 2705 | |
| 2706 | return dh; |
| 2707 | } |
| 2708 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2709 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2710 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2711 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2712 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2713 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2714 | if (in == NULL) |
| 2715 | goto end; |
| 2716 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2717 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2718 | goto end; |
| 2719 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2720 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2721 | |
| 2722 | end: |
| 2723 | if (in) |
| 2724 | BIO_free(in); |
| 2725 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2726 | ERR_clear_error(); |
| 2727 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2728 | return dh; |
| 2729 | } |
| 2730 | |
| 2731 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2732 | { |
| 2733 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2734 | |
| 2735 | if (global_dh) { |
| 2736 | return 0; |
| 2737 | } |
| 2738 | |
| 2739 | return -1; |
| 2740 | } |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2741 | #endif |
| 2742 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2743 | static int ssl_sock_add_cert_sni(SSL_CTX *ctx, struct bind_conf *s, struct ssl_bind_conf *conf, |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2744 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2745 | { |
| 2746 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2747 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2748 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2749 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2750 | if (*name == '!') { |
| 2751 | neg = 1; |
| 2752 | name++; |
| 2753 | } |
| 2754 | if (*name == '*') { |
| 2755 | wild = 1; |
| 2756 | name++; |
| 2757 | } |
| 2758 | /* !* filter is a nop */ |
| 2759 | if (neg && wild) |
| 2760 | return order; |
| 2761 | if (*name) { |
| 2762 | int j, len; |
| 2763 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2764 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2765 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2766 | if (j >= trash.size) |
| 2767 | return order; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2768 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2769 | |
| 2770 | /* Check for duplicates. */ |
| 2771 | if (wild) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2772 | node = ebst_lookup(&s->sni_w_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2773 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2774 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2775 | for (; node; node = ebmb_next_dup(node)) { |
| 2776 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2777 | if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2778 | return order; |
| 2779 | } |
| 2780 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2781 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2782 | if (!sc) |
| 2783 | return order; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2784 | memcpy(sc->name.key, trash.area, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2785 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2786 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2787 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2788 | sc->order = order++; |
| 2789 | sc->neg = neg; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2790 | if (kinfo.sig != TLSEXT_signature_anonymous) |
| 2791 | SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2792 | if (wild) |
| 2793 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2794 | else |
| 2795 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2796 | } |
| 2797 | return order; |
| 2798 | } |
| 2799 | |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2800 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2801 | * tree used to store the ckchs ordered by filename/bundle name |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2802 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2803 | struct eb_root ckchs_tree = EB_ROOT_UNIQUE; |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 2804 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2805 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 2806 | /* Loads Diffie-Hellman parameter from a ckchs. Returns 1 if loaded, else -1 |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2807 | if an error occurred, and 0 if parameter not found. */ |
| 2808 | #ifndef OPENSSL_NO_DH |
| 2809 | static int ssl_sock_load_dh_params(SSL_CTX *ctx, const struct cert_key_and_chain *ckch) |
| 2810 | { |
| 2811 | int ret = -1; |
| 2812 | DH *dh = NULL; |
| 2813 | |
William Lallemand | a8c7374 | 2019-07-31 18:31:34 +0200 | [diff] [blame] | 2814 | if (ckch && ckch->dh) { |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2815 | dh = ckch->dh; |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2816 | ret = 1; |
| 2817 | SSL_CTX_set_tmp_dh(ctx, dh); |
| 2818 | |
| 2819 | if (ssl_dh_ptr_index >= 0) { |
| 2820 | /* store a pointer to the DH params to avoid complaining about |
| 2821 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2822 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2823 | } |
| 2824 | } |
| 2825 | else if (global_dh) { |
| 2826 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2827 | ret = 0; /* DH params not found */ |
| 2828 | } |
| 2829 | else { |
| 2830 | /* Clear openssl global errors stack */ |
| 2831 | ERR_clear_error(); |
| 2832 | |
| 2833 | if (global_ssl.default_dh_param <= 1024) { |
| 2834 | /* we are limited to DH parameter of 1024 bits anyway */ |
| 2835 | if (local_dh_1024 == NULL) |
| 2836 | local_dh_1024 = ssl_get_dh_1024(); |
| 2837 | |
| 2838 | if (local_dh_1024 == NULL) |
| 2839 | goto end; |
| 2840 | |
| 2841 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
| 2842 | } |
| 2843 | else { |
| 2844 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2845 | } |
| 2846 | |
| 2847 | ret = 0; /* DH params not found */ |
| 2848 | } |
| 2849 | |
| 2850 | end: |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2851 | return ret; |
| 2852 | } |
| 2853 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2854 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2855 | /* Frees the contents of a cert_key_and_chain |
| 2856 | */ |
| 2857 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2858 | { |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2859 | if (!ckch) |
| 2860 | return; |
| 2861 | |
| 2862 | /* Free the certificate and set pointer to NULL */ |
| 2863 | if (ckch->cert) |
| 2864 | X509_free(ckch->cert); |
| 2865 | ckch->cert = NULL; |
| 2866 | |
| 2867 | /* Free the key and set pointer to NULL */ |
| 2868 | if (ckch->key) |
| 2869 | EVP_PKEY_free(ckch->key); |
| 2870 | ckch->key = NULL; |
| 2871 | |
| 2872 | /* Free each certificate in the chain */ |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 2873 | if (ckch->chain) |
| 2874 | sk_X509_pop_free(ckch->chain, X509_free); |
| 2875 | ckch->chain = NULL; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2876 | |
| 2877 | } |
| 2878 | |
| 2879 | /* checks if a key and cert exists in the ckch |
| 2880 | */ |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 2881 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2882 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2883 | { |
| 2884 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2885 | } |
William Lallemand | 1633e39 | 2019-09-30 12:58:13 +0200 | [diff] [blame] | 2886 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2887 | |
| 2888 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2889 | * This allows us to carry the contents of the file without having to |
| 2890 | * read the file multiple times. |
Emmanuel Hocdet | efa4b95 | 2018-12-04 17:37:47 +0100 | [diff] [blame] | 2891 | * The caller must call ssl_sock_free_cert_key_and_chain_contents. |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2892 | * |
| 2893 | * returns: |
| 2894 | * 0 on Success |
| 2895 | * 1 on SSL Failure |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2896 | */ |
| 2897 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 2898 | { |
| 2899 | |
| 2900 | BIO *in; |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 2901 | X509 *ca; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2902 | int ret = 1; |
| 2903 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2904 | in = BIO_new(BIO_s_file()); |
| 2905 | if (in == NULL) |
| 2906 | goto end; |
| 2907 | |
| 2908 | if (BIO_read_filename(in, path) <= 0) |
| 2909 | goto end; |
| 2910 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2911 | /* Read Private Key */ |
| 2912 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 2913 | if (ckch->key == NULL) { |
| 2914 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 2915 | err && *err ? *err : "", path); |
| 2916 | goto end; |
| 2917 | } |
| 2918 | |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 2919 | #ifndef OPENSSL_NO_DH |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2920 | /* Seek back to beginning of file */ |
| 2921 | if (BIO_reset(in) == -1) { |
| 2922 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2923 | err && *err ? *err : "", path); |
| 2924 | goto end; |
| 2925 | } |
| 2926 | |
| 2927 | ckch->dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2928 | /* no need to check for NULL there, dh is not mandatory */ |
Emmanuel Hocdet | 54227d8 | 2019-07-30 17:04:01 +0200 | [diff] [blame] | 2929 | #endif |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 2930 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2931 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 2932 | if (BIO_reset(in) == -1) { |
| 2933 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 2934 | err && *err ? *err : "", path); |
| 2935 | goto end; |
| 2936 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2937 | |
| 2938 | /* Read Certificate */ |
| 2939 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 2940 | if (ckch->cert == NULL) { |
| 2941 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 2942 | err && *err ? *err : "", path); |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 2943 | goto end; |
| 2944 | } |
| 2945 | |
Emmanuel Hocdet | 03e09f3 | 2019-07-30 14:21:25 +0200 | [diff] [blame] | 2946 | if (!X509_check_private_key(ckch->cert, ckch->key)) { |
| 2947 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 2948 | err && *err ? *err : "", path); |
| 2949 | goto end; |
| 2950 | } |
| 2951 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2952 | /* Read Certificate Chain */ |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 2953 | ckch->chain = sk_X509_new_null(); |
| 2954 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) |
| 2955 | if (!sk_X509_push(ckch->chain, ca)) { |
| 2956 | X509_free(ca); |
| 2957 | goto end; |
| 2958 | } |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2959 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2960 | ret = ERR_get_error(); |
| 2961 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 2962 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 2963 | err && *err ? *err : "", path); |
| 2964 | ret = 1; |
| 2965 | goto end; |
| 2966 | } |
| 2967 | |
| 2968 | ret = 0; |
| 2969 | |
| 2970 | end: |
| 2971 | |
| 2972 | ERR_clear_error(); |
| 2973 | if (in) |
| 2974 | BIO_free(in); |
| 2975 | |
| 2976 | /* Something went wrong in one of the reads */ |
| 2977 | if (ret != 0) |
| 2978 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 2979 | |
| 2980 | return ret; |
| 2981 | } |
| 2982 | |
| 2983 | /* Loads the info in ckch into ctx |
| 2984 | * Currently, this does not process any information about ocsp, dhparams or |
| 2985 | * sctl |
| 2986 | * Returns |
| 2987 | * 0 on success |
| 2988 | * 1 on failure |
| 2989 | */ |
| 2990 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 2991 | { |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2992 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 2993 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 2994 | err && *err ? *err : "", path); |
| 2995 | return 1; |
| 2996 | } |
| 2997 | |
| 2998 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 2999 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3000 | err && *err ? *err : "", path); |
| 3001 | return 1; |
| 3002 | } |
| 3003 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3004 | /* 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] | 3005 | #ifdef SSL_CTX_set1_chain |
Emmanuel Hocdet | 9246f8b | 2018-11-30 16:00:21 +0100 | [diff] [blame] | 3006 | if (!SSL_CTX_set1_chain(ctx, ckch->chain)) { |
| 3007 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3008 | err && *err ? *err : "", path); |
| 3009 | return 1; |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3010 | } |
Emmanuel Hocdet | 1c65fdd | 2018-12-03 18:07:44 +0100 | [diff] [blame] | 3011 | #else |
| 3012 | { /* legacy compat (< openssl 1.0.2) */ |
| 3013 | X509 *ca; |
| 3014 | while ((ca = sk_X509_shift(ckch->chain))) |
| 3015 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3016 | memprintf(err, "%sunable to load chain certificate into SSL Context '%s'.\n", |
| 3017 | err && *err ? *err : "", path); |
| 3018 | X509_free(ca); |
| 3019 | return 1; |
| 3020 | } |
| 3021 | } |
| 3022 | #endif |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3023 | |
William Lallemand | fa89222 | 2019-07-23 16:06:08 +0200 | [diff] [blame] | 3024 | #ifndef OPENSSL_NO_DH |
| 3025 | /* store a NULL pointer to indicate we have not yet loaded |
| 3026 | a custom DH param file */ |
| 3027 | if (ssl_dh_ptr_index >= 0) { |
| 3028 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3029 | } |
| 3030 | |
| 3031 | if (ssl_sock_load_dh_params(ctx, ckch) < 0) { |
| 3032 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3033 | err && *err ? *err : "", path); |
| 3034 | return 1; |
| 3035 | } |
| 3036 | #endif |
| 3037 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3038 | return 0; |
| 3039 | } |
| 3040 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3041 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3042 | |
| 3043 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 3044 | { |
| 3045 | struct sni_keytype *s_kt = NULL; |
| 3046 | struct ebmb_node *node; |
| 3047 | int i; |
| 3048 | |
| 3049 | for (i = 0; i < trash.size; i++) { |
| 3050 | if (!str[i]) |
| 3051 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3052 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3053 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3054 | trash.area[i] = 0; |
| 3055 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3056 | if (!node) { |
| 3057 | /* CN not found in tree */ |
| 3058 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3059 | /* Using memcpy here instead of strncpy. |
| 3060 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3061 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3062 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3063 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3064 | s_kt->keytypes = 0; |
| 3065 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3066 | } else { |
| 3067 | /* CN found in tree */ |
| 3068 | s_kt = container_of(node, struct sni_keytype, name); |
| 3069 | } |
| 3070 | |
| 3071 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3072 | s_kt->keytypes |= 1<<key_index; |
| 3073 | |
| 3074 | } |
| 3075 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3076 | #endif |
| 3077 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3078 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3079 | * lookup a path into the ckchs tree. |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3080 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3081 | static inline struct ckch_store *ckchs_lookup(char *path) |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3082 | { |
| 3083 | struct ebmb_node *eb; |
| 3084 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3085 | eb = ebst_lookup(&ckchs_tree, path); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3086 | if (!eb) |
| 3087 | return NULL; |
| 3088 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3089 | return ebmb_entry(eb, struct ckch_store, node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3093 | * 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] | 3094 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3095 | 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] | 3096 | { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3097 | struct ckch_store *ckchs; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3098 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3099 | ckchs = calloc(1, sizeof(*ckchs) + strlen(path) + 1); |
| 3100 | if (!ckchs) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3101 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 3102 | goto end; |
| 3103 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3104 | 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] | 3105 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3106 | if (!ckchs->ckch) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3107 | memprintf(err, "%sunable to allocate memory.\n", err && *err ? *err : ""); |
| 3108 | goto end; |
| 3109 | } |
| 3110 | |
| 3111 | if (!multi) { |
| 3112 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3113 | if (ssl_sock_load_crt_file_into_ckch(path, ckchs->ckch, err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3114 | goto end; |
| 3115 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3116 | /* insert into the ckchs tree */ |
| 3117 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 3118 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3119 | } else { |
| 3120 | int found = 0; |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3121 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3122 | char fp[MAXPATHLEN+1] = {0}; |
| 3123 | int n = 0; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3124 | |
| 3125 | /* Load all possible certs and keys */ |
| 3126 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3127 | struct stat buf; |
| 3128 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3129 | if (stat(fp, &buf) == 0) { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3130 | if (ssl_sock_load_crt_file_into_ckch(fp, &ckchs->ckch[n], err) == 1) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3131 | goto end; |
| 3132 | found = 1; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3133 | ckchs->multi = 1; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3134 | } |
| 3135 | } |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3136 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3137 | |
| 3138 | if (!found) { |
William Lallemand | 6e5f2ce | 2019-08-01 14:43:20 +0200 | [diff] [blame] | 3139 | 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] | 3140 | goto end; |
| 3141 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3142 | /* insert into the ckchs tree */ |
| 3143 | memcpy(ckchs->path, path, strlen(path) + 1); |
| 3144 | ebst_insert(&ckchs_tree, &ckchs->node); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3145 | } |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3146 | return ckchs; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3147 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3148 | end: |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3149 | if (ckchs) { |
| 3150 | free(ckchs->ckch); |
| 3151 | ebmb_delete(&ckchs->node); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3152 | } |
| 3153 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3154 | free(ckchs); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3155 | |
| 3156 | return NULL; |
| 3157 | } |
| 3158 | |
William Lallemand | c4ecddf | 2019-07-31 16:50:08 +0200 | [diff] [blame] | 3159 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
| 3160 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3161 | /* |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3162 | * Take a ckch_store which contains a multi-certificate bundle. |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3163 | * Group these certificates into a set of SSL_CTX* |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3164 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3165 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3166 | * 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] | 3167 | * |
| 3168 | * Returns |
| 3169 | * 0 on success |
| 3170 | * 1 on failure |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3171 | * |
| 3172 | * TODO: This function shouldn't access files anymore, sctl and ocsp file access |
| 3173 | * should be migrated to the ssl_sock_load_crt_file_into_ckch() function |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3174 | */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3175 | static int ssl_sock_load_multi_ckchs(const char *path, struct ckch_store *ckchs, |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3176 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3177 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3178 | { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3179 | int i = 0, n = 0; |
| 3180 | struct cert_key_and_chain *certs_and_keys; |
William Lallemand | 4b989f2 | 2019-10-04 18:36:55 +0200 | [diff] [blame^] | 3181 | struct eb_root sni_keytypes_map = EB_ROOT; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3182 | struct ebmb_node *node; |
| 3183 | struct ebmb_node *next; |
| 3184 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 3185 | * of keytypes |
| 3186 | */ |
| 3187 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 3188 | int rv = 0; |
| 3189 | X509_NAME *xname = NULL; |
| 3190 | char *str = NULL; |
| 3191 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3192 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 3193 | #endif |
| 3194 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3195 | if (!ckchs || !ckchs->ckch || !ckchs->multi) { |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3196 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3197 | err && *err ? *err : "", path); |
| 3198 | return 1; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3199 | } |
| 3200 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3201 | certs_and_keys = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3202 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3203 | /* Process each ckch and update keytypes for each CN/SAN |
| 3204 | * for example, if CN/SAN www.a.com is associated with |
| 3205 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3206 | * www.a.com will have: |
| 3207 | * keyindex = 0 | 1 | 4 = 5 |
| 3208 | */ |
| 3209 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3210 | |
| 3211 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3212 | continue; |
| 3213 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3214 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 3215 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3216 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3217 | } else { |
| 3218 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3219 | * so the line that contains logic is marked via comments |
| 3220 | */ |
| 3221 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3222 | i = -1; |
| 3223 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3224 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3225 | ASN1_STRING *value; |
| 3226 | value = X509_NAME_ENTRY_get_data(entry); |
| 3227 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3228 | /* Important line is here */ |
| 3229 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3230 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3231 | OPENSSL_free(str); |
| 3232 | str = NULL; |
| 3233 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3234 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3235 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3236 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3237 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3238 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3239 | if (names) { |
| 3240 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3241 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3242 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3243 | if (name->type == GEN_DNS) { |
| 3244 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3245 | /* Important line is here */ |
| 3246 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3247 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3248 | OPENSSL_free(str); |
| 3249 | str = NULL; |
| 3250 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3251 | } |
| 3252 | } |
| 3253 | } |
| 3254 | } |
| 3255 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3256 | } |
| 3257 | |
| 3258 | /* If no files found, return error */ |
| 3259 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3260 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3261 | err && *err ? *err : "", path); |
| 3262 | rv = 1; |
| 3263 | goto end; |
| 3264 | } |
| 3265 | |
| 3266 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3267 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3268 | * and add each CTX to the SNI tree |
| 3269 | * |
| 3270 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3271 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3272 | * combination is denoted by the key in the map. Each key |
| 3273 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3274 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3275 | * entry in the array to correspond to the unique combo (key) |
| 3276 | * associated with i. This unique key combo (i) will be associated |
| 3277 | * with combos[i-1] |
| 3278 | */ |
| 3279 | |
| 3280 | node = ebmb_first(&sni_keytypes_map); |
| 3281 | while (node) { |
| 3282 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3283 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3284 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3285 | |
| 3286 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3287 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3288 | cur_ctx = key_combos[i-1].ctx; |
| 3289 | |
| 3290 | if (cur_ctx == NULL) { |
| 3291 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3292 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3293 | if (cur_ctx == NULL) { |
| 3294 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3295 | err && *err ? *err : ""); |
| 3296 | rv = 1; |
| 3297 | goto end; |
| 3298 | } |
| 3299 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3300 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3301 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3302 | if (i & (1<<n)) { |
| 3303 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3304 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3305 | if (ssl_sock_put_ckch_into_ctx(cur_file, &certs_and_keys[n], cur_ctx, err) != 0) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3306 | SSL_CTX_free(cur_ctx); |
| 3307 | rv = 1; |
| 3308 | goto end; |
| 3309 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3310 | |
| 3311 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 3312 | /* Load OCSP Info into context */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3313 | /* TODO: store OCSP in ckch */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3314 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3315 | if (err) |
| 3316 | 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", |
Bertrand Jacquin | 5424ee0 | 2016-11-13 16:37:14 +0000 | [diff] [blame] | 3317 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3318 | SSL_CTX_free(cur_ctx); |
| 3319 | rv = 1; |
| 3320 | goto end; |
| 3321 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3322 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3323 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3324 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3325 | } |
| 3326 | } |
| 3327 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3328 | /* Update key_combos */ |
| 3329 | key_combos[i-1].ctx = cur_ctx; |
| 3330 | } |
| 3331 | |
| 3332 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3333 | key_combos[i-1].order = ssl_sock_add_cert_sni(cur_ctx, bind_conf, ssl_conf, |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3334 | kinfo, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3335 | node = ebmb_next(node); |
| 3336 | } |
| 3337 | |
| 3338 | |
| 3339 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3340 | if (!bind_conf->default_ctx) { |
| 3341 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3342 | if (key_combos[i].ctx) { |
| 3343 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3344 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3345 | break; |
| 3346 | } |
| 3347 | } |
| 3348 | } |
| 3349 | |
| 3350 | end: |
| 3351 | |
| 3352 | if (names) |
| 3353 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3354 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3355 | node = ebmb_first(&sni_keytypes_map); |
| 3356 | while (node) { |
| 3357 | next = ebmb_next(node); |
| 3358 | ebmb_delete(node); |
| 3359 | node = next; |
| 3360 | } |
| 3361 | |
| 3362 | return rv; |
| 3363 | } |
| 3364 | #else |
| 3365 | /* This is a dummy, that just logs an error and returns error */ |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3366 | static int ssl_sock_load_multi_ckchs(const char *path, struct ckch_store *ckchs, |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3367 | struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3368 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3369 | { |
| 3370 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3371 | err && *err ? *err : "", path, strerror(errno)); |
| 3372 | return 1; |
| 3373 | } |
| 3374 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3375 | #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] | 3376 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3377 | static int ssl_sock_load_ckchs(const char *path, struct ckch_store *ckchs, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3378 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3379 | { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3380 | SSL_CTX *ctx; |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3381 | int i; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3382 | int order = 0; |
| 3383 | X509_NAME *xname; |
| 3384 | char *str; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3385 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3386 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3387 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3388 | STACK_OF(GENERAL_NAME) *names; |
| 3389 | #endif |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3390 | struct cert_key_and_chain *ckch; |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3391 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3392 | if (!ckchs || !ckchs->ckch) |
William Lallemand | a59191b | 2019-05-15 16:08:56 +0200 | [diff] [blame] | 3393 | return 1; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3394 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3395 | ckch = ckchs->ckch; |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3396 | |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3397 | ctx = SSL_CTX_new(SSLv23_server_method()); |
| 3398 | if (!ctx) { |
| 3399 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3400 | err && *err ? *err : "", path); |
| 3401 | return 1; |
| 3402 | } |
| 3403 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3404 | if (ssl_sock_put_ckch_into_ctx(path, ckch, ctx, err) != 0) { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3405 | SSL_CTX_free(ctx); |
| 3406 | return 1; |
| 3407 | } |
| 3408 | |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3409 | pkey = X509_get_pubkey(ckch->cert); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3410 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3411 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3412 | switch(EVP_PKEY_base_id(pkey)) { |
| 3413 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3414 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3415 | break; |
| 3416 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3417 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3418 | break; |
| 3419 | case EVP_PKEY_DSA: |
| 3420 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3421 | break; |
| 3422 | } |
| 3423 | EVP_PKEY_free(pkey); |
| 3424 | } |
| 3425 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3426 | if (fcount) { |
| 3427 | while (fcount--) |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3428 | order = ssl_sock_add_cert_sni(ctx, bind_conf, ssl_conf, kinfo, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3429 | } |
| 3430 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3431 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3432 | 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] | 3433 | if (names) { |
| 3434 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3435 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3436 | if (name->type == GEN_DNS) { |
| 3437 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3438 | order = ssl_sock_add_cert_sni(ctx, bind_conf, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3439 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3440 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3441 | } |
| 3442 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3443 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3444 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3445 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3446 | xname = X509_get_subject_name(ckch->cert); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3447 | i = -1; |
| 3448 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3449 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3450 | ASN1_STRING *value; |
| 3451 | |
| 3452 | value = X509_NAME_ENTRY_get_data(entry); |
| 3453 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
William Lallemand | c940207 | 2019-05-15 15:33:54 +0200 | [diff] [blame] | 3454 | order = ssl_sock_add_cert_sni(ctx, bind_conf, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3455 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3456 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3457 | } |
| 3458 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3459 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3460 | * the tree, so it will be discovered and cleaned in time. |
| 3461 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3462 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3463 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emmanuel Hocdet | a7a0f99 | 2019-07-30 17:17:03 +0200 | [diff] [blame] | 3464 | if (ssl_sock_load_ocsp(ctx, path) < 0) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3465 | if (err) |
| 3466 | memprintf(err, "%s '%s.ocsp' is present and activates OCSP but it is impossible to compute the OCSP certificate ID (maybe the issuer could not be found)'.\n", |
| 3467 | *err ? *err : "", path); |
| 3468 | return 1; |
| 3469 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3470 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3471 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3472 | #endif |
| 3473 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3474 | #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] | 3475 | if (sctl_ex_index >= 0) { |
Emmanuel Hocdet | a7a0f99 | 2019-07-30 17:17:03 +0200 | [diff] [blame] | 3476 | if (ssl_sock_load_sctl(ctx, path) < 0) { |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 3477 | if (err) |
| 3478 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3479 | *err ? *err : "", path); |
| 3480 | return 1; |
| 3481 | } |
| 3482 | } |
| 3483 | #endif |
| 3484 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3485 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3486 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3487 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3488 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3489 | return 1; |
| 3490 | } |
| 3491 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3492 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3493 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3494 | bind_conf->default_ssl_conf = ssl_conf; |
| 3495 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3496 | |
| 3497 | return 0; |
| 3498 | } |
| 3499 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3500 | 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] | 3501 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3502 | struct dirent **de_list; |
| 3503 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3504 | DIR *dir; |
| 3505 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3506 | char *end; |
| 3507 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3508 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3509 | struct ckch_store *ckchs; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3510 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3511 | int is_bundle; |
| 3512 | int j; |
| 3513 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3514 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3515 | if ((ckchs = ckchs_lookup(path))) { |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3516 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3517 | /* we found the ckchs in the tree, we can use it directly */ |
| 3518 | if (ckchs->multi) |
| 3519 | return ssl_sock_load_multi_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
William Lallemand | 6af0399 | 2019-07-23 15:00:54 +0200 | [diff] [blame] | 3520 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3521 | 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] | 3522 | |
| 3523 | } |
| 3524 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3525 | if (stat(path, &buf) == 0) { |
| 3526 | dir = opendir(path); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3527 | if (!dir) { |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3528 | ckchs = ckchs_load_cert_file(path, 0, err); |
| 3529 | if (!ckchs) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3530 | return 1; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3531 | 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] | 3532 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3533 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3534 | /* strip trailing slashes, including first one */ |
| 3535 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3536 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3537 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3538 | n = scandir(path, &de_list, 0, alphasort); |
| 3539 | if (n < 0) { |
| 3540 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3541 | err && *err ? *err : "", path, strerror(errno)); |
| 3542 | cfgerr++; |
| 3543 | } |
| 3544 | else { |
| 3545 | for (i = 0; i < n; i++) { |
| 3546 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3547 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3548 | end = strrchr(de->d_name, '.'); |
| 3549 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3550 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3551 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3552 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3553 | if (stat(fp, &buf) != 0) { |
| 3554 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3555 | err && *err ? *err : "", fp, strerror(errno)); |
| 3556 | cfgerr++; |
| 3557 | goto ignore_entry; |
| 3558 | } |
| 3559 | if (!S_ISREG(buf.st_mode)) |
| 3560 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3561 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3562 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3563 | is_bundle = 0; |
| 3564 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3565 | |
| 3566 | if (end) { |
| 3567 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3568 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3569 | is_bundle = 1; |
| 3570 | break; |
| 3571 | } |
| 3572 | } |
| 3573 | |
| 3574 | if (is_bundle) { |
| 3575 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3576 | int dp_len; |
| 3577 | |
| 3578 | dp_len = end - de->d_name; |
| 3579 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3580 | |
| 3581 | /* increment i and free de until we get to a non-bundle cert |
| 3582 | * Note here that we look at de_list[i + 1] before freeing de |
| 3583 | * this is important since ignore_entry will free de |
| 3584 | */ |
| 3585 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3586 | free(de); |
| 3587 | i++; |
| 3588 | de = de_list[i]; |
| 3589 | } |
| 3590 | |
| 3591 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3592 | if ((ckchs = ckchs_lookup(fp)) == NULL) |
| 3593 | ckchs = ckchs_load_cert_file(fp, 1, err); |
| 3594 | if (!ckchs) |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 3595 | cfgerr++; |
| 3596 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3597 | cfgerr += ssl_sock_load_multi_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3598 | |
| 3599 | /* Successfully processed the bundle */ |
| 3600 | goto ignore_entry; |
| 3601 | } |
| 3602 | } |
| 3603 | |
| 3604 | #endif |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3605 | if ((ckchs = ckchs_lookup(fp)) == NULL) |
| 3606 | ckchs = ckchs_load_cert_file(fp, 0, err); |
| 3607 | if (!ckchs) |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 3608 | cfgerr++; |
| 3609 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3610 | cfgerr += ssl_sock_load_ckchs(fp, ckchs, bind_conf, NULL, NULL, 0, err); |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3611 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3612 | ignore_entry: |
| 3613 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3614 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3615 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3616 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3617 | closedir(dir); |
| 3618 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3619 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3620 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3621 | ckchs = ckchs_load_cert_file(path, 1, err); |
| 3622 | if (!ckchs) |
William Lallemand | 36b8463 | 2019-07-18 19:28:17 +0200 | [diff] [blame] | 3623 | return 1; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3624 | cfgerr = ssl_sock_load_multi_ckchs(path, ckchs, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3625 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3626 | return cfgerr; |
| 3627 | } |
| 3628 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3629 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3630 | * done once. Zero is returned if the operation fails. No error is returned |
| 3631 | * if the random is said as not implemented, because we expect that openssl |
| 3632 | * will use another method once needed. |
| 3633 | */ |
| 3634 | static int ssl_initialize_random() |
| 3635 | { |
| 3636 | unsigned char random; |
| 3637 | static int random_initialized = 0; |
| 3638 | |
| 3639 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3640 | random_initialized = 1; |
| 3641 | |
| 3642 | return random_initialized; |
| 3643 | } |
| 3644 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3645 | /* release ssl bind conf */ |
| 3646 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3647 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3648 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 3649 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3650 | free(conf->npn_str); |
| 3651 | conf->npn_str = NULL; |
| 3652 | #endif |
| 3653 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3654 | free(conf->alpn_str); |
| 3655 | conf->alpn_str = NULL; |
| 3656 | #endif |
| 3657 | free(conf->ca_file); |
| 3658 | conf->ca_file = NULL; |
| 3659 | free(conf->crl_file); |
| 3660 | conf->crl_file = NULL; |
| 3661 | free(conf->ciphers); |
| 3662 | conf->ciphers = NULL; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 3663 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 3664 | free(conf->ciphersuites); |
| 3665 | conf->ciphersuites = NULL; |
| 3666 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3667 | free(conf->curves); |
| 3668 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3669 | free(conf->ecdhe); |
| 3670 | conf->ecdhe = NULL; |
| 3671 | } |
| 3672 | } |
| 3673 | |
| 3674 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3675 | { |
| 3676 | char thisline[CRT_LINESIZE]; |
| 3677 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3678 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3679 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3680 | int linenum = 0; |
| 3681 | int cfgerr = 0; |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3682 | struct ckch_store *ckchs; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3683 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3684 | if ((f = fopen(file, "r")) == NULL) { |
| 3685 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3686 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3687 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3688 | |
| 3689 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3690 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3691 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3692 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3693 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3694 | char *crt_path; |
| 3695 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3696 | |
| 3697 | linenum++; |
| 3698 | end = line + strlen(line); |
| 3699 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3700 | /* Check if we reached the limit and the last char is not \n. |
| 3701 | * Watch out for the last line without the terminating '\n'! |
| 3702 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3703 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3704 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3705 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3706 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3707 | } |
| 3708 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3709 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3710 | newarg = 1; |
| 3711 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3712 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3713 | /* end of string, end of loop */ |
| 3714 | *line = 0; |
| 3715 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3716 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3717 | newarg = 1; |
| 3718 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3719 | } else if (*line == '[') { |
| 3720 | if (ssl_b) { |
| 3721 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3722 | cfgerr = 1; |
| 3723 | break; |
| 3724 | } |
| 3725 | if (!arg) { |
| 3726 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3727 | cfgerr = 1; |
| 3728 | break; |
| 3729 | } |
| 3730 | ssl_b = arg; |
| 3731 | newarg = 1; |
| 3732 | *line = 0; |
| 3733 | } else if (*line == ']') { |
| 3734 | if (ssl_e) { |
| 3735 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3736 | cfgerr = 1; |
| 3737 | break; |
| 3738 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3739 | if (!ssl_b) { |
| 3740 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3741 | cfgerr = 1; |
| 3742 | break; |
| 3743 | } |
| 3744 | ssl_e = arg; |
| 3745 | newarg = 1; |
| 3746 | *line = 0; |
| 3747 | } else if (newarg) { |
| 3748 | if (arg == MAX_CRT_ARGS) { |
| 3749 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3750 | cfgerr = 1; |
| 3751 | break; |
| 3752 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3753 | newarg = 0; |
| 3754 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3755 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3756 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3757 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3758 | if (cfgerr) |
| 3759 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3760 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3761 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3762 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3763 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3764 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3765 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3766 | crt_path = args[0]; |
| 3767 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3768 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3769 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3770 | crt_path, linenum, file); |
| 3771 | cfgerr = 1; |
| 3772 | break; |
| 3773 | } |
| 3774 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3775 | crt_path = path; |
| 3776 | } |
| 3777 | |
| 3778 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3779 | cur_arg = ssl_b ? ssl_b : 1; |
| 3780 | while (cur_arg < ssl_e) { |
| 3781 | newarg = 0; |
| 3782 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3783 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3784 | newarg = 1; |
| 3785 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3786 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3787 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3788 | args[cur_arg], linenum, file); |
| 3789 | cfgerr = 1; |
| 3790 | } |
| 3791 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3792 | break; |
| 3793 | } |
| 3794 | } |
| 3795 | if (!cfgerr && !newarg) { |
| 3796 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3797 | args[cur_arg], linenum, file); |
| 3798 | cfgerr = 1; |
| 3799 | break; |
| 3800 | } |
| 3801 | } |
| 3802 | if (cfgerr) { |
| 3803 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3804 | free(ssl_conf); |
| 3805 | ssl_conf = NULL; |
| 3806 | break; |
| 3807 | } |
| 3808 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3809 | if ((ckchs = ckchs_lookup(crt_path)) == NULL) { |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 3810 | if (stat(crt_path, &buf) == 0) |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3811 | ckchs = ckchs_load_cert_file(crt_path, 0, err); |
Emmanuel Hocdet | 1503e05 | 2019-07-31 18:30:33 +0200 | [diff] [blame] | 3812 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3813 | ckchs = ckchs_load_cert_file(crt_path, 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3814 | } |
| 3815 | |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3816 | if (!ckchs) |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 3817 | cfgerr++; |
| 3818 | else |
William Lallemand | e3af8fb | 2019-10-08 11:36:53 +0200 | [diff] [blame] | 3819 | cfgerr += ssl_sock_load_ckchs(crt_path, ckchs, bind_conf, ssl_conf, |
William Lallemand | eed4bf2 | 2019-10-10 11:38:13 +0200 | [diff] [blame] | 3820 | &args[cur_arg], arg - cur_arg - 1, err); |
| 3821 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3822 | if (cfgerr) { |
| 3823 | 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] | 3824 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3825 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3826 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3827 | fclose(f); |
| 3828 | return cfgerr; |
| 3829 | } |
| 3830 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3831 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3832 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3833 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3834 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3835 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3836 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3837 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3838 | SSL_OP_NO_SSLv2 | |
| 3839 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3840 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3841 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3842 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3843 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3844 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3845 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3846 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3847 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3848 | SSL_MODE_RELEASE_BUFFERS | |
| 3849 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3850 | 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] | 3851 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3852 | int flags = MC_SSL_O_ALL; |
| 3853 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3854 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3855 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3856 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3857 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3858 | 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] | 3859 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3860 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3861 | 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] | 3862 | else |
| 3863 | flags = conf_ssl_methods->flags; |
| 3864 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3865 | min = conf_ssl_methods->min; |
| 3866 | max = conf_ssl_methods->max; |
| 3867 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3868 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3869 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3870 | /* 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] | 3871 | if (min) |
| 3872 | flags |= (methodVersions[min].flag - 1); |
| 3873 | if (max) |
| 3874 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3875 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3876 | min = max = CONF_TLSV_NONE; |
| 3877 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3878 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3879 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3880 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3881 | if (min) { |
| 3882 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3883 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3884 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3885 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3886 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3887 | hole = 0; |
| 3888 | } |
| 3889 | max = i; |
| 3890 | } |
| 3891 | else { |
| 3892 | min = max = i; |
| 3893 | } |
| 3894 | } |
| 3895 | else { |
| 3896 | if (min) |
| 3897 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3898 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3899 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3900 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3901 | 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] | 3902 | cfgerr += 1; |
| 3903 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3904 | /* save real min/max in bind_conf */ |
| 3905 | conf_ssl_methods->min = min; |
| 3906 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3907 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3908 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3909 | /* 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] | 3910 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3911 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3912 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3913 | else |
| 3914 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3915 | if (flags & methodVersions[i].flag) |
| 3916 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3917 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3918 | /* 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] | 3919 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3920 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3921 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3922 | |
| 3923 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3924 | options |= SSL_OP_NO_TICKET; |
| 3925 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3926 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 3927 | |
| 3928 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 3929 | options |= SSL_OP_NO_RENEGOTIATION; |
| 3930 | #endif |
| 3931 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3932 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3933 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3934 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3935 | if (global_ssl.async) |
| 3936 | mode |= SSL_MODE_ASYNC; |
| 3937 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3938 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3939 | if (global_ssl.life_time) |
| 3940 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3941 | |
| 3942 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3943 | #ifdef OPENSSL_IS_BORINGSSL |
| 3944 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 3945 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3946 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 3947 | if (bind_conf->ssl_conf.early_data) { |
| 3948 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 3949 | SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite); |
| 3950 | } |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 3951 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 3952 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3953 | #else |
| 3954 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3955 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 3956 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3957 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3958 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3959 | } |
| 3960 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 3961 | |
| 3962 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 3963 | { |
| 3964 | if (first == block) { |
| 3965 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3966 | if (first->len > 0) |
| 3967 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | /* return first block from sh_ssl_sess */ |
| 3972 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 3973 | { |
| 3974 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 3975 | |
| 3976 | } |
| 3977 | |
| 3978 | /* store a session into the cache |
| 3979 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 3980 | * data: asn1 encoded session |
| 3981 | * data_len: asn1 encoded session length |
| 3982 | * Returns 1 id session was stored (else 0) |
| 3983 | */ |
| 3984 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 3985 | { |
| 3986 | struct shared_block *first; |
| 3987 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 3988 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 3989 | 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] | 3990 | if (!first) { |
| 3991 | /* Could not retrieve enough free blocks to store that session */ |
| 3992 | return 0; |
| 3993 | } |
| 3994 | |
| 3995 | /* STORE the key in the first elem */ |
| 3996 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 3997 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 3998 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 3999 | |
| 4000 | /* it returns the already existing node |
| 4001 | or current node if none, never returns null */ |
| 4002 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4003 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4004 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4005 | /* release the reserved row */ |
| 4006 | shctx_row_dec_hot(ssl_shctx, first); |
| 4007 | /* replace the previous session already in the tree */ |
| 4008 | sh_ssl_sess = oldsh_ssl_sess; |
| 4009 | /* ignore the previous session data, only use the header */ |
| 4010 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4011 | shctx_row_inc_hot(ssl_shctx, first); |
| 4012 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4013 | } |
| 4014 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4015 | 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] | 4016 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4017 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4021 | |
| 4022 | return 1; |
| 4023 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4024 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4025 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4026 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4027 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4028 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4029 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4030 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4031 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4032 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4033 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4034 | int len; |
| 4035 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4036 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4037 | len = i2d_SSL_SESSION(sess, NULL); |
| 4038 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4039 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4040 | } else { |
| 4041 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4042 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 4043 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4044 | } |
| 4045 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4046 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4047 | &ptr); |
| 4048 | } |
| 4049 | } else { |
| 4050 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4051 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4052 | } |
| 4053 | |
| 4054 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4055 | } |
| 4056 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4057 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4058 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4059 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4060 | { |
| 4061 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4062 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4063 | unsigned char *p; |
| 4064 | int data_len; |
| 4065 | unsigned int sid_length, sid_ctx_length; |
| 4066 | const unsigned char *sid_data; |
| 4067 | const unsigned char *sid_ctx_data; |
| 4068 | |
| 4069 | /* Session id is already stored in to key and session id is known |
| 4070 | * so we dont store it to keep size. |
| 4071 | */ |
| 4072 | |
| 4073 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4074 | sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length); |
| 4075 | SSL_SESSION_set1_id(sess, sid_data, 0); |
| 4076 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0); |
| 4077 | |
| 4078 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4079 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4080 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4081 | goto err; |
| 4082 | |
| 4083 | p = encsess; |
| 4084 | |
| 4085 | /* process ASN1 session encoding before the lock */ |
| 4086 | i2d_SSL_SESSION(sess, &p); |
| 4087 | |
| 4088 | memcpy(encid, sid_data, sid_length); |
| 4089 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4090 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4091 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4092 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4093 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4094 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4095 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4096 | err: |
| 4097 | /* reset original length values */ |
| 4098 | SSL_SESSION_set1_id(sess, sid_data, sid_length); |
| 4099 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length); |
| 4100 | |
| 4101 | return 0; /* do not increment session reference count */ |
| 4102 | } |
| 4103 | |
| 4104 | /* 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] | 4105 | 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] | 4106 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4107 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4108 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4109 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4110 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4111 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4112 | |
| 4113 | global.shctx_lookups++; |
| 4114 | |
| 4115 | /* allow the session to be freed automatically by openssl */ |
| 4116 | *do_copy = 0; |
| 4117 | |
| 4118 | /* tree key is zeros padded sessionid */ |
| 4119 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4120 | memcpy(tmpkey, key, key_len); |
| 4121 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4122 | key = tmpkey; |
| 4123 | } |
| 4124 | |
| 4125 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4126 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4127 | |
| 4128 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4129 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4130 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4131 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4132 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4133 | global.shctx_misses++; |
| 4134 | return NULL; |
| 4135 | } |
| 4136 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4137 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4138 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4139 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4140 | 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] | 4141 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4142 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4143 | |
| 4144 | /* decode ASN1 session */ |
| 4145 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4146 | 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] | 4147 | /* Reset session id and session id contenxt */ |
| 4148 | if (sess) { |
| 4149 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4150 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4151 | } |
| 4152 | |
| 4153 | return sess; |
| 4154 | } |
| 4155 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4156 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4157 | /* 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] | 4158 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4159 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4160 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4161 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4162 | unsigned int sid_length; |
| 4163 | const unsigned char *sid_data; |
| 4164 | (void)ctx; |
| 4165 | |
| 4166 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4167 | /* tree key is zeros padded sessionid */ |
| 4168 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4169 | memcpy(tmpkey, sid_data, sid_length); |
| 4170 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4171 | sid_data = tmpkey; |
| 4172 | } |
| 4173 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4174 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4175 | |
| 4176 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4177 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4178 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4179 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4180 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4181 | } |
| 4182 | |
| 4183 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4184 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4185 | } |
| 4186 | |
| 4187 | /* Set session cache mode to server and disable openssl internal cache. |
| 4188 | * Set shared cache callbacks on an ssl context. |
| 4189 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4190 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4191 | { |
| 4192 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4193 | |
| 4194 | if (!ssl_shctx) { |
| 4195 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4196 | return; |
| 4197 | } |
| 4198 | |
| 4199 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4200 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4201 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4202 | |
| 4203 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4204 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4205 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4206 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4207 | } |
| 4208 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4209 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 4210 | { |
| 4211 | struct proxy *curproxy = bind_conf->frontend; |
| 4212 | int cfgerr = 0; |
| 4213 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4214 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4215 | const char *conf_ciphers; |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4216 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4217 | const char *conf_ciphersuites; |
| 4218 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4219 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4220 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4221 | if (ssl_conf) { |
| 4222 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4223 | int i, min, max; |
| 4224 | int flags = MC_SSL_O_ALL; |
| 4225 | |
| 4226 | /* 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] | 4227 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4228 | 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] | 4229 | if (min) |
| 4230 | flags |= (methodVersions[min].flag - 1); |
| 4231 | if (max) |
| 4232 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4233 | min = max = CONF_TLSV_NONE; |
| 4234 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4235 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4236 | if (min) |
| 4237 | max = i; |
| 4238 | else |
| 4239 | min = max = i; |
| 4240 | } |
| 4241 | /* save real min/max */ |
| 4242 | conf_ssl_methods->min = min; |
| 4243 | conf_ssl_methods->max = max; |
| 4244 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4245 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4246 | 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] | 4247 | cfgerr += 1; |
| 4248 | } |
| 4249 | } |
| 4250 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4251 | 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] | 4252 | case SSL_SOCK_VERIFY_NONE: |
| 4253 | verify = SSL_VERIFY_NONE; |
| 4254 | break; |
| 4255 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4256 | verify = SSL_VERIFY_PEER; |
| 4257 | break; |
| 4258 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4259 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4260 | break; |
| 4261 | } |
| 4262 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4263 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4264 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 4265 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 4266 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4267 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4268 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4269 | ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
| 4270 | 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] | 4271 | cfgerr++; |
| 4272 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4273 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4274 | /* set CA names for client cert request, function returns void */ |
| 4275 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4276 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4277 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4278 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4279 | ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4280 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4281 | cfgerr++; |
| 4282 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4283 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4284 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4285 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4286 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4287 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4288 | ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4289 | 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] | 4290 | cfgerr++; |
| 4291 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4292 | else { |
| 4293 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4294 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4295 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4296 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4297 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4298 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4299 | #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] | 4300 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4301 | 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] | 4302 | ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4303 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4304 | cfgerr++; |
| 4305 | } |
| 4306 | } |
| 4307 | #endif |
| 4308 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4309 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4310 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4311 | if (conf_ciphers && |
| 4312 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4313 | ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4314 | 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] | 4315 | cfgerr++; |
| 4316 | } |
| 4317 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4318 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4319 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4320 | if (conf_ciphersuites && |
| 4321 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
| 4322 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4323 | curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4324 | cfgerr++; |
| 4325 | } |
| 4326 | #endif |
| 4327 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4328 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4329 | /* If tune.ssl.default-dh-param has not been set, |
| 4330 | neither has ssl-default-dh-file and no static DH |
| 4331 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4332 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4333 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4334 | (ssl_dh_ptr_index == -1 || |
| 4335 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4336 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4337 | const SSL_CIPHER * cipher = NULL; |
| 4338 | char cipher_description[128]; |
| 4339 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4340 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4341 | which is not ephemeral DH. */ |
| 4342 | const char dhe_description[] = " Kx=DH "; |
| 4343 | const char dhe_export_description[] = " Kx=DH("; |
| 4344 | int idx = 0; |
| 4345 | int dhe_found = 0; |
| 4346 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4347 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4348 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4349 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4350 | if (ssl) { |
| 4351 | ciphers = SSL_get_ciphers(ssl); |
| 4352 | |
| 4353 | if (ciphers) { |
| 4354 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4355 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4356 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4357 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4358 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4359 | dhe_found = 1; |
| 4360 | break; |
| 4361 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4362 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4363 | } |
| 4364 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4365 | SSL_free(ssl); |
| 4366 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4367 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4368 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4369 | if (dhe_found) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4370 | 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] | 4371 | } |
| 4372 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4373 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4374 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4375 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4376 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4377 | if (local_dh_1024 == NULL) { |
| 4378 | local_dh_1024 = ssl_get_dh_1024(); |
| 4379 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4380 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4381 | if (local_dh_2048 == NULL) { |
| 4382 | local_dh_2048 = ssl_get_dh_2048(); |
| 4383 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4384 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4385 | if (local_dh_4096 == NULL) { |
| 4386 | local_dh_4096 = ssl_get_dh_4096(); |
| 4387 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4388 | } |
| 4389 | } |
| 4390 | } |
| 4391 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4392 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4393 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4394 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4395 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4396 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4397 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4398 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4399 | ssl_conf_cur = NULL; |
| 4400 | if (ssl_conf && ssl_conf->npn_str) |
| 4401 | ssl_conf_cur = ssl_conf; |
| 4402 | else if (bind_conf->ssl_conf.npn_str) |
| 4403 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4404 | if (ssl_conf_cur) |
| 4405 | 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] | 4406 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4407 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4408 | ssl_conf_cur = NULL; |
| 4409 | if (ssl_conf && ssl_conf->alpn_str) |
| 4410 | ssl_conf_cur = ssl_conf; |
| 4411 | else if (bind_conf->ssl_conf.alpn_str) |
| 4412 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4413 | if (ssl_conf_cur) |
| 4414 | 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] | 4415 | #endif |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4416 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4417 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4418 | if (conf_curves) { |
| 4419 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4420 | ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4421 | 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] | 4422 | cfgerr++; |
| 4423 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4424 | #if defined(SSL_CTX_set_ecdh_auto) |
| 4425 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 4426 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4427 | } |
| 4428 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4429 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4430 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4431 | int i; |
| 4432 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4433 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4434 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4435 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4436 | NULL); |
| 4437 | |
| 4438 | if (ecdhe == NULL) { |
| 4439 | SSL_CTX_set_dh_auto(ctx, 1); |
| 4440 | return cfgerr; |
| 4441 | } |
| 4442 | #else |
| 4443 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4444 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4445 | ECDHE_DEFAULT_CURVE); |
| 4446 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4447 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4448 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4449 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4450 | ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4451 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4452 | cfgerr++; |
| 4453 | } |
| 4454 | else { |
| 4455 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4456 | EC_KEY_free(ecdh); |
| 4457 | } |
| 4458 | } |
| 4459 | #endif |
| 4460 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4461 | return cfgerr; |
| 4462 | } |
| 4463 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4464 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4465 | { |
| 4466 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4467 | size_t prefixlen, suffixlen; |
| 4468 | |
| 4469 | /* Trivial case */ |
| 4470 | if (strcmp(pattern, hostname) == 0) |
| 4471 | return 1; |
| 4472 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4473 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4474 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4475 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4476 | pattern_wildcard = NULL; |
| 4477 | pattern_left_label_end = pattern; |
| 4478 | while (*pattern_left_label_end != '.') { |
| 4479 | switch (*pattern_left_label_end) { |
| 4480 | case 0: |
| 4481 | /* End of label not found */ |
| 4482 | return 0; |
| 4483 | case '*': |
| 4484 | /* If there is more than one wildcards */ |
| 4485 | if (pattern_wildcard) |
| 4486 | return 0; |
| 4487 | pattern_wildcard = pattern_left_label_end; |
| 4488 | break; |
| 4489 | } |
| 4490 | pattern_left_label_end++; |
| 4491 | } |
| 4492 | |
| 4493 | /* If it's not trivial and there is no wildcard, it can't |
| 4494 | * match */ |
| 4495 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4496 | return 0; |
| 4497 | |
| 4498 | /* Make sure all labels match except the leftmost */ |
| 4499 | hostname_left_label_end = strchr(hostname, '.'); |
| 4500 | if (!hostname_left_label_end |
| 4501 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4502 | return 0; |
| 4503 | |
| 4504 | /* Make sure the leftmost label of the hostname is long enough |
| 4505 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4506 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4507 | return 0; |
| 4508 | |
| 4509 | /* Finally compare the string on either side of the |
| 4510 | * wildcard */ |
| 4511 | prefixlen = pattern_wildcard - pattern; |
| 4512 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4513 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4514 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4515 | return 0; |
| 4516 | |
| 4517 | return 1; |
| 4518 | } |
| 4519 | |
| 4520 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4521 | { |
| 4522 | SSL *ssl; |
| 4523 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4524 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4525 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4526 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4527 | |
| 4528 | int depth; |
| 4529 | X509 *cert; |
| 4530 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4531 | int i; |
| 4532 | X509_NAME *cert_subject; |
| 4533 | char *str; |
| 4534 | |
| 4535 | if (ok == 0) |
| 4536 | return ok; |
| 4537 | |
| 4538 | 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] | 4539 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4540 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4541 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4542 | /* We're checking if the provided hostnames match the desired one. The |
| 4543 | * desired hostname comes from the SNI we presented if any, or if not |
| 4544 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4545 | * directive. If neither is set, we don't care about the name so the |
| 4546 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4547 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4548 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4549 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4550 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4551 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4552 | if (!servername) |
| 4553 | return ok; |
| 4554 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4555 | |
| 4556 | /* We only need to verify the CN on the actual server cert, |
| 4557 | * not the indirect CAs */ |
| 4558 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4559 | if (depth != 0) |
| 4560 | return ok; |
| 4561 | |
| 4562 | /* At this point, the cert is *not* OK unless we can find a |
| 4563 | * hostname match */ |
| 4564 | ok = 0; |
| 4565 | |
| 4566 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4567 | /* It seems like this might happen if verify peer isn't set */ |
| 4568 | if (!cert) |
| 4569 | return ok; |
| 4570 | |
| 4571 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4572 | if (alt_names) { |
| 4573 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4574 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4575 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4576 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4577 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4578 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4579 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4580 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4581 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4582 | OPENSSL_free(str); |
| 4583 | } |
| 4584 | } |
| 4585 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4586 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4587 | } |
| 4588 | |
| 4589 | cert_subject = X509_get_subject_name(cert); |
| 4590 | i = -1; |
| 4591 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4592 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4593 | ASN1_STRING *value; |
| 4594 | value = X509_NAME_ENTRY_get_data(entry); |
| 4595 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4596 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4597 | OPENSSL_free(str); |
| 4598 | } |
| 4599 | } |
| 4600 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4601 | /* report the mismatch and indicate if SNI was used or not */ |
| 4602 | if (!ok && !conn->err_code) |
| 4603 | 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] | 4604 | return ok; |
| 4605 | } |
| 4606 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4607 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4608 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4609 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4610 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4611 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4612 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4613 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4614 | SSL_OP_NO_SSLv2 | |
| 4615 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4616 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4617 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4618 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4619 | SSL_MODE_RELEASE_BUFFERS | |
| 4620 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4621 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4622 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4623 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4624 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4625 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4626 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4627 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4628 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4629 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4630 | cfgerr++; |
| 4631 | } |
| 4632 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4633 | /* Automatic memory computations need to know we use SSL there */ |
| 4634 | global.ssl_used_backend = 1; |
| 4635 | |
| 4636 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4637 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4638 | 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] | 4639 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4640 | curproxy->id, srv->id, |
| 4641 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4642 | cfgerr++; |
| 4643 | return cfgerr; |
| 4644 | } |
| 4645 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4646 | if (srv->use_ssl) |
| 4647 | srv->xprt = &ssl_sock; |
| 4648 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 4649 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4650 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4651 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4652 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4653 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4654 | proxy_type_str(curproxy), curproxy->id, |
| 4655 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4656 | cfgerr++; |
| 4657 | return cfgerr; |
| 4658 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4659 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4660 | 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] | 4661 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4662 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4663 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4664 | else |
| 4665 | flags = conf_ssl_methods->flags; |
| 4666 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4667 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4668 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4669 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4670 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4671 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4672 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4673 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4674 | min = max = CONF_TLSV_NONE; |
| 4675 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4676 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4677 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4678 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4679 | if (min) { |
| 4680 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4681 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4682 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4683 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4684 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4685 | hole = 0; |
| 4686 | } |
| 4687 | max = i; |
| 4688 | } |
| 4689 | else { |
| 4690 | min = max = i; |
| 4691 | } |
| 4692 | } |
| 4693 | else { |
| 4694 | if (min) |
| 4695 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4696 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4697 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4698 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4699 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4700 | cfgerr += 1; |
| 4701 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4702 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4703 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4704 | /* 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] | 4705 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4706 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4707 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4708 | else |
| 4709 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4710 | if (flags & methodVersions[i].flag) |
| 4711 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4712 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4713 | /* 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] | 4714 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4715 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4716 | #endif |
| 4717 | |
| 4718 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4719 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4720 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4721 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4722 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4723 | if (global_ssl.async) |
| 4724 | mode |= SSL_MODE_ASYNC; |
| 4725 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4726 | SSL_CTX_set_mode(ctx, mode); |
| 4727 | srv->ssl_ctx.ctx = ctx; |
| 4728 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4729 | if (srv->ssl_ctx.client_crt) { |
| 4730 | 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] | 4731 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4732 | proxy_type_str(curproxy), curproxy->id, |
| 4733 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4734 | cfgerr++; |
| 4735 | } |
| 4736 | 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] | 4737 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4738 | proxy_type_str(curproxy), curproxy->id, |
| 4739 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4740 | cfgerr++; |
| 4741 | } |
| 4742 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4743 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4744 | proxy_type_str(curproxy), curproxy->id, |
| 4745 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4746 | cfgerr++; |
| 4747 | } |
| 4748 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4749 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4750 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4751 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4752 | switch (srv->ssl_ctx.verify) { |
| 4753 | case SSL_SOCK_VERIFY_NONE: |
| 4754 | verify = SSL_VERIFY_NONE; |
| 4755 | break; |
| 4756 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4757 | verify = SSL_VERIFY_PEER; |
| 4758 | break; |
| 4759 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4760 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4761 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4762 | (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] | 4763 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4764 | if (srv->ssl_ctx.ca_file) { |
| 4765 | /* load CAfile to verify */ |
| 4766 | 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] | 4767 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
| 4768 | curproxy->id, srv->id, |
| 4769 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4770 | cfgerr++; |
| 4771 | } |
| 4772 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4773 | else { |
| 4774 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4775 | 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", |
| 4776 | curproxy->id, srv->id, |
| 4777 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4778 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4779 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4780 | curproxy->id, srv->id, |
| 4781 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4782 | cfgerr++; |
| 4783 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4784 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4785 | if (srv->ssl_ctx.crl_file) { |
| 4786 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4787 | |
| 4788 | 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] | 4789 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4790 | curproxy->id, srv->id, |
| 4791 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4792 | cfgerr++; |
| 4793 | } |
| 4794 | else { |
| 4795 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4796 | } |
| 4797 | } |
| 4798 | #endif |
| 4799 | } |
| 4800 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4801 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4802 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4803 | 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] | 4804 | if (srv->ssl_ctx.ciphers && |
| 4805 | !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] | 4806 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4807 | curproxy->id, srv->id, |
| 4808 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4809 | cfgerr++; |
| 4810 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4811 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 4812 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4813 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 4814 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4815 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 4816 | curproxy->id, srv->id, |
| 4817 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 4818 | cfgerr++; |
| 4819 | } |
| 4820 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4821 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 4822 | if (srv->ssl_ctx.npn_str) |
| 4823 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 4824 | #endif |
| 4825 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4826 | if (srv->ssl_ctx.alpn_str) |
| 4827 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 4828 | #endif |
| 4829 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4830 | |
| 4831 | return cfgerr; |
| 4832 | } |
| 4833 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4834 | /* 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] | 4835 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4836 | * encountered. |
| 4837 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4838 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4839 | { |
| 4840 | struct ebmb_node *node; |
| 4841 | struct sni_ctx *sni; |
| 4842 | int err = 0; |
| 4843 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4844 | /* Automatic memory computations need to know we use SSL there */ |
| 4845 | global.ssl_used_frontend = 1; |
| 4846 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4847 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4848 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4849 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4850 | err++; |
| 4851 | } |
| 4852 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4853 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4854 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4855 | /* It should not be necessary to call this function, but it's |
| 4856 | necessary first to check and move all initialisation related |
| 4857 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4858 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4859 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4860 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4861 | 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] | 4862 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4863 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4864 | while (node) { |
| 4865 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4866 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4867 | /* only initialize the CTX on its first occurrence and |
| 4868 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4869 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4870 | node = ebmb_next(node); |
| 4871 | } |
| 4872 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4873 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4874 | while (node) { |
| 4875 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4876 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4877 | /* only initialize the CTX on its first occurrence and |
| 4878 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4879 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4880 | node = ebmb_next(node); |
| 4881 | } |
| 4882 | return err; |
| 4883 | } |
| 4884 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4885 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4886 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4887 | * alerts are directly emitted since the rest of the stack does it below. |
| 4888 | */ |
| 4889 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4890 | { |
| 4891 | struct proxy *px = bind_conf->frontend; |
| 4892 | int alloc_ctx; |
| 4893 | int err; |
| 4894 | |
| 4895 | if (!bind_conf->is_ssl) { |
| 4896 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4897 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4898 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4899 | } |
| 4900 | return 0; |
| 4901 | } |
| 4902 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4903 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4904 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4905 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4906 | } |
| 4907 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4908 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4909 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4910 | return -1; |
| 4911 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4912 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4913 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4914 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 4915 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4916 | sizeof(*sh_ssl_sess_tree), |
| 4917 | ((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] | 4918 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4919 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4920 | 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"); |
| 4921 | else |
| 4922 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4923 | return -1; |
| 4924 | } |
| 4925 | /* free block callback */ |
| 4926 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4927 | /* init the root tree within the extra space */ |
| 4928 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4929 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4930 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4931 | err = 0; |
| 4932 | /* initialize all certificate contexts */ |
| 4933 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4934 | |
| 4935 | /* initialize CA variables if the certificates generation is enabled */ |
| 4936 | err += ssl_sock_load_ca(bind_conf); |
| 4937 | |
| 4938 | return -err; |
| 4939 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4940 | |
| 4941 | /* release ssl context allocated for servers. */ |
| 4942 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 4943 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4944 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4945 | if (srv->ssl_ctx.alpn_str) |
| 4946 | free(srv->ssl_ctx.alpn_str); |
| 4947 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 4948 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4949 | if (srv->ssl_ctx.npn_str) |
| 4950 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 4951 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 4952 | if (srv->ssl_ctx.ctx) |
| 4953 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 4954 | } |
| 4955 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4956 | /* 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] | 4957 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 4958 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4959 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4960 | { |
| 4961 | struct ebmb_node *node, *back; |
| 4962 | struct sni_ctx *sni; |
| 4963 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4964 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4965 | while (node) { |
| 4966 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4967 | back = ebmb_next(node); |
| 4968 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4969 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4970 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4971 | ssl_sock_free_ssl_conf(sni->conf); |
| 4972 | free(sni->conf); |
| 4973 | sni->conf = NULL; |
| 4974 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4975 | free(sni); |
| 4976 | node = back; |
| 4977 | } |
| 4978 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4979 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4980 | while (node) { |
| 4981 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 4982 | back = ebmb_next(node); |
| 4983 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4984 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4985 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4986 | ssl_sock_free_ssl_conf(sni->conf); |
| 4987 | free(sni->conf); |
| 4988 | sni->conf = NULL; |
| 4989 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4990 | free(sni); |
| 4991 | node = back; |
| 4992 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4993 | SSL_CTX_free(bind_conf->initial_ctx); |
| 4994 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4995 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4996 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 4997 | } |
| 4998 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 4999 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5000 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5001 | { |
| 5002 | ssl_sock_free_ca(bind_conf); |
| 5003 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5004 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5005 | free(bind_conf->ca_sign_file); |
| 5006 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5007 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5008 | free(bind_conf->keys_ref->filename); |
| 5009 | free(bind_conf->keys_ref->tlskeys); |
| 5010 | LIST_DEL(&bind_conf->keys_ref->list); |
| 5011 | free(bind_conf->keys_ref); |
| 5012 | } |
| 5013 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5014 | bind_conf->ca_sign_pass = NULL; |
| 5015 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5016 | } |
| 5017 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5018 | /* Load CA cert file and private key used to generate certificates */ |
| 5019 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5020 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5021 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5022 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5023 | FILE *fp; |
| 5024 | X509 *cacert = NULL; |
| 5025 | EVP_PKEY *capkey = NULL; |
| 5026 | int err = 0; |
| 5027 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 5028 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5029 | return err; |
| 5030 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5031 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5032 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5033 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5034 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5035 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5036 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5037 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5038 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5039 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5040 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5041 | "no CA certificate File configured at [%s:%d].\n", |
| 5042 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5043 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5044 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5045 | |
| 5046 | /* read in the CA certificate */ |
| 5047 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5048 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5049 | 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] | 5050 | goto load_error; |
| 5051 | } |
| 5052 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5053 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5054 | 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] | 5055 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5056 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5057 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5058 | 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] | 5059 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 5060 | 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] | 5061 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5062 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5063 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5064 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5065 | bind_conf->ca_sign_cert = cacert; |
| 5066 | bind_conf->ca_sign_pkey = capkey; |
| 5067 | return err; |
| 5068 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5069 | read_error: |
| 5070 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5071 | if (capkey) EVP_PKEY_free(capkey); |
| 5072 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5073 | load_error: |
| 5074 | bind_conf->generate_certs = 0; |
| 5075 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5076 | return err; |
| 5077 | } |
| 5078 | |
| 5079 | /* Release CA cert and private key used to generate certificated */ |
| 5080 | void |
| 5081 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5082 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5083 | if (bind_conf->ca_sign_pkey) |
| 5084 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 5085 | if (bind_conf->ca_sign_cert) |
| 5086 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 5087 | bind_conf->ca_sign_pkey = NULL; |
| 5088 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5089 | } |
| 5090 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5091 | /* |
| 5092 | * This function is called if SSL * context is not yet allocated. The function |
| 5093 | * is designed to be called before any other data-layer operation and sets the |
| 5094 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5095 | * It returns 0 on success and -1 in error case. |
| 5096 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5097 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5098 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5099 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5100 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5101 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5102 | return 0; |
| 5103 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5104 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5105 | return 0; |
| 5106 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5107 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 5108 | if (!ctx) { |
| 5109 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5110 | return -1; |
| 5111 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5112 | ctx->wait_event.tasklet = tasklet_new(); |
| 5113 | if (!ctx->wait_event.tasklet) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5114 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5115 | pool_free(ssl_sock_ctx_pool, ctx); |
| 5116 | return -1; |
| 5117 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5118 | ctx->wait_event.tasklet->process = ssl_sock_io_cb; |
| 5119 | ctx->wait_event.tasklet->context = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5120 | ctx->wait_event.events = 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5121 | ctx->sent_early_data = 0; |
| 5122 | ctx->tmp_early_data = -1; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5123 | ctx->conn = conn; |
Olivier Houchard | 81284e6 | 2019-06-06 13:21:23 +0200 | [diff] [blame] | 5124 | ctx->send_wait = NULL; |
| 5125 | ctx->recv_wait = NULL; |
Emeric Brun | 5762a0d | 2019-09-06 15:36:02 +0200 | [diff] [blame] | 5126 | ctx->xprt_st = 0; |
| 5127 | ctx->xprt_ctx = NULL; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5128 | |
| 5129 | /* Only work with sockets for now, this should be adapted when we'll |
| 5130 | * add QUIC support. |
| 5131 | */ |
| 5132 | ctx->xprt = xprt_get(XPRT_RAW); |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5133 | if (ctx->xprt->init) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5134 | if (ctx->xprt->init(conn, &ctx->xprt_ctx) != 0) |
| 5135 | goto err; |
Olivier Houchard | 19afb27 | 2019-05-23 18:24:07 +0200 | [diff] [blame] | 5136 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5137 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5138 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 5139 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5140 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5141 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5142 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5143 | /* If it is in client mode initiate SSL session |
| 5144 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5145 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5146 | int may_retry = 1; |
| 5147 | |
| 5148 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5149 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5150 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 5151 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5152 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5153 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5154 | goto retry_connect; |
| 5155 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5156 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5157 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5158 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5159 | ctx->bio = BIO_new(ha_meth); |
| 5160 | if (!ctx->bio) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5161 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5162 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5163 | goto retry_connect; |
| 5164 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5165 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5166 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5167 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5168 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5169 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5170 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5171 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5172 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5173 | SSL_free(ctx->ssl); |
| 5174 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5175 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5176 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5177 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5178 | goto retry_connect; |
| 5179 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5180 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5181 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5182 | } |
| 5183 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5184 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5185 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5186 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 5187 | 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] | 5188 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5189 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5190 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5191 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5192 | } else if (sess) { |
| 5193 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5194 | } |
| 5195 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5196 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5197 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5198 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5199 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5200 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5201 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5202 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5203 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5204 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5205 | if (conn->flags & CO_FL_ERROR) |
| 5206 | goto err; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5207 | return 0; |
| 5208 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5209 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5210 | int may_retry = 1; |
| 5211 | |
| 5212 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5213 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5214 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 5215 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5216 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5217 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5218 | goto retry_accept; |
| 5219 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5220 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5221 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5222 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5223 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5224 | ctx->bio = BIO_new(ha_meth); |
| 5225 | if (!ctx->bio) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5226 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5227 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5228 | goto retry_accept; |
| 5229 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5230 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5231 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5232 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5233 | BIO_set_data(ctx->bio, ctx); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5234 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5235 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5236 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5237 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5238 | SSL_free(ctx->ssl); |
| 5239 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5240 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5241 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5242 | goto retry_accept; |
| 5243 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5244 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5245 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5246 | } |
| 5247 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5248 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5249 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5250 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5251 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5252 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5253 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5254 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5255 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5256 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5257 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5258 | *xprt_ctx = ctx; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5259 | /* Start the handshake */ |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5260 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5261 | if (conn->flags & CO_FL_ERROR) |
| 5262 | goto err; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5263 | return 0; |
| 5264 | } |
| 5265 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5266 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5267 | err: |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5268 | if (ctx && ctx->wait_event.tasklet) |
| 5269 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5270 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5271 | return -1; |
| 5272 | } |
| 5273 | |
| 5274 | |
| 5275 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5276 | * updates the FD status if it wants some polling before being called again. |
| 5277 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5278 | * otherwise it returns non-zero and removes itself from the connection's |
| 5279 | * flags (the bit is provided in <flag> by the caller). |
| 5280 | */ |
Olivier Houchard | 000694c | 2019-05-23 14:45:12 +0200 | [diff] [blame] | 5281 | static int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5282 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5283 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5284 | int ret; |
| 5285 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5286 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5287 | return 0; |
| 5288 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5289 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5290 | goto out_error; |
| 5291 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5292 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5293 | /* |
| 5294 | * Check if we have early data. If we do, we have to read them |
| 5295 | * before SSL_do_handshake() is called, And there's no way to |
| 5296 | * detect early data, except to try to read them |
| 5297 | */ |
| 5298 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5299 | size_t read_data; |
| 5300 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5301 | ret = SSL_read_early_data(ctx->ssl, &ctx->tmp_early_data, |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5302 | 1, &read_data); |
| 5303 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5304 | goto check_error; |
| 5305 | if (ret == SSL_READ_EARLY_DATA_SUCCESS) { |
| 5306 | conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN); |
| 5307 | return 1; |
| 5308 | } else |
| 5309 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5310 | } |
| 5311 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5312 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5313 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5314 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5315 | * the reneg handshake. |
| 5316 | * Here we use SSL_peek as a workaround for reneg. |
| 5317 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5318 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5319 | char c; |
| 5320 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5321 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5322 | if (ret <= 0) { |
| 5323 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5324 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5325 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5326 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5327 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5328 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5329 | 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] | 5330 | return 0; |
| 5331 | } |
| 5332 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5333 | /* handshake may have been completed but we have |
| 5334 | * no more data to read. |
| 5335 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5336 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5337 | ret = 1; |
| 5338 | goto reneg_ok; |
| 5339 | } |
| 5340 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5341 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5342 | 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] | 5343 | return 0; |
| 5344 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5345 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5346 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5347 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5348 | return 0; |
| 5349 | } |
| 5350 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5351 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5352 | /* if errno is null, then connection was successfully established */ |
| 5353 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5354 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5355 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5356 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5357 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5358 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5359 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5360 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5361 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5362 | /* 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] | 5363 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5364 | empty_handshake = state == TLS_ST_BEFORE; |
| 5365 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5366 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5367 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5368 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5369 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5370 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5371 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5372 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5373 | else |
| 5374 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5375 | } |
| 5376 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5377 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5378 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5379 | else |
| 5380 | conn->err_code = CO_ER_SSL_ABORT; |
| 5381 | } |
| 5382 | } |
| 5383 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5384 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5385 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5386 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5387 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5388 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5389 | #endif /* BoringSSL or LibreSSL */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5390 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5391 | goto out_error; |
| 5392 | } |
| 5393 | else { |
| 5394 | /* Fail on all other handshake errors */ |
| 5395 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5396 | * buffer, causing an RST to be emitted upon close() on |
| 5397 | * TCP sockets. We first try to drain possibly pending |
| 5398 | * data to avoid this as much as possible. |
| 5399 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5400 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5401 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5402 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5403 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5404 | goto out_error; |
| 5405 | } |
| 5406 | } |
| 5407 | /* read some data: consider handshake completed */ |
| 5408 | goto reneg_ok; |
| 5409 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5410 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5411 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5412 | if (ret != 1) { |
| 5413 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5414 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5415 | |
| 5416 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5417 | /* SSL handshake needs to write, L4 connection may not be ready */ |
Olivier Houchard | 03abf2d | 2019-05-28 10:12:02 +0200 | [diff] [blame] | 5418 | if (!(ctx->wait_event.events & SUB_RETRY_SEND)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5419 | 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] | 5420 | return 0; |
| 5421 | } |
| 5422 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5423 | /* SSL handshake needs to read, L4 connection is ready */ |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5424 | if (!(ctx->wait_event.events & SUB_RETRY_RECV)) |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5425 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5426 | SUB_RETRY_RECV, &ctx->wait_event); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5427 | return 0; |
| 5428 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5429 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5430 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5431 | ssl_async_process_fds(ctx); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5432 | return 0; |
| 5433 | } |
| 5434 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5435 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5436 | /* if errno is null, then connection was successfully established */ |
| 5437 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5438 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5439 | if (!conn->err_code) { |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5440 | #if defined(OPENSSL_IS_BORINGSSL) || defined(LIBRESSL_VERSION_NUMBER) |
| 5441 | /* do not handle empty handshakes in BoringSSL or LibreSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5442 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5443 | #else |
| 5444 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5445 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5446 | /* 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] | 5447 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5448 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5449 | #else |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5450 | /* access packet_length directly in OpenSSL <= 1.0.2; SSL_state() is broken */ |
| 5451 | empty_handshake = !ctx->ssl->packet_length; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5452 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5453 | if (empty_handshake) { |
| 5454 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5455 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5456 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5457 | else |
| 5458 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5459 | } |
| 5460 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5461 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5462 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5463 | else |
| 5464 | conn->err_code = CO_ER_SSL_ABORT; |
| 5465 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5466 | } |
| 5467 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5468 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5469 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5470 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5471 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5472 | } |
Lukas Tribus | 4979916 | 2019-07-08 14:29:15 +0200 | [diff] [blame] | 5473 | #endif /* BoringSSL or LibreSSL */ |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5474 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5475 | goto out_error; |
| 5476 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5477 | else { |
| 5478 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5479 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5480 | * buffer, causing an RST to be emitted upon close() on |
| 5481 | * TCP sockets. We first try to drain possibly pending |
| 5482 | * data to avoid this as much as possible. |
| 5483 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5484 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5485 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5486 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5487 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5488 | goto out_error; |
| 5489 | } |
| 5490 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5491 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5492 | else { |
| 5493 | /* |
| 5494 | * If the server refused the early data, we have to send a |
| 5495 | * 425 to the client, as we no longer have the data to sent |
| 5496 | * them again. |
| 5497 | */ |
| 5498 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5499 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5500 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5501 | goto out_error; |
| 5502 | } |
| 5503 | } |
| 5504 | } |
| 5505 | #endif |
| 5506 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5507 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5508 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5509 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5510 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5511 | /* ASYNC engine API doesn't support moving read/write |
| 5512 | * buffers. So we disable ASYNC mode right after |
| 5513 | * the handshake to avoid buffer oveflows. |
| 5514 | */ |
| 5515 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5516 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5517 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5518 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5519 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5520 | if (objt_server(conn->target)) { |
| 5521 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5522 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5523 | 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] | 5524 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5525 | else { |
| 5526 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5527 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5528 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5529 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5530 | } |
| 5531 | |
| 5532 | /* The connection is now established at both layers, it's time to leave */ |
| 5533 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5534 | return 1; |
| 5535 | |
| 5536 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5537 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5538 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5539 | ERR_clear_error(); |
| 5540 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5541 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5542 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5543 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5544 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5545 | } |
| 5546 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5547 | /* Fail on all other handshake errors */ |
| 5548 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5549 | if (!conn->err_code) |
| 5550 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5551 | return 0; |
| 5552 | } |
| 5553 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5554 | 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] | 5555 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5556 | struct wait_event *sw; |
| 5557 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5558 | |
Olivier Houchard | 0ff2865 | 2019-06-24 18:57:39 +0200 | [diff] [blame] | 5559 | if (!ctx) |
| 5560 | return -1; |
| 5561 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5562 | if (event_type & SUB_RETRY_RECV) { |
| 5563 | sw = param; |
| 5564 | BUG_ON(ctx->recv_wait != NULL || (sw->events & SUB_RETRY_RECV)); |
| 5565 | sw->events |= SUB_RETRY_RECV; |
| 5566 | ctx->recv_wait = sw; |
| 5567 | if (!(conn->flags & CO_FL_SSL_WAIT_HS) && |
| 5568 | !(ctx->wait_event.events & SUB_RETRY_RECV)) |
| 5569 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, &ctx->wait_event); |
| 5570 | event_type &= ~SUB_RETRY_RECV; |
| 5571 | } |
| 5572 | if (event_type & SUB_RETRY_SEND) { |
| 5573 | sw = param; |
| 5574 | BUG_ON(ctx->send_wait != NULL || (sw->events & SUB_RETRY_SEND)); |
| 5575 | sw->events |= SUB_RETRY_SEND; |
| 5576 | ctx->send_wait = sw; |
| 5577 | if (!(conn->flags & CO_FL_SSL_WAIT_HS) && |
| 5578 | !(ctx->wait_event.events & SUB_RETRY_SEND)) |
| 5579 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
| 5580 | event_type &= ~SUB_RETRY_SEND; |
| 5581 | |
| 5582 | } |
| 5583 | if (event_type != 0) |
| 5584 | return -1; |
| 5585 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5586 | } |
| 5587 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5588 | 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] | 5589 | { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5590 | struct wait_event *sw; |
| 5591 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5592 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5593 | if (event_type & SUB_RETRY_RECV) { |
| 5594 | sw = param; |
| 5595 | BUG_ON(ctx->recv_wait != sw); |
| 5596 | ctx->recv_wait = NULL; |
| 5597 | sw->events &= ~SUB_RETRY_RECV; |
| 5598 | /* If we subscribed, and we're not doing the handshake, |
| 5599 | * then we subscribed because the upper layer asked for it, |
| 5600 | * as the upper layer is no longer interested, we can |
| 5601 | * unsubscribe too. |
| 5602 | */ |
| 5603 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) && |
| 5604 | (ctx->wait_event.events & SUB_RETRY_RECV)) |
| 5605 | conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_RECV, |
| 5606 | &ctx->wait_event); |
| 5607 | } |
| 5608 | if (event_type & SUB_RETRY_SEND) { |
| 5609 | sw = param; |
| 5610 | BUG_ON(ctx->send_wait != sw); |
| 5611 | ctx->send_wait = NULL; |
| 5612 | sw->events &= ~SUB_RETRY_SEND; |
| 5613 | if (!(ctx->conn->flags & CO_FL_SSL_WAIT_HS) && |
| 5614 | (ctx->wait_event.events & SUB_RETRY_SEND)) |
| 5615 | conn_unsubscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, |
| 5616 | &ctx->wait_event); |
| 5617 | |
| 5618 | } |
| 5619 | |
| 5620 | return 0; |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5621 | } |
| 5622 | |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 5623 | /* Use the provided XPRT as an underlying XPRT, and provide the old one. |
| 5624 | * Returns 0 on success, and non-zero on failure. |
| 5625 | */ |
| 5626 | 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) |
| 5627 | { |
| 5628 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5629 | |
| 5630 | if (oldxprt_ops != NULL) |
| 5631 | *oldxprt_ops = ctx->xprt; |
| 5632 | if (oldxprt_ctx != NULL) |
| 5633 | *oldxprt_ctx = ctx->xprt_ctx; |
| 5634 | ctx->xprt = toadd_ops; |
| 5635 | ctx->xprt_ctx = toadd_ctx; |
| 5636 | return 0; |
| 5637 | } |
| 5638 | |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 5639 | /* Remove the specified xprt. If if it our underlying XPRT, remove it and |
| 5640 | * return 0, otherwise just call the remove_xprt method from the underlying |
| 5641 | * XPRT. |
| 5642 | */ |
| 5643 | static int ssl_remove_xprt(struct connection *conn, void *xprt_ctx, void *toremove_ctx, const struct xprt_ops *newops, void *newctx) |
| 5644 | { |
| 5645 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 5646 | |
| 5647 | if (ctx->xprt_ctx == toremove_ctx) { |
| 5648 | ctx->xprt_ctx = newctx; |
| 5649 | ctx->xprt = newops; |
| 5650 | return 0; |
| 5651 | } |
| 5652 | return (ctx->xprt->remove_xprt(conn, ctx->xprt_ctx, toremove_ctx, newops, newctx)); |
| 5653 | } |
| 5654 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5655 | static struct task *ssl_sock_io_cb(struct task *t, void *context, unsigned short state) |
| 5656 | { |
| 5657 | struct ssl_sock_ctx *ctx = context; |
| 5658 | |
| 5659 | /* First if we're doing an handshake, try that */ |
| 5660 | if (ctx->conn->flags & CO_FL_SSL_WAIT_HS) |
| 5661 | ssl_sock_handshake(ctx->conn, CO_FL_SSL_WAIT_HS); |
| 5662 | /* If we had an error, or the handshake is done and I/O is available, |
| 5663 | * let the upper layer know. |
| 5664 | * If no mux was set up yet, and nobody subscribed, then call |
| 5665 | * xprt_done_cb() ourself if it's set, or destroy the connection, |
| 5666 | * we can't be sure conn_fd_handler() will be called again. |
| 5667 | */ |
| 5668 | if ((ctx->conn->flags & CO_FL_ERROR) || |
| 5669 | !(ctx->conn->flags & CO_FL_SSL_WAIT_HS)) { |
| 5670 | int ret = 0; |
| 5671 | int woke = 0; |
| 5672 | |
| 5673 | /* On error, wake any waiter */ |
| 5674 | if (ctx->recv_wait) { |
| 5675 | ctx->recv_wait->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5676 | tasklet_wakeup(ctx->recv_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5677 | ctx->recv_wait = NULL; |
| 5678 | woke = 1; |
| 5679 | } |
| 5680 | if (ctx->send_wait) { |
| 5681 | ctx->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 5682 | tasklet_wakeup(ctx->send_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5683 | ctx->send_wait = NULL; |
| 5684 | woke = 1; |
| 5685 | } |
| 5686 | /* If we're the first xprt for the connection, let the |
| 5687 | * upper layers know. If xprt_done_cb() is set, call it, |
| 5688 | * otherwise, we should have a mux, so call its wake |
| 5689 | * method if we didn't woke a tasklet already. |
| 5690 | */ |
| 5691 | if (ctx->conn->xprt_ctx == ctx) { |
| 5692 | if (ctx->conn->xprt_done_cb) |
| 5693 | ret = ctx->conn->xprt_done_cb(ctx->conn); |
| 5694 | if (ret >= 0 && !woke && ctx->conn->mux && ctx->conn->mux->wake) |
| 5695 | ctx->conn->mux->wake(ctx->conn); |
| 5696 | return NULL; |
| 5697 | } |
| 5698 | } |
| 5699 | return NULL; |
| 5700 | } |
| 5701 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5702 | /* 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] | 5703 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5704 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5705 | * flags are updated with whatever special event is detected (error, read0, |
| 5706 | * empty). The caller is responsible for taking care of those events and |
| 5707 | * avoiding the call if inappropriate. The function does not call the |
| 5708 | * connection's polling update function, so the caller is responsible for this. |
| 5709 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5710 | 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] | 5711 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5712 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5713 | ssize_t ret; |
| 5714 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5715 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5716 | conn_refresh_polling_flags(conn); |
| 5717 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5718 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5719 | goto out_error; |
| 5720 | |
| 5721 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5722 | /* a handshake was requested */ |
| 5723 | return 0; |
| 5724 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5725 | /* read the largest possible block. For this, we perform only one call |
| 5726 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5727 | * in which case we accept to do it once again. A new attempt is made on |
| 5728 | * EINTR too. |
| 5729 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5730 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5731 | int need_out = 0; |
| 5732 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5733 | try = b_contig_space(buf); |
| 5734 | if (!try) |
| 5735 | break; |
| 5736 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5737 | if (try > count) |
| 5738 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5739 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5740 | 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] | 5741 | ctx->tmp_early_data != -1) { |
| 5742 | *b_tail(buf) = ctx->tmp_early_data; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5743 | done++; |
| 5744 | try--; |
| 5745 | count--; |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5746 | b_add(buf, 1); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5747 | ctx->tmp_early_data = -1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5748 | continue; |
| 5749 | } |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5750 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5751 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5752 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5753 | size_t read_length; |
| 5754 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5755 | ret = SSL_read_early_data(ctx->ssl, |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 5756 | b_tail(buf), try, &read_length); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5757 | if (ret == SSL_READ_EARLY_DATA_SUCCESS && |
| 5758 | read_length > 0) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5759 | conn->flags |= CO_FL_EARLY_DATA; |
| 5760 | if (ret == SSL_READ_EARLY_DATA_SUCCESS || |
| 5761 | ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5762 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5763 | /* |
| 5764 | * We're done reading the early data, |
| 5765 | * let's make the handshake |
| 5766 | */ |
| 5767 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5768 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5769 | need_out = 1; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5770 | /* Now initiate the handshake */ |
| 5771 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5772 | if (read_length == 0) |
| 5773 | break; |
| 5774 | } |
| 5775 | ret = read_length; |
| 5776 | } |
| 5777 | } else |
| 5778 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5779 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | f967c31 | 2019-08-05 18:04:16 +0200 | [diff] [blame] | 5780 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5781 | if (conn->flags & CO_FL_ERROR) { |
| 5782 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5783 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5784 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5785 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5786 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5787 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5788 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5789 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5790 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5791 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5792 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5793 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5794 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5795 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, SUB_RETRY_SEND, &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5796 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5797 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5798 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5799 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5800 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5801 | break; |
| 5802 | } |
| 5803 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5804 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5805 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5806 | SUB_RETRY_RECV, |
| 5807 | &ctx->wait_event); |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5808 | /* handshake is running, and it may need to re-enable read */ |
| 5809 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5810 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5811 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5812 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5813 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5814 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5815 | break; |
| 5816 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5817 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5818 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5819 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5820 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5821 | * stack before shutting down the connection for |
| 5822 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5823 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5824 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5825 | /* otherwise it's a real error */ |
| 5826 | goto out_error; |
| 5827 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5828 | if (need_out) |
| 5829 | break; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5830 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5831 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5832 | return done; |
| 5833 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5834 | clear_ssl_error: |
| 5835 | /* Clear openssl global errors stack */ |
| 5836 | ssl_sock_dump_errors(conn); |
| 5837 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5838 | read0: |
| 5839 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5840 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5841 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5842 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5843 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5844 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5845 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5846 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5847 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5848 | } |
| 5849 | |
| 5850 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5851 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5852 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5853 | * 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] | 5854 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5855 | * a second call may be performed. The connection's flags are updated with |
| 5856 | * whatever special event is detected (error, empty). The caller is responsible |
| 5857 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5858 | * 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] | 5859 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5860 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5861 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5862 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5863 | 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] | 5864 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5865 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5866 | ssize_t ret; |
| 5867 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5868 | |
| 5869 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5870 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5871 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5872 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5873 | goto out_error; |
| 5874 | |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5875 | if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5876 | /* a handshake was requested */ |
| 5877 | return 0; |
| 5878 | |
| 5879 | /* send the largest possible block. For this we perform only one call |
| 5880 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5881 | * in which case we accept to do it once again. |
| 5882 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5883 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5884 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5885 | size_t written_data; |
| 5886 | #endif |
| 5887 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5888 | try = b_contig_data(buf, done); |
| 5889 | if (try > count) |
| 5890 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5891 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5892 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5893 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5894 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5895 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5896 | } |
| 5897 | else { |
| 5898 | /* we need to keep the information about the fact that |
| 5899 | * we're not limiting the upcoming send(), because if it |
| 5900 | * fails, we'll have to retry with at least as many data. |
| 5901 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5902 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5903 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5904 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5905 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5906 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5907 | unsigned int max_early; |
| 5908 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5909 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5910 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5911 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5912 | if (SSL_get0_session(ctx->ssl)) |
| 5913 | 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] | 5914 | else |
| 5915 | max_early = 0; |
| 5916 | } |
| 5917 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5918 | if (try + ctx->sent_early_data > max_early) { |
| 5919 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5920 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5921 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5922 | tasklet_wakeup(ctx->wait_event.tasklet); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5923 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5924 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5925 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5926 | 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] | 5927 | if (ret == 1) { |
| 5928 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5929 | ctx->sent_early_data += ret; |
Olivier Houchard | 965e84e | 2019-06-15 20:59:30 +0200 | [diff] [blame] | 5930 | if (objt_server(conn->target)) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5931 | 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] | 5932 | /* Initiate the handshake, now */ |
| 5933 | tasklet_wakeup(ctx->wait_event.tasklet); |
| 5934 | } |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5935 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5936 | } |
| 5937 | |
| 5938 | } else |
| 5939 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5940 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5941 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5942 | if (conn->flags & CO_FL_ERROR) { |
| 5943 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5944 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5945 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5946 | if (ret > 0) { |
Olivier Houchard | f24502b | 2019-01-17 19:09:11 +0100 | [diff] [blame] | 5947 | /* A send succeeded, so we can consier ourself connected */ |
| 5948 | conn->flags |= CO_FL_CONNECTED; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5949 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5950 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5951 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5952 | } |
| 5953 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5954 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5955 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5956 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5957 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5958 | /* handshake is running, and it may need to re-enable write */ |
| 5959 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5960 | 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] | 5961 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5962 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5963 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5964 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5965 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5966 | break; |
| 5967 | } |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5968 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5969 | break; |
| 5970 | } |
| 5971 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5972 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5973 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 5974 | ctx->xprt->subscribe(conn, ctx->xprt_ctx, |
| 5975 | SUB_RETRY_RECV, |
| 5976 | &ctx->wait_event); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5977 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5978 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5979 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5980 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5981 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5982 | break; |
| 5983 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5984 | goto out_error; |
| 5985 | } |
| 5986 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5987 | leave: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5988 | return done; |
| 5989 | |
| 5990 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5991 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5992 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5993 | ERR_clear_error(); |
| 5994 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5995 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5996 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5997 | } |
| 5998 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5999 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6000 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6001 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6002 | |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6003 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6004 | if (ctx) { |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6005 | if (ctx->wait_event.events != 0) |
| 6006 | ctx->xprt->unsubscribe(ctx->conn, ctx->xprt_ctx, |
| 6007 | ctx->wait_event.events, |
| 6008 | &ctx->wait_event); |
| 6009 | if (ctx->send_wait) { |
| 6010 | ctx->send_wait->events &= ~SUB_RETRY_SEND; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6011 | tasklet_wakeup(ctx->send_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6012 | } |
| 6013 | if (ctx->recv_wait) { |
| 6014 | ctx->recv_wait->events &= ~SUB_RETRY_RECV; |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6015 | tasklet_wakeup(ctx->recv_wait->tasklet); |
Olivier Houchard | ea8dd94 | 2019-05-20 14:02:16 +0200 | [diff] [blame] | 6016 | } |
Olivier Houchard | 692c1d0 | 2019-05-23 18:41:47 +0200 | [diff] [blame] | 6017 | if (ctx->xprt->close) |
| 6018 | ctx->xprt->close(conn, ctx->xprt_ctx); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 6019 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6020 | if (global_ssl.async) { |
| 6021 | OSSL_ASYNC_FD all_fd[32], afd; |
| 6022 | size_t num_all_fds = 0; |
| 6023 | int i; |
| 6024 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6025 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6026 | if (num_all_fds > 32) { |
| 6027 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 6028 | return; |
| 6029 | } |
| 6030 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6031 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6032 | |
| 6033 | /* If an async job is pending, we must try to |
| 6034 | to catch the end using polling before calling |
| 6035 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6036 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6037 | for (i=0 ; i < num_all_fds ; i++) { |
| 6038 | /* switch on an handler designed to |
| 6039 | * handle the SSL_free |
| 6040 | */ |
| 6041 | afd = all_fd[i]; |
| 6042 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6043 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6044 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 6045 | /* To ensure that the fd cache won't be used |
| 6046 | * and we'll catch a real RD event. |
| 6047 | */ |
| 6048 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6049 | } |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6050 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6051 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6052 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6053 | return; |
| 6054 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 6055 | /* Else we can remove the fds from the fdtab |
| 6056 | * and call SSL_free. |
| 6057 | * note: we do a fd_remove and not a delete |
| 6058 | * because the fd is owned by the engine. |
| 6059 | * the engine is responsible to close |
| 6060 | */ |
| 6061 | for (i=0 ; i < num_all_fds ; i++) |
| 6062 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 6063 | } |
| 6064 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6065 | SSL_free(ctx->ssl); |
Willy Tarreau | 3c39a7d | 2019-06-14 14:42:29 +0200 | [diff] [blame] | 6066 | tasklet_free(ctx->wait_event.tasklet); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6067 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 6068 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6069 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6070 | } |
| 6071 | |
| 6072 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 6073 | * any case, flags the connection as reusable if no handshake was in progress. |
| 6074 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6075 | 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] | 6076 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6077 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6078 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6079 | if (conn->flags & CO_FL_HANDSHAKE) |
| 6080 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 6081 | if (!clean) |
| 6082 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6083 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6084 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6085 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6086 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 6087 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 6088 | ERR_clear_error(); |
| 6089 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 6090 | } |
| 6091 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6092 | /* used for ppv2 pkey alog (can be used for logging) */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6093 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6094 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6095 | struct ssl_sock_ctx *ctx; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6096 | struct pkey_info *pkinfo; |
| 6097 | int bits = 0; |
| 6098 | int sig = TLSEXT_signature_anonymous; |
| 6099 | int len = -1; |
| 6100 | |
| 6101 | if (!ssl_sock_is_ssl(conn)) |
| 6102 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6103 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6104 | 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] | 6105 | if (pkinfo) { |
| 6106 | sig = pkinfo->sig; |
| 6107 | bits = pkinfo->bits; |
| 6108 | } else { |
| 6109 | /* multicert and generated cert have no pkey info */ |
| 6110 | X509 *crt; |
| 6111 | EVP_PKEY *pkey; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6112 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6113 | if (!crt) |
| 6114 | return 0; |
| 6115 | pkey = X509_get_pubkey(crt); |
| 6116 | if (pkey) { |
| 6117 | bits = EVP_PKEY_bits(pkey); |
| 6118 | switch(EVP_PKEY_base_id(pkey)) { |
| 6119 | case EVP_PKEY_RSA: |
| 6120 | sig = TLSEXT_signature_rsa; |
| 6121 | break; |
| 6122 | case EVP_PKEY_EC: |
| 6123 | sig = TLSEXT_signature_ecdsa; |
| 6124 | break; |
| 6125 | case EVP_PKEY_DSA: |
| 6126 | sig = TLSEXT_signature_dsa; |
| 6127 | break; |
| 6128 | } |
| 6129 | EVP_PKEY_free(pkey); |
| 6130 | } |
| 6131 | } |
| 6132 | |
| 6133 | switch(sig) { |
| 6134 | case TLSEXT_signature_rsa: |
| 6135 | len = chunk_printf(out, "RSA%d", bits); |
| 6136 | break; |
| 6137 | case TLSEXT_signature_ecdsa: |
| 6138 | len = chunk_printf(out, "EC%d", bits); |
| 6139 | break; |
| 6140 | case TLSEXT_signature_dsa: |
| 6141 | len = chunk_printf(out, "DSA%d", bits); |
| 6142 | break; |
| 6143 | default: |
| 6144 | return 0; |
| 6145 | } |
| 6146 | if (len < 0) |
| 6147 | return 0; |
| 6148 | return 1; |
| 6149 | } |
| 6150 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6151 | /* used for ppv2 cert signature (can be used for logging) */ |
| 6152 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 6153 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6154 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6155 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6156 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 6157 | X509 *crt; |
| 6158 | |
| 6159 | if (!ssl_sock_is_ssl(conn)) |
| 6160 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6161 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6162 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6163 | if (!crt) |
| 6164 | return NULL; |
| 6165 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6166 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6167 | } |
| 6168 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6169 | /* used for ppv2 authority */ |
| 6170 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6171 | { |
| 6172 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6173 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6174 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6175 | if (!ssl_sock_is_ssl(conn)) |
| 6176 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6177 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6178 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6179 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6180 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6181 | #endif |
| 6182 | } |
| 6183 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6184 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6185 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6186 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6187 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6188 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6189 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6190 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6191 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6192 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6193 | } |
| 6194 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6195 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6196 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 6197 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6198 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6199 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6200 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6201 | return NULL; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6202 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6203 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6204 | } |
| 6205 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6206 | /* Extract a serial from a cert, and copy it to a chunk. |
| 6207 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 6208 | * -1 if output is not large enough. |
| 6209 | */ |
| 6210 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6211 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6212 | { |
| 6213 | ASN1_INTEGER *serial; |
| 6214 | |
| 6215 | serial = X509_get_serialNumber(crt); |
| 6216 | if (!serial) |
| 6217 | return 0; |
| 6218 | |
| 6219 | if (out->size < serial->length) |
| 6220 | return -1; |
| 6221 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6222 | memcpy(out->area, serial->data, serial->length); |
| 6223 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6224 | return 1; |
| 6225 | } |
| 6226 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6227 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 6228 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 6229 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6230 | */ |
| 6231 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6232 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6233 | { |
| 6234 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6235 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6236 | |
| 6237 | len =i2d_X509(crt, NULL); |
| 6238 | if (len <= 0) |
| 6239 | return 1; |
| 6240 | |
| 6241 | if (out->size < len) |
| 6242 | return -1; |
| 6243 | |
| 6244 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6245 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6246 | return 1; |
| 6247 | } |
| 6248 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6249 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6250 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6251 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 6252 | * and -1 if output is not large enough. |
| 6253 | */ |
| 6254 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6255 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6256 | { |
| 6257 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 6258 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 6259 | |
| 6260 | if (gentm->length < 12) |
| 6261 | return 0; |
| 6262 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 6263 | return 0; |
| 6264 | if (out->size < gentm->length-2) |
| 6265 | return -1; |
| 6266 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6267 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 6268 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6269 | return 1; |
| 6270 | } |
| 6271 | else if (tm->type == V_ASN1_UTCTIME) { |
| 6272 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 6273 | |
| 6274 | if (utctm->length < 10) |
| 6275 | return 0; |
| 6276 | if (utctm->data[0] >= 0x35) |
| 6277 | return 0; |
| 6278 | if (out->size < utctm->length) |
| 6279 | return -1; |
| 6280 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6281 | memcpy(out->area, utctm->data, utctm->length); |
| 6282 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6283 | return 1; |
| 6284 | } |
| 6285 | |
| 6286 | return 0; |
| 6287 | } |
| 6288 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6289 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 6290 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 6291 | */ |
| 6292 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6293 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 6294 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6295 | { |
| 6296 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6297 | ASN1_OBJECT *obj; |
| 6298 | ASN1_STRING *data; |
| 6299 | const unsigned char *data_ptr; |
| 6300 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6301 | int i, j, n; |
| 6302 | int cur = 0; |
| 6303 | const char *s; |
| 6304 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6305 | int name_count; |
| 6306 | |
| 6307 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6308 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6309 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6310 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6311 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6312 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6313 | else |
| 6314 | j = i; |
| 6315 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6316 | ne = X509_NAME_get_entry(a, j); |
| 6317 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6318 | data = X509_NAME_ENTRY_get_data(ne); |
| 6319 | data_ptr = ASN1_STRING_get0_data(data); |
| 6320 | data_len = ASN1_STRING_length(data); |
| 6321 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6322 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6323 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6324 | s = tmp; |
| 6325 | } |
| 6326 | |
| 6327 | if (chunk_strcasecmp(entry, s) != 0) |
| 6328 | continue; |
| 6329 | |
| 6330 | if (pos < 0) |
| 6331 | cur--; |
| 6332 | else |
| 6333 | cur++; |
| 6334 | |
| 6335 | if (cur != pos) |
| 6336 | continue; |
| 6337 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6338 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6339 | return -1; |
| 6340 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6341 | memcpy(out->area, data_ptr, data_len); |
| 6342 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6343 | return 1; |
| 6344 | } |
| 6345 | |
| 6346 | return 0; |
| 6347 | |
| 6348 | } |
| 6349 | |
| 6350 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 6351 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 6352 | */ |
| 6353 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6354 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6355 | { |
| 6356 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6357 | ASN1_OBJECT *obj; |
| 6358 | ASN1_STRING *data; |
| 6359 | const unsigned char *data_ptr; |
| 6360 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6361 | int i, n, ln; |
| 6362 | int l = 0; |
| 6363 | const char *s; |
| 6364 | char *p; |
| 6365 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6366 | int name_count; |
| 6367 | |
| 6368 | |
| 6369 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6370 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6371 | out->data = 0; |
| 6372 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6373 | for (i = 0; i < name_count; i++) { |
| 6374 | ne = X509_NAME_get_entry(a, i); |
| 6375 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6376 | data = X509_NAME_ENTRY_get_data(ne); |
| 6377 | data_ptr = ASN1_STRING_get0_data(data); |
| 6378 | data_len = ASN1_STRING_length(data); |
| 6379 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6380 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6381 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6382 | s = tmp; |
| 6383 | } |
| 6384 | ln = strlen(s); |
| 6385 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6386 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6387 | if (l > out->size) |
| 6388 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6389 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6390 | |
| 6391 | *(p++)='/'; |
| 6392 | memcpy(p, s, ln); |
| 6393 | p += ln; |
| 6394 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6395 | memcpy(p, data_ptr, data_len); |
| 6396 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6397 | } |
| 6398 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6399 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6400 | return 0; |
| 6401 | |
| 6402 | return 1; |
| 6403 | } |
| 6404 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6405 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6406 | { |
| 6407 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6408 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6409 | |
Olivier Houchard | e488ea8 | 2019-06-28 14:10:33 +0200 | [diff] [blame] | 6410 | if (!ssl_sock_is_ssl(conn)) |
| 6411 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6412 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6413 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6414 | #endif |
| 6415 | } |
| 6416 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6417 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6418 | * to disable SNI. |
| 6419 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6420 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6421 | { |
| 6422 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6423 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6424 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6425 | char *prev_name; |
| 6426 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6427 | if (!ssl_sock_is_ssl(conn)) |
| 6428 | return; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6429 | ctx = conn->xprt_ctx; |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6430 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6431 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6432 | * new connection will present a new SNI. As an optimization we could |
| 6433 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6434 | * server. |
| 6435 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6436 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6437 | if ((!prev_name && hostname) || |
| 6438 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6439 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6440 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6441 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6442 | #endif |
| 6443 | } |
| 6444 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6445 | /* Extract peer certificate's common name into the chunk dest |
| 6446 | * Returns |
| 6447 | * the len of the extracted common name |
| 6448 | * or 0 if no CN found in DN |
| 6449 | * or -1 on error case (i.e. no peer certificate) |
| 6450 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6451 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6452 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6453 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6454 | struct ssl_sock_ctx *ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6455 | X509 *crt = NULL; |
| 6456 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6457 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6458 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6459 | .area = (char *)&find_cn, |
| 6460 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6461 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6462 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6463 | |
| 6464 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6465 | goto out; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6466 | ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6467 | |
| 6468 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6469 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6470 | if (!crt) |
| 6471 | goto out; |
| 6472 | |
| 6473 | name = X509_get_subject_name(crt); |
| 6474 | if (!name) |
| 6475 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6476 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6477 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6478 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6479 | if (crt) |
| 6480 | X509_free(crt); |
| 6481 | |
| 6482 | return result; |
| 6483 | } |
| 6484 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6485 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6486 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6487 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6488 | struct ssl_sock_ctx *ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6489 | X509 *crt = NULL; |
| 6490 | |
| 6491 | if (!ssl_sock_is_ssl(conn)) |
| 6492 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6493 | ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6494 | |
| 6495 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6496 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6497 | if (!crt) |
| 6498 | return 0; |
| 6499 | |
| 6500 | X509_free(crt); |
| 6501 | return 1; |
| 6502 | } |
| 6503 | |
| 6504 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6505 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6506 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6507 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6508 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6509 | if (!ssl_sock_is_ssl(conn)) |
| 6510 | return 0; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6511 | ctx = conn->xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6512 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6513 | } |
| 6514 | |
| 6515 | /* returns result from SSL verify */ |
| 6516 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6517 | { |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6518 | struct ssl_sock_ctx *ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6519 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6520 | if (!ssl_sock_is_ssl(conn)) |
| 6521 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
Christopher Faulet | 8200414 | 2019-09-10 10:12:03 +0200 | [diff] [blame] | 6522 | ctx = conn->xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6523 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6524 | } |
| 6525 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6526 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6527 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6528 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6529 | * freed by the caller. NPN is also checked if available since older versions |
| 6530 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6531 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6532 | 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] | 6533 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6534 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6535 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6536 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6537 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6538 | return 0; |
| 6539 | |
| 6540 | *str = NULL; |
| 6541 | |
| 6542 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6543 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6544 | if (*str) |
| 6545 | return 1; |
| 6546 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6547 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6548 | 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] | 6549 | if (*str) |
| 6550 | return 1; |
| 6551 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6552 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6553 | return 0; |
| 6554 | } |
| 6555 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6556 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 6557 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6558 | static int |
| 6559 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6560 | { |
| 6561 | struct connection *conn; |
| 6562 | |
| 6563 | conn = objt_conn(smp->sess->origin); |
| 6564 | if (!conn || conn->xprt != &ssl_sock) |
| 6565 | return 0; |
| 6566 | |
| 6567 | smp->flags = 0; |
| 6568 | smp->data.type = SMP_T_BOOL; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 6569 | #ifdef OPENSSL_IS_BORINGSSL |
| 6570 | { |
| 6571 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6572 | smp->data.u.sint = (SSL_in_early_data(ctx->ssl) && |
| 6573 | SSL_early_data_accepted(ctx->ssl)); |
| 6574 | } |
| 6575 | #else |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 6576 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
| 6577 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0; |
Emmanuel Hocdet | c985801 | 2019-08-07 14:44:49 +0200 | [diff] [blame] | 6578 | #endif |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6579 | return 1; |
| 6580 | } |
| 6581 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6582 | /* boolean, returns true if client cert was present */ |
| 6583 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6584 | 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] | 6585 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6586 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6587 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6588 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6589 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6590 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6591 | return 0; |
| 6592 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6593 | ctx = conn->xprt_ctx; |
| 6594 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6595 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6596 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6597 | return 0; |
| 6598 | } |
| 6599 | |
| 6600 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6601 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6602 | 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] | 6603 | |
| 6604 | return 1; |
| 6605 | } |
| 6606 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6607 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 6608 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6609 | * should be use. |
| 6610 | */ |
| 6611 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6612 | 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] | 6613 | { |
| 6614 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 6615 | X509 *crt = NULL; |
| 6616 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6617 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6618 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6619 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6620 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6621 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6622 | if (!conn || conn->xprt != &ssl_sock) |
| 6623 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6624 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6625 | |
| 6626 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6627 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6628 | return 0; |
| 6629 | } |
| 6630 | |
| 6631 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6632 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6633 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6634 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6635 | |
| 6636 | if (!crt) |
| 6637 | goto out; |
| 6638 | |
| 6639 | smp_trash = get_trash_chunk(); |
| 6640 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 6641 | goto out; |
| 6642 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6643 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6644 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6645 | ret = 1; |
| 6646 | out: |
| 6647 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6648 | if (cert_peer && crt) |
| 6649 | X509_free(crt); |
| 6650 | return ret; |
| 6651 | } |
| 6652 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6653 | /* binary, returns serial of certificate in a binary chunk. |
| 6654 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6655 | * should be use. |
| 6656 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6657 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6658 | 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] | 6659 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6660 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6661 | X509 *crt = NULL; |
| 6662 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6663 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6664 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6665 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6666 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6667 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6668 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6669 | return 0; |
| 6670 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6671 | ctx = conn->xprt_ctx; |
| 6672 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6673 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6674 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6675 | return 0; |
| 6676 | } |
| 6677 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6678 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6679 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6680 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6681 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6682 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6683 | if (!crt) |
| 6684 | goto out; |
| 6685 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6686 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6687 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 6688 | goto out; |
| 6689 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6690 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6691 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6692 | ret = 1; |
| 6693 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6694 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6695 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6696 | X509_free(crt); |
| 6697 | return ret; |
| 6698 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6699 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6700 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 6701 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6702 | * should be use. |
| 6703 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6704 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6705 | 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] | 6706 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6707 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6708 | X509 *crt = NULL; |
| 6709 | const EVP_MD *digest; |
| 6710 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6711 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6712 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6713 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6714 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6715 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6716 | if (!conn || conn->xprt != &ssl_sock) |
| 6717 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6718 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6719 | |
| 6720 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6721 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6722 | return 0; |
| 6723 | } |
| 6724 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6725 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6726 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6727 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6728 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6729 | if (!crt) |
| 6730 | goto out; |
| 6731 | |
| 6732 | smp_trash = get_trash_chunk(); |
| 6733 | digest = EVP_sha1(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6734 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, |
| 6735 | (unsigned int *)&smp_trash->data); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6736 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6737 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6738 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6739 | ret = 1; |
| 6740 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6741 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6742 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6743 | X509_free(crt); |
| 6744 | return ret; |
| 6745 | } |
| 6746 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6747 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 6748 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6749 | * should be use. |
| 6750 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6751 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6752 | 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] | 6753 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6754 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6755 | X509 *crt = NULL; |
| 6756 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6757 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6758 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6759 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6760 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6761 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6762 | if (!conn || conn->xprt != &ssl_sock) |
| 6763 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6764 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6765 | |
| 6766 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6767 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6768 | return 0; |
| 6769 | } |
| 6770 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6771 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6772 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6773 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6774 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6775 | if (!crt) |
| 6776 | goto out; |
| 6777 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6778 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6779 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6780 | goto out; |
| 6781 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6782 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6783 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6784 | ret = 1; |
| 6785 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6786 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6787 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6788 | X509_free(crt); |
| 6789 | return ret; |
| 6790 | } |
| 6791 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6792 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 6793 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6794 | * should be use. |
| 6795 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6796 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6797 | 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] | 6798 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6799 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6800 | X509 *crt = NULL; |
| 6801 | X509_NAME *name; |
| 6802 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6803 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6804 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6805 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6806 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6807 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6808 | if (!conn || conn->xprt != &ssl_sock) |
| 6809 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6810 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6811 | |
| 6812 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6813 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6814 | return 0; |
| 6815 | } |
| 6816 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6817 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6818 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6819 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6820 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6821 | if (!crt) |
| 6822 | goto out; |
| 6823 | |
| 6824 | name = X509_get_issuer_name(crt); |
| 6825 | if (!name) |
| 6826 | goto out; |
| 6827 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6828 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6829 | if (args && args[0].type == ARGT_STR) { |
| 6830 | int pos = 1; |
| 6831 | |
| 6832 | if (args[1].type == ARGT_SINT) |
| 6833 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6834 | |
| 6835 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6836 | goto out; |
| 6837 | } |
| 6838 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6839 | goto out; |
| 6840 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6841 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6842 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6843 | ret = 1; |
| 6844 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6845 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6846 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6847 | X509_free(crt); |
| 6848 | return ret; |
| 6849 | } |
| 6850 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6851 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 6852 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6853 | * should be use. |
| 6854 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6855 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6856 | 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] | 6857 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6858 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6859 | X509 *crt = NULL; |
| 6860 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6861 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6862 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6863 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6864 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6865 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6866 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6867 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6868 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6869 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6870 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6871 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6872 | return 0; |
| 6873 | } |
| 6874 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6875 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6876 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6877 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6878 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6879 | if (!crt) |
| 6880 | goto out; |
| 6881 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6882 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6883 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6884 | goto out; |
| 6885 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6886 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6887 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6888 | ret = 1; |
| 6889 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6890 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6891 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6892 | X509_free(crt); |
| 6893 | return ret; |
| 6894 | } |
| 6895 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6896 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 6897 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6898 | * should be use. |
| 6899 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6900 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6901 | 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] | 6902 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6903 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6904 | X509 *crt = NULL; |
| 6905 | X509_NAME *name; |
| 6906 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6907 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6908 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6909 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6910 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6911 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6912 | if (!conn || conn->xprt != &ssl_sock) |
| 6913 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6914 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6915 | |
| 6916 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6917 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6918 | return 0; |
| 6919 | } |
| 6920 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6921 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6922 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6923 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6924 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6925 | if (!crt) |
| 6926 | goto out; |
| 6927 | |
| 6928 | name = X509_get_subject_name(crt); |
| 6929 | if (!name) |
| 6930 | goto out; |
| 6931 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6932 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6933 | if (args && args[0].type == ARGT_STR) { |
| 6934 | int pos = 1; |
| 6935 | |
| 6936 | if (args[1].type == ARGT_SINT) |
| 6937 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6938 | |
| 6939 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6940 | goto out; |
| 6941 | } |
| 6942 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6943 | goto out; |
| 6944 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6945 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6946 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6947 | ret = 1; |
| 6948 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6949 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6950 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6951 | X509_free(crt); |
| 6952 | return ret; |
| 6953 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6954 | |
| 6955 | /* integer, returns true if current session use a client certificate */ |
| 6956 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6957 | 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] | 6958 | { |
| 6959 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6960 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6961 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6962 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6963 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6964 | if (!conn || conn->xprt != &ssl_sock) |
| 6965 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6966 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6967 | |
| 6968 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6969 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6970 | return 0; |
| 6971 | } |
| 6972 | |
| 6973 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6974 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6975 | if (crt) { |
| 6976 | X509_free(crt); |
| 6977 | } |
| 6978 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6979 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6980 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6981 | return 1; |
| 6982 | } |
| 6983 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6984 | /* integer, returns the certificate version |
| 6985 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6986 | * should be use. |
| 6987 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6988 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6989 | 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] | 6990 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6991 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6992 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6993 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6994 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6995 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6996 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6997 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6998 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6999 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7000 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7001 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7002 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7003 | return 0; |
| 7004 | } |
| 7005 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7006 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7007 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7008 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7009 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7010 | if (!crt) |
| 7011 | return 0; |
| 7012 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7013 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7014 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7015 | if (cert_peer) |
| 7016 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7017 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 7018 | |
| 7019 | return 1; |
| 7020 | } |
| 7021 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7022 | /* string, returns the certificate's signature algorithm. |
| 7023 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7024 | * should be use. |
| 7025 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7026 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7027 | 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] | 7028 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7029 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7030 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7031 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7032 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7033 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7034 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7035 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7036 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7037 | if (!conn || conn->xprt != &ssl_sock) |
| 7038 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7039 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7040 | |
| 7041 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7042 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7043 | return 0; |
| 7044 | } |
| 7045 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7046 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7047 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7048 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7049 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7050 | if (!crt) |
| 7051 | return 0; |
| 7052 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7053 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 7054 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7055 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7056 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 7057 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7058 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7059 | if (cert_peer) |
| 7060 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7061 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 7062 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7063 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7064 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7065 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7066 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7067 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7068 | if (cert_peer) |
| 7069 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 7070 | |
| 7071 | return 1; |
| 7072 | } |
| 7073 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7074 | /* string, returns the certificate's key algorithm. |
| 7075 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 7076 | * should be use. |
| 7077 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7078 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7079 | 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] | 7080 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7081 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7082 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7083 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7084 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7085 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7086 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7087 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7088 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7089 | if (!conn || conn->xprt != &ssl_sock) |
| 7090 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7091 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7092 | |
| 7093 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7094 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7095 | return 0; |
| 7096 | } |
| 7097 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7098 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7099 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7100 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7101 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7102 | if (!crt) |
| 7103 | return 0; |
| 7104 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 7105 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 7106 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7107 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7108 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 7109 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7110 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7111 | if (cert_peer) |
| 7112 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7113 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 7114 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7115 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7116 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7117 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7118 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7119 | if (cert_peer) |
| 7120 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7121 | |
| 7122 | return 1; |
| 7123 | } |
| 7124 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7125 | /* boolean, returns true if front conn. transport layer is SSL. |
| 7126 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7127 | * char is 'b'. |
| 7128 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7129 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7130 | 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] | 7131 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7132 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7133 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7134 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7135 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7136 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7137 | return 1; |
| 7138 | } |
| 7139 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7140 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7141 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7142 | 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] | 7143 | { |
| 7144 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7145 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7146 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7147 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7148 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7149 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7150 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7151 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7152 | return 1; |
| 7153 | #else |
| 7154 | return 0; |
| 7155 | #endif |
| 7156 | } |
| 7157 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7158 | /* boolean, returns true if client session has been resumed. |
| 7159 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7160 | * char is 'b'. |
| 7161 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7162 | static int |
| 7163 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7164 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7165 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7166 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7167 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7168 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7169 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7170 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7171 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7172 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7173 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7174 | return 1; |
| 7175 | } |
| 7176 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7177 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 7178 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7179 | * char is 'b'. |
| 7180 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7181 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7182 | 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] | 7183 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7184 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7185 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7186 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7187 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7188 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7189 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7190 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7191 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7192 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7193 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7194 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7195 | return 0; |
| 7196 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7197 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7198 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7199 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7200 | |
| 7201 | return 1; |
| 7202 | } |
| 7203 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7204 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 7205 | * is SSL. |
| 7206 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7207 | * char is 'b'. |
| 7208 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7209 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7210 | 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] | 7211 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7212 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7213 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7214 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7215 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7216 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7217 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7218 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7219 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7220 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7221 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7222 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7223 | return 0; |
| 7224 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7225 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7226 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7227 | |
| 7228 | return 1; |
| 7229 | } |
| 7230 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7231 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 7232 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7233 | * char is 'b'. |
| 7234 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7235 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7236 | 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] | 7237 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7238 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7239 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7240 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7241 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7242 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7243 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7244 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7245 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7246 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7247 | 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] | 7248 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7249 | return 0; |
| 7250 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7251 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7252 | |
| 7253 | return 1; |
| 7254 | } |
| 7255 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7256 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7257 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7258 | 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] | 7259 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7260 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7261 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7262 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7263 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7264 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7265 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 7266 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 7267 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7268 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7269 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7270 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7271 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7272 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7273 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7274 | (const unsigned char **)&smp->data.u.str.area, |
| 7275 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7276 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7277 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7278 | return 0; |
| 7279 | |
| 7280 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7281 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7282 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7283 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7284 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7285 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7286 | 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] | 7287 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7288 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7289 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7290 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7291 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7292 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7293 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 7294 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 7295 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7296 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7297 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7298 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7299 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7300 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7301 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7302 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7303 | (const unsigned char **)&smp->data.u.str.area, |
| 7304 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7305 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7306 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7307 | return 0; |
| 7308 | |
| 7309 | return 1; |
| 7310 | } |
| 7311 | #endif |
| 7312 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7313 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 7314 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7315 | * char is 'b'. |
| 7316 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7317 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7318 | 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] | 7319 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7320 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7321 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7322 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7323 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7324 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7325 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7326 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7327 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7328 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7329 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7330 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7331 | return 0; |
| 7332 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7333 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7334 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7335 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7336 | |
| 7337 | return 1; |
| 7338 | } |
| 7339 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7340 | /* 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] | 7341 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7342 | * char is 'b'. |
| 7343 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7344 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7345 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7346 | 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] | 7347 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7348 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7349 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7350 | SSL_SESSION *ssl_sess; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7351 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7352 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7353 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7354 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7355 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7356 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7357 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7358 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7359 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7360 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7361 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7362 | return 0; |
| 7363 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7364 | smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, |
| 7365 | (unsigned int *)&smp->data.u.str.data); |
| 7366 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7367 | return 0; |
| 7368 | |
| 7369 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7370 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7371 | #endif |
| 7372 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7373 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 7374 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7375 | static int |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 7376 | smp_fetch_ssl_fc_random(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7377 | { |
| 7378 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7379 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7380 | struct buffer *data; |
| 7381 | struct ssl_sock_ctx *ctx; |
| 7382 | |
| 7383 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7384 | return 0; |
| 7385 | ctx = conn->xprt_ctx; |
| 7386 | |
| 7387 | data = get_trash_chunk(); |
| 7388 | if (kw[7] == 'c') |
| 7389 | data->data = SSL_get_client_random(ctx->ssl, |
| 7390 | (unsigned char *) data->area, |
| 7391 | data->size); |
| 7392 | else |
| 7393 | data->data = SSL_get_server_random(ctx->ssl, |
| 7394 | (unsigned char *) data->area, |
| 7395 | data->size); |
| 7396 | if (!data->data) |
| 7397 | return 0; |
| 7398 | |
| 7399 | smp->flags = 0; |
| 7400 | smp->data.type = SMP_T_BIN; |
| 7401 | smp->data.u.str = *data; |
| 7402 | |
| 7403 | return 1; |
| 7404 | } |
| 7405 | |
| 7406 | static int |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7407 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7408 | { |
| 7409 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7410 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7411 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7412 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7413 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7414 | |
| 7415 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7416 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7417 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7418 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7419 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7420 | if (!ssl_sess) |
| 7421 | return 0; |
| 7422 | |
| 7423 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7424 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 7425 | (unsigned char *) data->area, |
| 7426 | data->size); |
| 7427 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7428 | return 0; |
| 7429 | |
| 7430 | smp->flags = 0; |
| 7431 | smp->data.type = SMP_T_BIN; |
| 7432 | smp->data.u.str = *data; |
| 7433 | |
| 7434 | return 1; |
| 7435 | } |
| 7436 | #endif |
| 7437 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7438 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7439 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7440 | 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] | 7441 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7442 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7443 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7444 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7445 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7446 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7447 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7448 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7449 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7450 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7451 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7452 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7453 | 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] | 7454 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 7455 | return 0; |
| 7456 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7457 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7458 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7459 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7460 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7461 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7462 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7463 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7464 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7465 | struct connection *conn; |
| 7466 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7467 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7468 | |
| 7469 | conn = objt_conn(smp->sess->origin); |
| 7470 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7471 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7472 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7473 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7474 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7475 | if (!capture) |
| 7476 | return 0; |
| 7477 | |
| 7478 | smp->flags = SMP_F_CONST; |
| 7479 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7480 | smp->data.u.str.area = capture->ciphersuite; |
| 7481 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7482 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7483 | } |
| 7484 | |
| 7485 | static int |
| 7486 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7487 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7488 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7489 | |
| 7490 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7491 | return 0; |
| 7492 | |
| 7493 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7494 | 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] | 7495 | smp->data.type = SMP_T_BIN; |
| 7496 | smp->data.u.str = *data; |
| 7497 | return 1; |
| 7498 | } |
| 7499 | |
| 7500 | static int |
| 7501 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7502 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7503 | struct connection *conn; |
| 7504 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7505 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7506 | |
| 7507 | conn = objt_conn(smp->sess->origin); |
| 7508 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7509 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7510 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7511 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7512 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7513 | if (!capture) |
| 7514 | return 0; |
| 7515 | |
| 7516 | smp->data.type = SMP_T_SINT; |
| 7517 | smp->data.u.sint = capture->xxh64; |
| 7518 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7519 | } |
| 7520 | |
| 7521 | static int |
| 7522 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7523 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7524 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7525 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7526 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7527 | |
| 7528 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7529 | return 0; |
| 7530 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7531 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7532 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7533 | const char *str; |
| 7534 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7535 | 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] | 7536 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 7537 | #if defined(OPENSSL_IS_BORINGSSL) |
| 7538 | cipher = SSL_get_cipher_by_value(id); |
| 7539 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 7540 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7541 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7542 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7543 | #endif |
| 7544 | str = SSL_CIPHER_get_name(cipher); |
| 7545 | if (!str || strcmp(str, "(NONE)") == 0) |
| 7546 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7547 | else |
| 7548 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 7549 | } |
| 7550 | smp->data.type = SMP_T_STR; |
| 7551 | smp->data.u.str = *data; |
| 7552 | return 1; |
| 7553 | #else |
| 7554 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 7555 | #endif |
| 7556 | } |
| 7557 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7558 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7559 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7560 | 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] | 7561 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7562 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7563 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7564 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7565 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7566 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7567 | |
| 7568 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7569 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7570 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7571 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7572 | |
| 7573 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 7574 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7575 | return 0; |
| 7576 | } |
| 7577 | |
| 7578 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7579 | if (!SSL_session_reused(ctx->ssl)) |
| 7580 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7581 | finished_trash->area, |
| 7582 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7583 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7584 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7585 | finished_trash->area, |
| 7586 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7587 | |
| 7588 | if (!finished_len) |
| 7589 | return 0; |
| 7590 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7591 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7592 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7593 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7594 | |
| 7595 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7596 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7597 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7598 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7599 | /* 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] | 7600 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7601 | 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] | 7602 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7603 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7604 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7605 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7606 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7607 | if (!conn || conn->xprt != &ssl_sock) |
| 7608 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7609 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7610 | |
| 7611 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7612 | smp->flags = SMP_F_MAY_CHANGE; |
| 7613 | return 0; |
| 7614 | } |
| 7615 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7616 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7617 | 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] | 7618 | smp->flags = 0; |
| 7619 | |
| 7620 | return 1; |
| 7621 | } |
| 7622 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7623 | /* 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] | 7624 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7625 | 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] | 7626 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7627 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7628 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7629 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7630 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7631 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7632 | return 0; |
| 7633 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7634 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7635 | smp->flags = SMP_F_MAY_CHANGE; |
| 7636 | return 0; |
| 7637 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7638 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7639 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7640 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7641 | 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] | 7642 | smp->flags = 0; |
| 7643 | |
| 7644 | return 1; |
| 7645 | } |
| 7646 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7647 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7648 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7649 | 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] | 7650 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7651 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7652 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7653 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7654 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7655 | if (!conn || conn->xprt != &ssl_sock) |
| 7656 | return 0; |
| 7657 | |
| 7658 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7659 | smp->flags = SMP_F_MAY_CHANGE; |
| 7660 | return 0; |
| 7661 | } |
| 7662 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7663 | ctx = conn->xprt_ctx; |
| 7664 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7665 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7666 | 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] | 7667 | smp->flags = 0; |
| 7668 | |
| 7669 | return 1; |
| 7670 | } |
| 7671 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7672 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7673 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7674 | 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] | 7675 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7676 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7677 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7678 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7679 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7680 | if (!conn || conn->xprt != &ssl_sock) |
| 7681 | return 0; |
| 7682 | |
| 7683 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7684 | smp->flags = SMP_F_MAY_CHANGE; |
| 7685 | return 0; |
| 7686 | } |
| 7687 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7688 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7689 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7690 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7691 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7692 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7693 | 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] | 7694 | smp->flags = 0; |
| 7695 | |
| 7696 | return 1; |
| 7697 | } |
| 7698 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7699 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7700 | 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] | 7701 | { |
| 7702 | if (!*args[cur_arg + 1]) { |
| 7703 | if (err) |
| 7704 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7705 | return ERR_ALERT | ERR_FATAL; |
| 7706 | } |
| 7707 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7708 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7709 | 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] | 7710 | else |
| 7711 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7712 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7713 | return 0; |
| 7714 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7715 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7716 | { |
| 7717 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7718 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7719 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7720 | /* parse the "ca-sign-file" bind keyword */ |
| 7721 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7722 | { |
| 7723 | if (!*args[cur_arg + 1]) { |
| 7724 | if (err) |
| 7725 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7726 | return ERR_ALERT | ERR_FATAL; |
| 7727 | } |
| 7728 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7729 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7730 | 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] | 7731 | else |
| 7732 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 7733 | |
| 7734 | return 0; |
| 7735 | } |
| 7736 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7737 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7738 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7739 | { |
| 7740 | if (!*args[cur_arg + 1]) { |
| 7741 | if (err) |
| 7742 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 7743 | return ERR_ALERT | ERR_FATAL; |
| 7744 | } |
| 7745 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 7746 | return 0; |
| 7747 | } |
| 7748 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7749 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7750 | 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] | 7751 | { |
| 7752 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7753 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7754 | return ERR_ALERT | ERR_FATAL; |
| 7755 | } |
| 7756 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7757 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7758 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7759 | return 0; |
| 7760 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7761 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7762 | { |
| 7763 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 7764 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7765 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 7766 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7767 | /* parse the "ciphersuites" bind keyword */ |
| 7768 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7769 | { |
| 7770 | if (!*args[cur_arg + 1]) { |
| 7771 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 7772 | return ERR_ALERT | ERR_FATAL; |
| 7773 | } |
| 7774 | |
| 7775 | free(conf->ciphersuites); |
| 7776 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 7777 | return 0; |
| 7778 | } |
| 7779 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7780 | { |
| 7781 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 7782 | } |
| 7783 | #endif |
| 7784 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7785 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7786 | 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] | 7787 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 7788 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 7789 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7790 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7791 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7792 | return ERR_ALERT | ERR_FATAL; |
| 7793 | } |
| 7794 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7795 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 7796 | 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] | 7797 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 7798 | return ERR_ALERT | ERR_FATAL; |
| 7799 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7800 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, args[cur_arg + 1]); |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 7801 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7802 | return ERR_ALERT | ERR_FATAL; |
| 7803 | |
| 7804 | return 0; |
| 7805 | } |
| 7806 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 7807 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7808 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7809 | |
| 7810 | return 0; |
| 7811 | } |
| 7812 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7813 | /* parse the "crt-list" bind keyword */ |
| 7814 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7815 | { |
| 7816 | if (!*args[cur_arg + 1]) { |
| 7817 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 7818 | return ERR_ALERT | ERR_FATAL; |
| 7819 | } |
| 7820 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7821 | if (ssl_sock_load_cert_list_file(args[cur_arg + 1], conf, px, err) > 0) { |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 7822 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7823 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 7824 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7825 | |
| 7826 | return 0; |
| 7827 | } |
| 7828 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7829 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7830 | 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] | 7831 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7832 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7833 | if (err) |
| 7834 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 7835 | return ERR_ALERT | ERR_FATAL; |
| 7836 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7837 | if (!*args[cur_arg + 1]) { |
| 7838 | if (err) |
| 7839 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 7840 | return ERR_ALERT | ERR_FATAL; |
| 7841 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7842 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7843 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7844 | 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] | 7845 | else |
| 7846 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7847 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7848 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7849 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7850 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7851 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7852 | { |
| 7853 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7854 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7855 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7856 | /* parse the "curves" bind keyword keyword */ |
| 7857 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7858 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7859 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7860 | if (!*args[cur_arg + 1]) { |
| 7861 | if (err) |
| 7862 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 7863 | return ERR_ALERT | ERR_FATAL; |
| 7864 | } |
| 7865 | conf->curves = strdup(args[cur_arg + 1]); |
| 7866 | return 0; |
| 7867 | #else |
| 7868 | if (err) |
| 7869 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 7870 | return ERR_ALERT | ERR_FATAL; |
| 7871 | #endif |
| 7872 | } |
| 7873 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7874 | { |
| 7875 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 7876 | } |
| 7877 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7878 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7879 | 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] | 7880 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7881 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7882 | if (err) |
| 7883 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 7884 | return ERR_ALERT | ERR_FATAL; |
| 7885 | #elif defined(OPENSSL_NO_ECDH) |
| 7886 | if (err) |
| 7887 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 7888 | return ERR_ALERT | ERR_FATAL; |
| 7889 | #else |
| 7890 | if (!*args[cur_arg + 1]) { |
| 7891 | if (err) |
| 7892 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 7893 | return ERR_ALERT | ERR_FATAL; |
| 7894 | } |
| 7895 | |
| 7896 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7897 | |
| 7898 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7899 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7900 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7901 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7902 | { |
| 7903 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 7904 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7905 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7906 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7907 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7908 | { |
| 7909 | int code; |
| 7910 | char *p = args[cur_arg + 1]; |
| 7911 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 7912 | |
| 7913 | if (!*p) { |
| 7914 | if (err) |
| 7915 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 7916 | return ERR_ALERT | ERR_FATAL; |
| 7917 | } |
| 7918 | |
| 7919 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 7920 | ignerr = &conf->ca_ignerr; |
| 7921 | |
| 7922 | if (strcmp(p, "all") == 0) { |
| 7923 | *ignerr = ~0ULL; |
| 7924 | return 0; |
| 7925 | } |
| 7926 | |
| 7927 | while (p) { |
| 7928 | code = atoi(p); |
| 7929 | if ((code <= 0) || (code > 63)) { |
| 7930 | if (err) |
| 7931 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 7932 | args[cur_arg], code, args[cur_arg + 1]); |
| 7933 | return ERR_ALERT | ERR_FATAL; |
| 7934 | } |
| 7935 | *ignerr |= 1ULL << code; |
| 7936 | p = strchr(p, ','); |
| 7937 | if (p) |
| 7938 | p++; |
| 7939 | } |
| 7940 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7941 | return 0; |
| 7942 | } |
| 7943 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7944 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 7945 | 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] | 7946 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7947 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7948 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7949 | p = strchr(arg, '-'); |
| 7950 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7951 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7952 | p++; |
| 7953 | if (!strcmp(p, "sslv3")) |
| 7954 | v = CONF_SSLV3; |
| 7955 | else if (!strcmp(p, "tlsv10")) |
| 7956 | v = CONF_TLSV10; |
| 7957 | else if (!strcmp(p, "tlsv11")) |
| 7958 | v = CONF_TLSV11; |
| 7959 | else if (!strcmp(p, "tlsv12")) |
| 7960 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7961 | else if (!strcmp(p, "tlsv13")) |
| 7962 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7963 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7964 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7965 | if (!strncmp(arg, "no-", 3)) |
| 7966 | methods->flags |= methodVersions[v].flag; |
| 7967 | else if (!strncmp(arg, "force-", 6)) |
| 7968 | methods->min = methods->max = v; |
| 7969 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7970 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7971 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7972 | fail: |
| 7973 | if (err) |
| 7974 | memprintf(err, "'%s' : option not implemented", arg); |
| 7975 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7976 | } |
| 7977 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7978 | 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] | 7979 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7980 | 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] | 7981 | } |
| 7982 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7983 | 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] | 7984 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7985 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 7986 | } |
| 7987 | |
| 7988 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 7989 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 7990 | { |
| 7991 | uint16_t i, v = 0; |
| 7992 | char *argv = args[cur_arg + 1]; |
| 7993 | if (!*argv) { |
| 7994 | if (err) |
| 7995 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 7996 | return ERR_ALERT | ERR_FATAL; |
| 7997 | } |
| 7998 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7999 | if (!strcmp(argv, methodVersions[i].name)) |
| 8000 | v = i; |
| 8001 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8002 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8003 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8004 | return ERR_ALERT | ERR_FATAL; |
| 8005 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8006 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 8007 | methods->min = v; |
| 8008 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 8009 | methods->max = v; |
| 8010 | else { |
| 8011 | if (err) |
| 8012 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 8013 | return ERR_ALERT | ERR_FATAL; |
| 8014 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8015 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8016 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 8017 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 8018 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8019 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 8020 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8021 | 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] | 8022 | #endif |
| 8023 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 8024 | } |
| 8025 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8026 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8027 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8028 | 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] | 8029 | } |
| 8030 | |
| 8031 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8032 | { |
| 8033 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 8034 | } |
| 8035 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8036 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 8037 | 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] | 8038 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 8039 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 8040 | return 0; |
| 8041 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 8042 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8043 | /* parse the "allow-0rtt" bind keyword */ |
| 8044 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8045 | { |
| 8046 | conf->early_data = 1; |
| 8047 | return 0; |
| 8048 | } |
| 8049 | |
| 8050 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8051 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 8052 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 8053 | return 0; |
| 8054 | } |
| 8055 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8056 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8057 | 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] | 8058 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 8059 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8060 | char *p1, *p2; |
| 8061 | |
| 8062 | if (!*args[cur_arg + 1]) { |
| 8063 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 8064 | return ERR_ALERT | ERR_FATAL; |
| 8065 | } |
| 8066 | |
| 8067 | free(conf->npn_str); |
| 8068 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 8069 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 8070 | * so we reuse each comma to store the next <len> and need |
| 8071 | * one more for the end of the string. |
| 8072 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8073 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 8074 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 8075 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 8076 | |
| 8077 | /* replace commas with the name length */ |
| 8078 | p1 = conf->npn_str; |
| 8079 | p2 = p1 + 1; |
| 8080 | while (1) { |
| 8081 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 8082 | if (!p2) |
| 8083 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8084 | |
| 8085 | if (p2 - (p1 + 1) > 255) { |
| 8086 | *p2 = '\0'; |
| 8087 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 8088 | return ERR_ALERT | ERR_FATAL; |
| 8089 | } |
| 8090 | |
| 8091 | *p1 = p2 - (p1 + 1); |
| 8092 | p1 = p2; |
| 8093 | |
| 8094 | if (!*p2) |
| 8095 | break; |
| 8096 | |
| 8097 | *(p2++) = '\0'; |
| 8098 | } |
| 8099 | return 0; |
| 8100 | #else |
| 8101 | if (err) |
| 8102 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 8103 | return ERR_ALERT | ERR_FATAL; |
| 8104 | #endif |
| 8105 | } |
| 8106 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8107 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8108 | { |
| 8109 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 8110 | } |
| 8111 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8112 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8113 | 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] | 8114 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 8115 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8116 | char *p1, *p2; |
| 8117 | |
| 8118 | if (!*args[cur_arg + 1]) { |
| 8119 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 8120 | return ERR_ALERT | ERR_FATAL; |
| 8121 | } |
| 8122 | |
| 8123 | free(conf->alpn_str); |
| 8124 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 8125 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 8126 | * so we reuse each comma to store the next <len> and need |
| 8127 | * one more for the end of the string. |
| 8128 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8129 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 8130 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 8131 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 8132 | |
| 8133 | /* replace commas with the name length */ |
| 8134 | p1 = conf->alpn_str; |
| 8135 | p2 = p1 + 1; |
| 8136 | while (1) { |
| 8137 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 8138 | if (!p2) |
| 8139 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8140 | |
| 8141 | if (p2 - (p1 + 1) > 255) { |
| 8142 | *p2 = '\0'; |
| 8143 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 8144 | return ERR_ALERT | ERR_FATAL; |
| 8145 | } |
| 8146 | |
| 8147 | *p1 = p2 - (p1 + 1); |
| 8148 | p1 = p2; |
| 8149 | |
| 8150 | if (!*p2) |
| 8151 | break; |
| 8152 | |
| 8153 | *(p2++) = '\0'; |
| 8154 | } |
| 8155 | return 0; |
| 8156 | #else |
| 8157 | if (err) |
| 8158 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 8159 | return ERR_ALERT | ERR_FATAL; |
| 8160 | #endif |
| 8161 | } |
| 8162 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8163 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8164 | { |
| 8165 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 8166 | } |
| 8167 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8168 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8169 | 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] | 8170 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 8171 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8172 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8173 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8174 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 8175 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8176 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8177 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 8178 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 8179 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 8180 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8181 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 8182 | if (!conf->ssl_conf.ssl_methods.min) |
| 8183 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 8184 | if (!conf->ssl_conf.ssl_methods.max) |
| 8185 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8186 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8187 | return 0; |
| 8188 | } |
| 8189 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8190 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 8191 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8192 | { |
| 8193 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 8194 | return 0; |
| 8195 | } |
| 8196 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8197 | /* parse the "generate-certificates" bind keyword */ |
| 8198 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8199 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8200 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8201 | conf->generate_certs = 1; |
| 8202 | #else |
| 8203 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 8204 | err && *err ? *err : ""); |
| 8205 | #endif |
| 8206 | return 0; |
| 8207 | } |
| 8208 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 8209 | /* parse the "strict-sni" bind keyword */ |
| 8210 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8211 | { |
| 8212 | conf->strict_sni = 1; |
| 8213 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8214 | } |
| 8215 | |
| 8216 | /* parse the "tls-ticket-keys" bind keyword */ |
| 8217 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8218 | { |
| 8219 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8220 | FILE *f; |
| 8221 | int i = 0; |
| 8222 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8223 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8224 | |
| 8225 | if (!*args[cur_arg + 1]) { |
| 8226 | if (err) |
| 8227 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 8228 | return ERR_ALERT | ERR_FATAL; |
| 8229 | } |
| 8230 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8231 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 8232 | if (keys_ref) { |
| 8233 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8234 | conf->keys_ref = keys_ref; |
| 8235 | return 0; |
| 8236 | } |
| 8237 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8238 | keys_ref = malloc(sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8239 | if (!keys_ref) { |
| 8240 | if (err) |
| 8241 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 8242 | return ERR_ALERT | ERR_FATAL; |
| 8243 | } |
| 8244 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8245 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8246 | if (!keys_ref->tlskeys) { |
| 8247 | free(keys_ref); |
| 8248 | if (err) |
| 8249 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 8250 | return ERR_ALERT | ERR_FATAL; |
| 8251 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8252 | |
| 8253 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8254 | free(keys_ref->tlskeys); |
| 8255 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8256 | if (err) |
| 8257 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 8258 | return ERR_ALERT | ERR_FATAL; |
| 8259 | } |
| 8260 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8261 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8262 | if (!keys_ref->filename) { |
| 8263 | free(keys_ref->tlskeys); |
| 8264 | free(keys_ref); |
| 8265 | if (err) |
| 8266 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 8267 | return ERR_ALERT | ERR_FATAL; |
| 8268 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8269 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8270 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8271 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 8272 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8273 | int dec_size; |
| 8274 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8275 | /* Strip newline characters from the end */ |
| 8276 | if(thisline[len - 1] == '\n') |
| 8277 | thisline[--len] = 0; |
| 8278 | |
| 8279 | if(thisline[len - 1] == '\r') |
| 8280 | thisline[--len] = 0; |
| 8281 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8282 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 8283 | if (dec_size < 0) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8284 | free(keys_ref->filename); |
| 8285 | free(keys_ref->tlskeys); |
| 8286 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8287 | if (err) |
| 8288 | memprintf(err, "'%s' : unable to decode base64 key on line %d", args[cur_arg+1], i + 1); |
mildis | 16aa015 | 2016-06-22 17:46:29 +0200 | [diff] [blame] | 8289 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8290 | return ERR_ALERT | ERR_FATAL; |
| 8291 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8292 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 8293 | keys_ref->key_size_bits = 128; |
| 8294 | } |
| 8295 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 8296 | keys_ref->key_size_bits = 256; |
| 8297 | } |
| 8298 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 8299 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 8300 | || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) { |
| 8301 | free(keys_ref->filename); |
| 8302 | free(keys_ref->tlskeys); |
| 8303 | free(keys_ref); |
| 8304 | if (err) |
| 8305 | memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1); |
| 8306 | fclose(f); |
| 8307 | return ERR_ALERT | ERR_FATAL; |
| 8308 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8309 | i++; |
| 8310 | } |
| 8311 | |
| 8312 | if (i < TLS_TICKETS_NO) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8313 | free(keys_ref->filename); |
| 8314 | free(keys_ref->tlskeys); |
| 8315 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8316 | if (err) |
| 8317 | memprintf(err, "'%s' : please supply at least %d keys in the tls-tickets-file", args[cur_arg+1], TLS_TICKETS_NO); |
mildis | 16aa015 | 2016-06-22 17:46:29 +0200 | [diff] [blame] | 8318 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8319 | return ERR_ALERT | ERR_FATAL; |
| 8320 | } |
| 8321 | |
| 8322 | fclose(f); |
| 8323 | |
| 8324 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 8325 | i -= 2; |
| 8326 | 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] | 8327 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 8328 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8329 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8330 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8331 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8332 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 8333 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8334 | return 0; |
| 8335 | #else |
| 8336 | if (err) |
| 8337 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 8338 | return ERR_ALERT | ERR_FATAL; |
| 8339 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 8340 | } |
| 8341 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8342 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8343 | 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] | 8344 | { |
| 8345 | if (!*args[cur_arg + 1]) { |
| 8346 | if (err) |
| 8347 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 8348 | return ERR_ALERT | ERR_FATAL; |
| 8349 | } |
| 8350 | |
| 8351 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8352 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8353 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8354 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8355 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8356 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8357 | else { |
| 8358 | if (err) |
| 8359 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 8360 | args[cur_arg], args[cur_arg + 1]); |
| 8361 | return ERR_ALERT | ERR_FATAL; |
| 8362 | } |
| 8363 | |
| 8364 | return 0; |
| 8365 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8366 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8367 | { |
| 8368 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 8369 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8370 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8371 | /* parse the "no-ca-names" bind keyword */ |
| 8372 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8373 | { |
| 8374 | conf->no_ca_names = 1; |
| 8375 | return 0; |
| 8376 | } |
| 8377 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8378 | { |
| 8379 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 8380 | } |
| 8381 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8382 | /************** "server" keywords ****************/ |
| 8383 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8384 | /* parse the "npn" bind keyword */ |
| 8385 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8386 | { |
| 8387 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 8388 | char *p1, *p2; |
| 8389 | |
| 8390 | if (!*args[*cur_arg + 1]) { |
| 8391 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 8392 | return ERR_ALERT | ERR_FATAL; |
| 8393 | } |
| 8394 | |
| 8395 | free(newsrv->ssl_ctx.npn_str); |
| 8396 | |
| 8397 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 8398 | * so we reuse each comma to store the next <len> and need |
| 8399 | * one more for the end of the string. |
| 8400 | */ |
| 8401 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8402 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 8403 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 8404 | newsrv->ssl_ctx.npn_len); |
| 8405 | |
| 8406 | /* replace commas with the name length */ |
| 8407 | p1 = newsrv->ssl_ctx.npn_str; |
| 8408 | p2 = p1 + 1; |
| 8409 | while (1) { |
| 8410 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 8411 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 8412 | if (!p2) |
| 8413 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8414 | |
| 8415 | if (p2 - (p1 + 1) > 255) { |
| 8416 | *p2 = '\0'; |
| 8417 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8418 | return ERR_ALERT | ERR_FATAL; |
| 8419 | } |
| 8420 | |
| 8421 | *p1 = p2 - (p1 + 1); |
| 8422 | p1 = p2; |
| 8423 | |
| 8424 | if (!*p2) |
| 8425 | break; |
| 8426 | |
| 8427 | *(p2++) = '\0'; |
| 8428 | } |
| 8429 | return 0; |
| 8430 | #else |
| 8431 | if (err) |
| 8432 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]); |
| 8433 | return ERR_ALERT | ERR_FATAL; |
| 8434 | #endif |
| 8435 | } |
| 8436 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8437 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8438 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8439 | { |
| 8440 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 8441 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8442 | char **alpn_str; |
| 8443 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8444 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8445 | if (*args[*cur_arg] == 'c') { |
| 8446 | alpn_str = &newsrv->check.alpn_str; |
| 8447 | alpn_len = &newsrv->check.alpn_len; |
| 8448 | } else { |
| 8449 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 8450 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 8451 | |
| 8452 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8453 | if (!*args[*cur_arg + 1]) { |
| 8454 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 8455 | return ERR_ALERT | ERR_FATAL; |
| 8456 | } |
| 8457 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8458 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8459 | |
| 8460 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 8461 | * so we reuse each comma to store the next <len> and need |
| 8462 | * one more for the end of the string. |
| 8463 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8464 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8465 | *alpn_str = calloc(1, *alpn_len + 1); |
| 8466 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8467 | |
| 8468 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8469 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8470 | p2 = p1 + 1; |
| 8471 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8472 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8473 | if (!p2) |
| 8474 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8475 | |
| 8476 | if (p2 - (p1 + 1) > 255) { |
| 8477 | *p2 = '\0'; |
| 8478 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8479 | return ERR_ALERT | ERR_FATAL; |
| 8480 | } |
| 8481 | |
| 8482 | *p1 = p2 - (p1 + 1); |
| 8483 | p1 = p2; |
| 8484 | |
| 8485 | if (!*p2) |
| 8486 | break; |
| 8487 | |
| 8488 | *(p2++) = '\0'; |
| 8489 | } |
| 8490 | return 0; |
| 8491 | #else |
| 8492 | if (err) |
| 8493 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]); |
| 8494 | return ERR_ALERT | ERR_FATAL; |
| 8495 | #endif |
| 8496 | } |
| 8497 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8498 | /* parse the "ca-file" server keyword */ |
| 8499 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8500 | { |
| 8501 | if (!*args[*cur_arg + 1]) { |
| 8502 | if (err) |
| 8503 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 8504 | return ERR_ALERT | ERR_FATAL; |
| 8505 | } |
| 8506 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8507 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8508 | 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] | 8509 | else |
| 8510 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 8511 | |
| 8512 | return 0; |
| 8513 | } |
| 8514 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 8515 | /* parse the "check-sni" server keyword */ |
| 8516 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8517 | { |
| 8518 | if (!*args[*cur_arg + 1]) { |
| 8519 | if (err) |
| 8520 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 8521 | return ERR_ALERT | ERR_FATAL; |
| 8522 | } |
| 8523 | |
| 8524 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 8525 | if (!newsrv->check.sni) { |
| 8526 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 8527 | return ERR_ALERT | ERR_FATAL; |
| 8528 | } |
| 8529 | return 0; |
| 8530 | |
| 8531 | } |
| 8532 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8533 | /* parse the "check-ssl" server keyword */ |
| 8534 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8535 | { |
| 8536 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8537 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 8538 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8539 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8540 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 8541 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8542 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8543 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8544 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 8545 | if (!newsrv->ssl_ctx.methods.min) |
| 8546 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 8547 | if (!newsrv->ssl_ctx.methods.max) |
| 8548 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 8549 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8550 | return 0; |
| 8551 | } |
| 8552 | |
| 8553 | /* parse the "ciphers" server keyword */ |
| 8554 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8555 | { |
| 8556 | if (!*args[*cur_arg + 1]) { |
| 8557 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 8558 | return ERR_ALERT | ERR_FATAL; |
| 8559 | } |
| 8560 | |
| 8561 | free(newsrv->ssl_ctx.ciphers); |
| 8562 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 8563 | return 0; |
| 8564 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8565 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8566 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8567 | /* parse the "ciphersuites" server keyword */ |
| 8568 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8569 | { |
| 8570 | if (!*args[*cur_arg + 1]) { |
| 8571 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 8572 | return ERR_ALERT | ERR_FATAL; |
| 8573 | } |
| 8574 | |
| 8575 | free(newsrv->ssl_ctx.ciphersuites); |
| 8576 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 8577 | return 0; |
| 8578 | } |
| 8579 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8580 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8581 | /* parse the "crl-file" server keyword */ |
| 8582 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8583 | { |
| 8584 | #ifndef X509_V_FLAG_CRL_CHECK |
| 8585 | if (err) |
| 8586 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 8587 | return ERR_ALERT | ERR_FATAL; |
| 8588 | #else |
| 8589 | if (!*args[*cur_arg + 1]) { |
| 8590 | if (err) |
| 8591 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 8592 | return ERR_ALERT | ERR_FATAL; |
| 8593 | } |
| 8594 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8595 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8596 | 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] | 8597 | else |
| 8598 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 8599 | |
| 8600 | return 0; |
| 8601 | #endif |
| 8602 | } |
| 8603 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 8604 | /* parse the "crt" server keyword */ |
| 8605 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8606 | { |
| 8607 | if (!*args[*cur_arg + 1]) { |
| 8608 | if (err) |
| 8609 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 8610 | return ERR_ALERT | ERR_FATAL; |
| 8611 | } |
| 8612 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8613 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 8614 | 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] | 8615 | else |
| 8616 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 8617 | |
| 8618 | return 0; |
| 8619 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8620 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 8621 | /* parse the "no-check-ssl" server keyword */ |
| 8622 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8623 | { |
| 8624 | newsrv->check.use_ssl = 0; |
| 8625 | free(newsrv->ssl_ctx.ciphers); |
| 8626 | newsrv->ssl_ctx.ciphers = NULL; |
| 8627 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 8628 | return 0; |
| 8629 | } |
| 8630 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 8631 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 8632 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8633 | { |
| 8634 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 8635 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 8636 | return 0; |
| 8637 | } |
| 8638 | |
| 8639 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 8640 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8641 | { |
| 8642 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 8643 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 8644 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 8645 | return 0; |
| 8646 | } |
| 8647 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 8648 | /* parse the "no-ssl" server keyword */ |
| 8649 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8650 | { |
| 8651 | newsrv->use_ssl = 0; |
| 8652 | free(newsrv->ssl_ctx.ciphers); |
| 8653 | newsrv->ssl_ctx.ciphers = NULL; |
| 8654 | return 0; |
| 8655 | } |
| 8656 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 8657 | /* parse the "allow-0rtt" server keyword */ |
| 8658 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8659 | { |
| 8660 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 8661 | return 0; |
| 8662 | } |
| 8663 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 8664 | /* parse the "no-ssl-reuse" server keyword */ |
| 8665 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8666 | { |
| 8667 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 8668 | return 0; |
| 8669 | } |
| 8670 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 8671 | /* parse the "no-tls-tickets" server keyword */ |
| 8672 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8673 | { |
| 8674 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 8675 | return 0; |
| 8676 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 8677 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 8678 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8679 | { |
| 8680 | newsrv->pp_opts |= SRV_PP_V2; |
| 8681 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 8682 | return 0; |
| 8683 | } |
| 8684 | |
| 8685 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 8686 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8687 | { |
| 8688 | newsrv->pp_opts |= SRV_PP_V2; |
| 8689 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 8690 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 8691 | return 0; |
| 8692 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 8693 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8694 | /* parse the "sni" server keyword */ |
| 8695 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8696 | { |
| 8697 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8698 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 8699 | return ERR_ALERT | ERR_FATAL; |
| 8700 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8701 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8702 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8703 | arg = args[*cur_arg + 1]; |
| 8704 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8705 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 8706 | return ERR_ALERT | ERR_FATAL; |
| 8707 | } |
| 8708 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8709 | free(newsrv->sni_expr); |
| 8710 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8711 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8712 | return 0; |
| 8713 | #endif |
| 8714 | } |
| 8715 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8716 | /* parse the "ssl" server keyword */ |
| 8717 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8718 | { |
| 8719 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8720 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 8721 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8722 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8723 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 8724 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8725 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8726 | return 0; |
| 8727 | } |
| 8728 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8729 | /* parse the "ssl-reuse" server keyword */ |
| 8730 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8731 | { |
| 8732 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 8733 | return 0; |
| 8734 | } |
| 8735 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8736 | /* parse the "tls-tickets" server keyword */ |
| 8737 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8738 | { |
| 8739 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 8740 | return 0; |
| 8741 | } |
| 8742 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8743 | /* parse the "verify" server keyword */ |
| 8744 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8745 | { |
| 8746 | if (!*args[*cur_arg + 1]) { |
| 8747 | if (err) |
| 8748 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 8749 | return ERR_ALERT | ERR_FATAL; |
| 8750 | } |
| 8751 | |
| 8752 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8753 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8754 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8755 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8756 | else { |
| 8757 | if (err) |
| 8758 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 8759 | args[*cur_arg], args[*cur_arg + 1]); |
| 8760 | return ERR_ALERT | ERR_FATAL; |
| 8761 | } |
| 8762 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8763 | return 0; |
| 8764 | } |
| 8765 | |
| 8766 | /* parse the "verifyhost" server keyword */ |
| 8767 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8768 | { |
| 8769 | if (!*args[*cur_arg + 1]) { |
| 8770 | if (err) |
| 8771 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 8772 | return ERR_ALERT | ERR_FATAL; |
| 8773 | } |
| 8774 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 8775 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8776 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 8777 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8778 | return 0; |
| 8779 | } |
| 8780 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8781 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 8782 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 8783 | struct proxy *defpx, const char *file, int line, |
| 8784 | char **err) { |
| 8785 | int i = 1; |
| 8786 | |
| 8787 | if (*(args[i]) == 0) { |
| 8788 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8789 | return -1; |
| 8790 | } |
| 8791 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8792 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8793 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8794 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 8795 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8796 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8797 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 8798 | i++; |
| 8799 | else { |
| 8800 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8801 | return -1; |
| 8802 | } |
| 8803 | } |
| 8804 | 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] | 8805 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8806 | return -1; |
| 8807 | } |
| 8808 | i++; |
| 8809 | } |
| 8810 | return 0; |
| 8811 | } |
| 8812 | |
| 8813 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 8814 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 8815 | struct proxy *defpx, const char *file, int line, |
| 8816 | char **err) { |
| 8817 | int i = 1; |
| 8818 | |
| 8819 | if (*(args[i]) == 0) { |
| 8820 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8821 | return -1; |
| 8822 | } |
| 8823 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8824 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8825 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8826 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8827 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 8828 | i++; |
| 8829 | else { |
| 8830 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8831 | return -1; |
| 8832 | } |
| 8833 | } |
| 8834 | 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] | 8835 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8836 | return -1; |
| 8837 | } |
| 8838 | i++; |
| 8839 | } |
| 8840 | return 0; |
| 8841 | } |
| 8842 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8843 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 8844 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8845 | */ |
| 8846 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 8847 | struct proxy *defpx, const char *file, int line, |
| 8848 | char **err) |
| 8849 | { |
| 8850 | char **target; |
| 8851 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8852 | 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] | 8853 | |
| 8854 | if (too_many_args(1, args, err, NULL)) |
| 8855 | return -1; |
| 8856 | |
| 8857 | if (*target) { |
| 8858 | memprintf(err, "'%s' already specified.", args[0]); |
| 8859 | return -1; |
| 8860 | } |
| 8861 | |
| 8862 | if (*(args[1]) == 0) { |
| 8863 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 8864 | return -1; |
| 8865 | } |
| 8866 | *target = strdup(args[1]); |
| 8867 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8868 | } |
| 8869 | |
| 8870 | /* parse the "ssl-mode-async" keyword in global section. |
| 8871 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8872 | */ |
| 8873 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 8874 | struct proxy *defpx, const char *file, int line, |
| 8875 | char **err) |
| 8876 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8877 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8878 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 8879 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8880 | return 0; |
| 8881 | #else |
| 8882 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 8883 | return -1; |
| 8884 | #endif |
| 8885 | } |
| 8886 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8887 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8888 | static int ssl_check_async_engine_count(void) { |
| 8889 | int err_code = 0; |
| 8890 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 8891 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8892 | 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] | 8893 | err_code = ERR_ABORT; |
| 8894 | } |
| 8895 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8896 | } |
| 8897 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8898 | /* parse the "ssl-engine" keyword in global section. |
| 8899 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8900 | */ |
| 8901 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 8902 | struct proxy *defpx, const char *file, int line, |
| 8903 | char **err) |
| 8904 | { |
| 8905 | char *algo; |
| 8906 | int ret = -1; |
| 8907 | |
| 8908 | if (*(args[1]) == 0) { |
| 8909 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 8910 | return ret; |
| 8911 | } |
| 8912 | |
| 8913 | if (*(args[2]) == 0) { |
| 8914 | /* if no list of algorithms is given, it defaults to ALL */ |
| 8915 | algo = strdup("ALL"); |
| 8916 | goto add_engine; |
| 8917 | } |
| 8918 | |
| 8919 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 8920 | if (strcmp(args[2], "algo") != 0) { |
| 8921 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 8922 | return ret; |
| 8923 | } |
| 8924 | |
| 8925 | if (*(args[3]) == 0) { |
| 8926 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 8927 | return ret; |
| 8928 | } |
| 8929 | algo = strdup(args[3]); |
| 8930 | |
| 8931 | add_engine: |
| 8932 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 8933 | openssl_engines_initialized++; |
| 8934 | ret = 0; |
| 8935 | } |
| 8936 | free(algo); |
| 8937 | return ret; |
| 8938 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8939 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8940 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8941 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 8942 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8943 | */ |
| 8944 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 8945 | struct proxy *defpx, const char *file, int line, |
| 8946 | char **err) |
| 8947 | { |
| 8948 | char **target; |
| 8949 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8950 | 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] | 8951 | |
| 8952 | if (too_many_args(1, args, err, NULL)) |
| 8953 | return -1; |
| 8954 | |
| 8955 | if (*(args[1]) == 0) { |
| 8956 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8957 | return -1; |
| 8958 | } |
| 8959 | |
| 8960 | free(*target); |
| 8961 | *target = strdup(args[1]); |
| 8962 | return 0; |
| 8963 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8964 | |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 8965 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8966 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 8967 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8968 | */ |
| 8969 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 8970 | struct proxy *defpx, const char *file, int line, |
| 8971 | char **err) |
| 8972 | { |
| 8973 | char **target; |
| 8974 | |
| 8975 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 8976 | |
| 8977 | if (too_many_args(1, args, err, NULL)) |
| 8978 | return -1; |
| 8979 | |
| 8980 | if (*(args[1]) == 0) { |
| 8981 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8982 | return -1; |
| 8983 | } |
| 8984 | |
| 8985 | free(*target); |
| 8986 | *target = strdup(args[1]); |
| 8987 | return 0; |
| 8988 | } |
| 8989 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8990 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8991 | /* parse various global tune.ssl settings consisting in positive integers. |
| 8992 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8993 | */ |
| 8994 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 8995 | struct proxy *defpx, const char *file, int line, |
| 8996 | char **err) |
| 8997 | { |
| 8998 | int *target; |
| 8999 | |
| 9000 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 9001 | target = &global.tune.sslcachesize; |
| 9002 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9003 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9004 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9005 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 9006 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 9007 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9008 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 9009 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9010 | else { |
| 9011 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 9012 | return -1; |
| 9013 | } |
| 9014 | |
| 9015 | if (too_many_args(1, args, err, NULL)) |
| 9016 | return -1; |
| 9017 | |
| 9018 | if (*(args[1]) == 0) { |
| 9019 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 9020 | return -1; |
| 9021 | } |
| 9022 | |
| 9023 | *target = atoi(args[1]); |
| 9024 | if (*target < 0) { |
| 9025 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 9026 | return -1; |
| 9027 | } |
| 9028 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9029 | } |
| 9030 | |
| 9031 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 9032 | struct proxy *defpx, const char *file, int line, |
| 9033 | char **err) |
| 9034 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9035 | int ret; |
| 9036 | |
| 9037 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 9038 | if (ret != 0) |
| 9039 | return ret; |
| 9040 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9041 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9042 | memprintf(err, "'%s' is already configured.", args[0]); |
| 9043 | return -1; |
| 9044 | } |
| 9045 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9046 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 9047 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9048 | memprintf(err, "Out of memory error."); |
| 9049 | return -1; |
| 9050 | } |
| 9051 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9052 | } |
| 9053 | |
| 9054 | /* parse "ssl.force-private-cache". |
| 9055 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9056 | */ |
| 9057 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 9058 | struct proxy *defpx, const char *file, int line, |
| 9059 | char **err) |
| 9060 | { |
| 9061 | if (too_many_args(0, args, err, NULL)) |
| 9062 | return -1; |
| 9063 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9064 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9065 | return 0; |
| 9066 | } |
| 9067 | |
| 9068 | /* parse "ssl.lifetime". |
| 9069 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9070 | */ |
| 9071 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 9072 | struct proxy *defpx, const char *file, int line, |
| 9073 | char **err) |
| 9074 | { |
| 9075 | const char *res; |
| 9076 | |
| 9077 | if (too_many_args(1, args, err, NULL)) |
| 9078 | return -1; |
| 9079 | |
| 9080 | if (*(args[1]) == 0) { |
| 9081 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 9082 | return -1; |
| 9083 | } |
| 9084 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9085 | 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] | 9086 | if (res == PARSE_TIME_OVER) { |
| 9087 | memprintf(err, "timer overflow in argument '%s' to <%s> (maximum value is 2147483647 s or ~68 years).", |
| 9088 | args[1], args[0]); |
| 9089 | return -1; |
| 9090 | } |
| 9091 | else if (res == PARSE_TIME_UNDER) { |
| 9092 | memprintf(err, "timer underflow in argument '%s' to <%s> (minimum non-null value is 1 s).", |
| 9093 | args[1], args[0]); |
| 9094 | return -1; |
| 9095 | } |
| 9096 | else if (res) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9097 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 9098 | return -1; |
| 9099 | } |
| 9100 | return 0; |
| 9101 | } |
| 9102 | |
| 9103 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 9104 | /* parse "ssl-dh-param-file". |
| 9105 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9106 | */ |
| 9107 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 9108 | struct proxy *defpx, const char *file, int line, |
| 9109 | char **err) |
| 9110 | { |
| 9111 | if (too_many_args(1, args, err, NULL)) |
| 9112 | return -1; |
| 9113 | |
| 9114 | if (*(args[1]) == 0) { |
| 9115 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 9116 | return -1; |
| 9117 | } |
| 9118 | |
| 9119 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 9120 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 9121 | return -1; |
| 9122 | } |
| 9123 | return 0; |
| 9124 | } |
| 9125 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9126 | /* parse "ssl.default-dh-param". |
| 9127 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 9128 | */ |
| 9129 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 9130 | struct proxy *defpx, const char *file, int line, |
| 9131 | char **err) |
| 9132 | { |
| 9133 | if (too_many_args(1, args, err, NULL)) |
| 9134 | return -1; |
| 9135 | |
| 9136 | if (*(args[1]) == 0) { |
| 9137 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 9138 | return -1; |
| 9139 | } |
| 9140 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9141 | global_ssl.default_dh_param = atoi(args[1]); |
| 9142 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9143 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 9144 | return -1; |
| 9145 | } |
| 9146 | return 0; |
| 9147 | } |
| 9148 | #endif |
| 9149 | |
| 9150 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9151 | /* This function is used with TLS ticket keys management. It permits to browse |
| 9152 | * each reference. The variable <getnext> must contain the current node, |
| 9153 | * <end> point to the root node. |
| 9154 | */ |
| 9155 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9156 | static inline |
| 9157 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 9158 | { |
| 9159 | struct tls_keys_ref *ref = getnext; |
| 9160 | |
| 9161 | while (1) { |
| 9162 | |
| 9163 | /* Get next list entry. */ |
| 9164 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 9165 | |
| 9166 | /* If the entry is the last of the list, return NULL. */ |
| 9167 | if (&ref->list == end) |
| 9168 | return NULL; |
| 9169 | |
| 9170 | return ref; |
| 9171 | } |
| 9172 | } |
| 9173 | |
| 9174 | static inline |
| 9175 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 9176 | { |
| 9177 | int id; |
| 9178 | char *error; |
| 9179 | |
| 9180 | /* If the reference starts by a '#', this is numeric id. */ |
| 9181 | if (reference[0] == '#') { |
| 9182 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 9183 | id = strtol(reference + 1, &error, 10); |
| 9184 | if (*error != '\0') |
| 9185 | return NULL; |
| 9186 | |
| 9187 | /* Perform the unique id lookup. */ |
| 9188 | return tlskeys_ref_lookupid(id); |
| 9189 | } |
| 9190 | |
| 9191 | /* Perform the string lookup. */ |
| 9192 | return tlskeys_ref_lookup(reference); |
| 9193 | } |
| 9194 | #endif |
| 9195 | |
| 9196 | |
| 9197 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9198 | |
| 9199 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 9200 | |
| 9201 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 9202 | return cli_io_handler_tlskeys_files(appctx); |
| 9203 | } |
| 9204 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9205 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 9206 | * (next index to be dumped), and cli.p0 (next key reference). |
| 9207 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9208 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 9209 | |
| 9210 | struct stream_interface *si = appctx->owner; |
| 9211 | |
| 9212 | switch (appctx->st2) { |
| 9213 | case STAT_ST_INIT: |
| 9214 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 9215 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9216 | * later and restart at the state "STAT_ST_INIT". |
| 9217 | */ |
| 9218 | chunk_reset(&trash); |
| 9219 | |
| 9220 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 9221 | chunk_appendf(&trash, "# id secret\n"); |
| 9222 | else |
| 9223 | chunk_appendf(&trash, "# id (file)\n"); |
| 9224 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9225 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9226 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9227 | return 0; |
| 9228 | } |
| 9229 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9230 | /* Now, we start the browsing of the references lists. |
| 9231 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 9232 | * available field of this pointer is <list>. It is used with the function |
| 9233 | * tlskeys_list_get_next() for retruning the first available entry |
| 9234 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9235 | if (appctx->ctx.cli.p0 == NULL) { |
| 9236 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 9237 | 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] | 9238 | } |
| 9239 | |
| 9240 | appctx->st2 = STAT_ST_LIST; |
| 9241 | /* fall through */ |
| 9242 | |
| 9243 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9244 | while (appctx->ctx.cli.p0) { |
| 9245 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9246 | |
| 9247 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9248 | 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] | 9249 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9250 | |
| 9251 | if (appctx->ctx.cli.i1 == 0) |
| 9252 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 9253 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9254 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9255 | int head; |
| 9256 | |
| 9257 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 9258 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9259 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9260 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9261 | |
| 9262 | chunk_reset(t2); |
| 9263 | /* 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] | 9264 | if (ref->key_size_bits == 128) { |
| 9265 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 9266 | sizeof(struct tls_sess_key_128), |
| 9267 | t2->area, t2->size); |
| 9268 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 9269 | t2->area); |
| 9270 | } |
| 9271 | else if (ref->key_size_bits == 256) { |
| 9272 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 9273 | sizeof(struct tls_sess_key_256), |
| 9274 | t2->area, t2->size); |
| 9275 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 9276 | t2->area); |
| 9277 | } |
| 9278 | else { |
| 9279 | /* This case should never happen */ |
| 9280 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 9281 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9282 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9283 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9284 | /* let's try again later from this stream. We add ourselves into |
| 9285 | * this stream's users so that it can remove us upon termination. |
| 9286 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9287 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9288 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9289 | return 0; |
| 9290 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9291 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9292 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9293 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9294 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9295 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9296 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9297 | /* let's try again later from this stream. We add ourselves into |
| 9298 | * this stream's users so that it can remove us upon termination. |
| 9299 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9300 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9301 | return 0; |
| 9302 | } |
| 9303 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9304 | 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] | 9305 | break; |
| 9306 | |
| 9307 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9308 | 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] | 9309 | } |
| 9310 | |
| 9311 | appctx->st2 = STAT_ST_FIN; |
| 9312 | /* fall through */ |
| 9313 | |
| 9314 | default: |
| 9315 | appctx->st2 = STAT_ST_FIN; |
| 9316 | return 1; |
| 9317 | } |
| 9318 | return 0; |
| 9319 | } |
| 9320 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9321 | /* 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] | 9322 | 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] | 9323 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9324 | /* no parameter, shows only file list */ |
| 9325 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9326 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9327 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 9328 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9329 | } |
| 9330 | |
| 9331 | if (args[2][0] == '*') { |
| 9332 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9333 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9334 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9335 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9336 | if (!appctx->ctx.cli.p0) |
| 9337 | 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] | 9338 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9339 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 9340 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9341 | } |
| 9342 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9343 | 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] | 9344 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9345 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9346 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9347 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9348 | /* 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] | 9349 | if (!*args[3] || !*args[4]) |
| 9350 | 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] | 9351 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9352 | ref = tlskeys_ref_lookup_ref(args[3]); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9353 | if (!ref) |
| 9354 | 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] | 9355 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9356 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9357 | if (ret < 0) |
| 9358 | 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] | 9359 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9360 | trash.data = ret; |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9361 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) |
| 9362 | 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] | 9363 | |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9364 | return cli_msg(appctx, LOG_INFO, "TLS ticket key updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9365 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9366 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9367 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9368 | 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] | 9369 | { |
| 9370 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 9371 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9372 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9373 | |
| 9374 | if (!payload) |
| 9375 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9376 | |
| 9377 | /* Expect one parameter: the new response in base64 encoding */ |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9378 | if (!*payload) |
| 9379 | 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] | 9380 | |
| 9381 | /* remove \r and \n from the payload */ |
| 9382 | for (i = 0, j = 0; payload[i]; i++) { |
| 9383 | if (payload[i] == '\r' || payload[i] == '\n') |
| 9384 | continue; |
| 9385 | payload[j++] = payload[i]; |
| 9386 | } |
| 9387 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9388 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9389 | ret = base64dec(payload, j, trash.area, trash.size); |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9390 | if (ret < 0) |
| 9391 | 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] | 9392 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9393 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9394 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9395 | if (err) |
| 9396 | return cli_dynerr(appctx, memprintf(&err, "%s.\n", err)); |
| 9397 | else |
| 9398 | return cli_err(appctx, "Failed to update OCSP response.\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9399 | } |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9400 | |
| 9401 | return cli_msg(appctx, LOG_INFO, "OCSP Response updated!\n"); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9402 | #else |
Willy Tarreau | 9d00869 | 2019-08-09 11:21:01 +0200 | [diff] [blame] | 9403 | 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] | 9404 | #endif |
| 9405 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9406 | } |
| 9407 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 9408 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9409 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 9410 | { |
| 9411 | switch (arg->type) { |
| 9412 | case ARGT_STR: |
| 9413 | smp->data.type = SMP_T_STR; |
| 9414 | smp->data.u.str = arg->data.str; |
| 9415 | return 1; |
| 9416 | case ARGT_VAR: |
| 9417 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 9418 | return 0; |
| 9419 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 9420 | return 0; |
| 9421 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 9422 | return 0; |
| 9423 | return 1; |
| 9424 | default: |
| 9425 | return 0; |
| 9426 | } |
| 9427 | } |
| 9428 | |
| 9429 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 9430 | const char *file, int line, char **err) |
| 9431 | { |
| 9432 | switch(args[0].data.sint) { |
| 9433 | case 128: |
| 9434 | case 192: |
| 9435 | case 256: |
| 9436 | break; |
| 9437 | default: |
| 9438 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 9439 | return 0; |
| 9440 | } |
| 9441 | /* Try to decode a variable. */ |
| 9442 | vars_check_arg(&args[1], NULL); |
| 9443 | vars_check_arg(&args[2], NULL); |
| 9444 | vars_check_arg(&args[3], NULL); |
| 9445 | return 1; |
| 9446 | } |
| 9447 | |
| 9448 | /* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */ |
| 9449 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 9450 | { |
| 9451 | struct sample nonce, key, aead_tag; |
| 9452 | struct buffer *smp_trash, *smp_trash_alloc; |
| 9453 | EVP_CIPHER_CTX *ctx; |
| 9454 | int dec_size, ret; |
| 9455 | |
| 9456 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 9457 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 9458 | return 0; |
| 9459 | |
| 9460 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 9461 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 9462 | return 0; |
| 9463 | |
| 9464 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 9465 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 9466 | return 0; |
| 9467 | |
| 9468 | smp_trash = get_trash_chunk(); |
| 9469 | smp_trash_alloc = alloc_trash_chunk(); |
| 9470 | if (!smp_trash_alloc) |
| 9471 | return 0; |
| 9472 | |
| 9473 | ctx = EVP_CIPHER_CTX_new(); |
| 9474 | |
| 9475 | if (!ctx) |
| 9476 | goto err; |
| 9477 | |
| 9478 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 9479 | if (dec_size < 0) |
| 9480 | goto err; |
| 9481 | smp_trash->data = dec_size; |
| 9482 | |
| 9483 | /* Set cipher type and mode */ |
| 9484 | switch(arg_p[0].data.sint) { |
| 9485 | case 128: |
| 9486 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 9487 | break; |
| 9488 | case 192: |
| 9489 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 9490 | break; |
| 9491 | case 256: |
| 9492 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 9493 | break; |
| 9494 | } |
| 9495 | |
| 9496 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 9497 | |
| 9498 | /* Initialise IV */ |
| 9499 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 9500 | goto err; |
| 9501 | |
| 9502 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 9503 | if (dec_size < 0) |
| 9504 | goto err; |
| 9505 | smp_trash->data = dec_size; |
| 9506 | |
| 9507 | /* Initialise key */ |
| 9508 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 9509 | goto err; |
| 9510 | |
| 9511 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 9512 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 9513 | goto err; |
| 9514 | |
| 9515 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 9516 | if (dec_size < 0) |
| 9517 | goto err; |
| 9518 | smp_trash_alloc->data = dec_size; |
| 9519 | dec_size = smp_trash->data; |
| 9520 | |
| 9521 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 9522 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 9523 | |
| 9524 | if (ret <= 0) |
| 9525 | goto err; |
| 9526 | |
| 9527 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 9528 | smp->data.u.str.area = smp_trash->area; |
| 9529 | smp->data.type = SMP_T_BIN; |
| 9530 | smp->flags &= ~SMP_F_CONST; |
| 9531 | free_trash_chunk(smp_trash_alloc); |
| 9532 | return 1; |
| 9533 | |
| 9534 | err: |
| 9535 | free_trash_chunk(smp_trash_alloc); |
| 9536 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9537 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9538 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9539 | |
| 9540 | /* register cli keywords */ |
| 9541 | static struct cli_kw_list cli_kws = {{ },{ |
| 9542 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9543 | { { "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] | 9544 | { { "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] | 9545 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9546 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9547 | { { NULL }, NULL, NULL, NULL } |
| 9548 | }}; |
| 9549 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9550 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9551 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9552 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9553 | * Please take care of keeping this list alphabetically sorted. |
| 9554 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 9555 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9556 | { "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] | 9557 | { "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] | 9558 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 9559 | { "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] | 9560 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9561 | { "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] | 9562 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9563 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 9564 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 9565 | { "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] | 9566 | { "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] | 9567 | { "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] | 9568 | { "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] | 9569 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9570 | { "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] | 9571 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9572 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 9573 | { "ssl_bc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 9574 | { "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] | 9575 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 9576 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9577 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 9578 | { "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] | 9579 | { "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] | 9580 | { "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] | 9581 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9582 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9583 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9584 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9585 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9586 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9587 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9588 | { "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] | 9589 | { "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] | 9590 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 9591 | { "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] | 9592 | { "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] | 9593 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9594 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9595 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9596 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9597 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9598 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9599 | { "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] | 9600 | { "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] | 9601 | { "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] | 9602 | { "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] | 9603 | { "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] | 9604 | { "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] | 9605 | { "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] | 9606 | { "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] | 9607 | { "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] | 9608 | { "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] | 9609 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9610 | { "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] | 9611 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 9612 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9613 | { "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] | 9614 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9615 | { "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] | 9616 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 9617 | { "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] | 9618 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9619 | { "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] | 9620 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9621 | { "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] | 9622 | #endif |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9623 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L |
Patrick Hemmer | 6567466 | 2019-06-04 08:13:03 -0400 | [diff] [blame] | 9624 | { "ssl_fc_client_random", smp_fetch_ssl_fc_random, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9625 | { "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] | 9626 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9627 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9628 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9629 | { "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] | 9630 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9631 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9632 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9633 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9634 | { "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] | 9635 | { NULL, NULL, 0, 0, 0 }, |
| 9636 | }}; |
| 9637 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9638 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 9639 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9640 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9641 | * Please take care of keeping this list alphabetically sorted. |
| 9642 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 9643 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 9644 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 9645 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 9646 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9647 | }}; |
| 9648 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9649 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 9650 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9651 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9652 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 9653 | * all code contributors. |
| 9654 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 9655 | * the config parser can report an appropriate error when a known keyword was |
| 9656 | * not enabled. |
| 9657 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9658 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9659 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9660 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 9661 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 9662 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9663 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9664 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 9665 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9666 | { "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] | 9667 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9668 | { "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] | 9669 | { "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] | 9670 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 9671 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 9672 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9673 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 9674 | { NULL, NULL, 0 }, |
| 9675 | }; |
| 9676 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9677 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 9678 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 9679 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9680 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9681 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 9682 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 9683 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 9684 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 9685 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 9686 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9687 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9688 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 9689 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9690 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 9691 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 9692 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 9693 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 9694 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 9695 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 9696 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 9697 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 9698 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 9699 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9700 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9701 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9702 | { "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] | 9703 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 9704 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 9705 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 9706 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9707 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9708 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 9709 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9710 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 9711 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9712 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 9713 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 9714 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 9715 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 9716 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9717 | { NULL, NULL, 0 }, |
| 9718 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9719 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9720 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 9721 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9722 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9723 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 9724 | * all code contributors. |
| 9725 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 9726 | * the config parser can report an appropriate error when a known keyword was |
| 9727 | * not enabled. |
| 9728 | */ |
| 9729 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9730 | { "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] | 9731 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9732 | { "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] | 9733 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9734 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9735 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 9736 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9737 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9738 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 9739 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9740 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 9741 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 9742 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 9743 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 9744 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 9745 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 9746 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 9747 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 9748 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 9749 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 9750 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 9751 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 9752 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 9753 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 9754 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 9755 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 9756 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 9757 | { "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] | 9758 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9759 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 9760 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 9761 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 9762 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 9763 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 9764 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 9765 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 9766 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 9767 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 9768 | { "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] | 9769 | { NULL, NULL, 0, 0 }, |
| 9770 | }}; |
| 9771 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9772 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 9773 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9774 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9775 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 9776 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 9777 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9778 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 9779 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 9780 | #ifndef OPENSSL_NO_DH |
| 9781 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 9782 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9783 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9784 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9785 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9786 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9787 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 9788 | #ifndef OPENSSL_NO_DH |
| 9789 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 9790 | #endif |
| 9791 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 9792 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 9793 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 9794 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9795 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9796 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 9797 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9798 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9799 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 9800 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 9801 | #endif |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9802 | { 0, NULL, NULL }, |
| 9803 | }}; |
| 9804 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9805 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 9806 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9807 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 9808 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 9809 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9810 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 9811 | #endif |
| 9812 | { NULL, NULL, 0, 0, 0 }, |
| 9813 | }}; |
| 9814 | |
| 9815 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 9816 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 9817 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 9818 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9819 | .snd_buf = ssl_sock_from_buf, |
| 9820 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 9821 | .subscribe = ssl_subscribe, |
| 9822 | .unsubscribe = ssl_unsubscribe, |
Olivier Houchard | 5149b59 | 2019-05-23 17:47:36 +0200 | [diff] [blame] | 9823 | .remove_xprt = ssl_remove_xprt, |
Olivier Houchard | 2e05548 | 2019-05-27 19:50:12 +0200 | [diff] [blame] | 9824 | .add_xprt = ssl_add_xprt, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9825 | .rcv_pipe = NULL, |
| 9826 | .snd_pipe = NULL, |
| 9827 | .shutr = NULL, |
| 9828 | .shutw = ssl_sock_shutw, |
| 9829 | .close = ssl_sock_close, |
| 9830 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 9831 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 9832 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 9833 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 9834 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 9835 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 9836 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9837 | }; |
| 9838 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9839 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 9840 | struct session *sess, struct stream *s, int flags) |
| 9841 | { |
| 9842 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9843 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9844 | |
| 9845 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9846 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9847 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9848 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9849 | 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] | 9850 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9851 | s->req.flags |= CF_READ_NULL; |
| 9852 | return ACT_RET_YIELD; |
| 9853 | } |
| 9854 | } |
| 9855 | return (ACT_RET_CONT); |
| 9856 | } |
| 9857 | |
| 9858 | 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) |
| 9859 | { |
| 9860 | rule->action_ptr = ssl_action_wait_for_hs; |
| 9861 | |
| 9862 | return ACT_RET_PRS_OK; |
| 9863 | } |
| 9864 | |
| 9865 | static struct action_kw_list http_req_actions = {ILH, { |
| 9866 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 9867 | { /* END */ } |
| 9868 | }}; |
| 9869 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9870 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 9871 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9872 | #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] | 9873 | |
| 9874 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 9875 | { |
| 9876 | if (ptr) { |
| 9877 | chunk_destroy(ptr); |
| 9878 | free(ptr); |
| 9879 | } |
| 9880 | } |
| 9881 | |
| 9882 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 9883 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 9884 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9885 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 9886 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 9887 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9888 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9889 | static void __ssl_sock_init(void) |
| 9890 | { |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 9891 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9892 | STACK_OF(SSL_COMP)* cm; |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 9893 | int n; |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 9894 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9895 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9896 | if (global_ssl.listen_default_ciphers) |
| 9897 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 9898 | if (global_ssl.connect_default_ciphers) |
| 9899 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Emmanuel Hocdet | 839af57 | 2019-05-14 16:27:35 +0200 | [diff] [blame] | 9900 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9901 | if (global_ssl.listen_default_ciphersuites) |
| 9902 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 9903 | if (global_ssl.connect_default_ciphersuites) |
| 9904 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9905 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 9906 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 9907 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9908 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9909 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9910 | #endif |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 9911 | #if (!defined(OPENSSL_NO_COMP) && !defined(SSL_OP_NO_COMPRESSION)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9912 | cm = SSL_COMP_get_compression_methods(); |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 9913 | n = sk_SSL_COMP_num(cm); |
| 9914 | while (n--) { |
| 9915 | (void) sk_SSL_COMP_pop(cm); |
| 9916 | } |
Ilya Shipitsin | 0590f44 | 2019-05-25 19:30:50 +0500 | [diff] [blame] | 9917 | #endif |
Ilya Shipitsin | e242f3d | 2019-05-25 03:38:14 +0500 | [diff] [blame] | 9918 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9919 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9920 | ssl_locking_init(); |
| 9921 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9922 | #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] | 9923 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 9924 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 9925 | 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] | 9926 | 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] | 9927 | 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] | 9928 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9929 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9930 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9931 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 9932 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9933 | hap_register_post_check(tlskeys_finalize_config); |
| 9934 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9935 | |
| 9936 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 9937 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 9938 | |
| 9939 | #ifndef OPENSSL_NO_DH |
| 9940 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 9941 | hap_register_post_deinit(ssl_free_dh); |
| 9942 | #endif |
| 9943 | #ifndef OPENSSL_NO_ENGINE |
| 9944 | hap_register_post_deinit(ssl_free_engines); |
| 9945 | #endif |
| 9946 | /* Load SSL string for the verbose & debug mode. */ |
| 9947 | ERR_load_SSL_strings(); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 9948 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 9949 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 9950 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 9951 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 9952 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 9953 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 9954 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 9955 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9956 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 9957 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9958 | /* Compute and register the version string */ |
| 9959 | static void ssl_register_build_options() |
| 9960 | { |
| 9961 | char *ptr = NULL; |
| 9962 | int i; |
| 9963 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9964 | memprintf(&ptr, "Built with OpenSSL version : " |
| 9965 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9966 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9967 | #else /* OPENSSL_IS_BORINGSSL */ |
| 9968 | OPENSSL_VERSION_TEXT |
| 9969 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9970 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 9971 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9972 | #endif |
| 9973 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9974 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9975 | "no (library version too old)" |
| 9976 | #elif defined(OPENSSL_NO_TLSEXT) |
| 9977 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 9978 | #else |
| 9979 | "yes" |
| 9980 | #endif |
| 9981 | "", ptr); |
| 9982 | |
| 9983 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 9984 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9985 | "yes" |
| 9986 | #else |
| 9987 | #ifdef OPENSSL_NO_TLSEXT |
| 9988 | "no (because of OPENSSL_NO_TLSEXT)" |
| 9989 | #else |
| 9990 | "no (version might be too old, 0.9.8f min needed)" |
| 9991 | #endif |
| 9992 | #endif |
| 9993 | "", ptr); |
| 9994 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 9995 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 9996 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9997 | if (methodVersions[i].option) |
| 9998 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9999 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 10000 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10001 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 10002 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 10003 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 10004 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10005 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10006 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10007 | void ssl_free_engines(void) { |
| 10008 | struct ssl_engine_list *wl, *wlb; |
| 10009 | /* free up engine list */ |
| 10010 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 10011 | ENGINE_finish(wl->e); |
| 10012 | ENGINE_free(wl->e); |
| 10013 | LIST_DEL(&wl->list); |
| 10014 | free(wl); |
| 10015 | } |
| 10016 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 10017 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 10018 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10019 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10020 | void ssl_free_dh(void) { |
| 10021 | if (local_dh_1024) { |
| 10022 | DH_free(local_dh_1024); |
| 10023 | local_dh_1024 = NULL; |
| 10024 | } |
| 10025 | if (local_dh_2048) { |
| 10026 | DH_free(local_dh_2048); |
| 10027 | local_dh_2048 = NULL; |
| 10028 | } |
| 10029 | if (local_dh_4096) { |
| 10030 | DH_free(local_dh_4096); |
| 10031 | local_dh_4096 = NULL; |
| 10032 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 10033 | if (global_dh) { |
| 10034 | DH_free(global_dh); |
| 10035 | global_dh = NULL; |
| 10036 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 10037 | } |
| 10038 | #endif |
| 10039 | |
| 10040 | __attribute__((destructor)) |
| 10041 | static void __ssl_sock_deinit(void) |
| 10042 | { |
| 10043 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 10044 | if (ssl_ctx_lru_tree) { |
| 10045 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 10046 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 10047 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10048 | #endif |
| 10049 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10050 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10051 | ERR_remove_state(0); |
| 10052 | ERR_free_strings(); |
| 10053 | |
| 10054 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 10055 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10056 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 10057 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10058 | CRYPTO_cleanup_all_ex_data(); |
| 10059 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 10060 | BIO_meth_free(ha_meth); |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 10061 | } |
| 10062 | |
| 10063 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 10064 | /* |
| 10065 | * Local variables: |
| 10066 | * c-indent-level: 8 |
| 10067 | * c-basic-offset: 8 |
| 10068 | * End: |
| 10069 | */ |