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> |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 80 | #include <proto/proto_http.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; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 160 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
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 |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 182 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
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 | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 214 | struct xprt_ops *xprt; |
| 215 | void *xprt_ctx; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 216 | int xprt_st; /* transport layer state, initialized to zero */ |
| 217 | int tmp_early_data; /* 1st byte of early data, if any */ |
| 218 | int sent_early_data; /* Amount of early data we sent so far */ |
| 219 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | DECLARE_STATIC_POOL(ssl_sock_ctx_pool, "ssl_sock_ctx_pool", sizeof(struct ssl_sock_ctx)); |
| 223 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 224 | /* Methods to implement OpenSSL BIO */ |
| 225 | static int ha_ssl_write(BIO *h, const char *buf, int num) |
| 226 | { |
| 227 | struct buffer tmpbuf; |
| 228 | struct ssl_sock_ctx *ctx; |
| 229 | int ret; |
| 230 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 231 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000 |
Olivier Houchard | 66a7b33 | 2019-04-18 15:58:15 +0200 | [diff] [blame] | 232 | ctx = h->ptr; |
| 233 | #else |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 234 | ctx = BIO_get_data(h); |
Olivier Houchard | 66a7b33 | 2019-04-18 15:58:15 +0200 | [diff] [blame] | 235 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 236 | tmpbuf.size = num; |
| 237 | tmpbuf.area = (void *)(uintptr_t)buf; |
| 238 | tmpbuf.data = num; |
| 239 | tmpbuf.head = 0; |
| 240 | 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] | 241 | 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] | 242 | BIO_set_retry_write(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 243 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 244 | } else if (ret == 0) |
| 245 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 246 | return ret; |
| 247 | } |
| 248 | |
| 249 | static int ha_ssl_gets(BIO *h, char *buf, int size) |
| 250 | { |
| 251 | |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static int ha_ssl_puts(BIO *h, const char *str) |
| 256 | { |
| 257 | |
| 258 | return ha_ssl_write(h, str, strlen(str)); |
| 259 | } |
| 260 | |
| 261 | static int ha_ssl_read(BIO *h, char *buf, int size) |
| 262 | { |
| 263 | struct buffer tmpbuf; |
| 264 | struct ssl_sock_ctx *ctx; |
| 265 | int ret; |
| 266 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 267 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000 |
Olivier Houchard | 66a7b33 | 2019-04-18 15:58:15 +0200 | [diff] [blame] | 268 | ctx = h->ptr; |
| 269 | #else |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 270 | ctx = BIO_get_data(h); |
Olivier Houchard | 66a7b33 | 2019-04-18 15:58:15 +0200 | [diff] [blame] | 271 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 272 | tmpbuf.size = size; |
| 273 | tmpbuf.area = buf; |
| 274 | tmpbuf.data = 0; |
| 275 | tmpbuf.head = 0; |
| 276 | 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] | 277 | 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] | 278 | BIO_set_retry_read(h); |
Olivier Houchard | a28454e | 2019-04-24 12:04:36 +0200 | [diff] [blame] | 279 | ret = -1; |
Olivier Houchard | b51937e | 2019-05-01 17:24:36 +0200 | [diff] [blame] | 280 | } else if (ret == 0) |
| 281 | BIO_clear_retry_flags(h); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 282 | |
| 283 | return ret; |
| 284 | } |
| 285 | |
| 286 | static long ha_ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2) |
| 287 | { |
| 288 | int ret = 0; |
| 289 | switch (cmd) { |
| 290 | case BIO_CTRL_DUP: |
| 291 | case BIO_CTRL_FLUSH: |
| 292 | ret = 1; |
| 293 | break; |
| 294 | } |
| 295 | return ret; |
| 296 | } |
| 297 | |
| 298 | static int ha_ssl_new(BIO *h) |
| 299 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 300 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Olivier Houchard | 66a7b33 | 2019-04-18 15:58:15 +0200 | [diff] [blame] | 301 | h->init = 1; |
| 302 | h->ptr = NULL; |
| 303 | #else |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 304 | BIO_set_init(h, 1); |
| 305 | BIO_set_data(h, NULL); |
Olivier Houchard | 66a7b33 | 2019-04-18 15:58:15 +0200 | [diff] [blame] | 306 | #endif |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 307 | BIO_clear_flags(h, ~0); |
| 308 | return 1; |
| 309 | } |
| 310 | |
| 311 | static int ha_ssl_free(BIO *data) |
| 312 | { |
| 313 | |
| 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 318 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 319 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 320 | static HA_RWLOCK_T *ssl_rwlocks; |
| 321 | |
| 322 | |
| 323 | unsigned long ssl_id_function(void) |
| 324 | { |
| 325 | return (unsigned long)tid; |
| 326 | } |
| 327 | |
| 328 | void ssl_locking_function(int mode, int n, const char * file, int line) |
| 329 | { |
| 330 | if (mode & CRYPTO_LOCK) { |
| 331 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 332 | HA_RWLOCK_RDLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 333 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 334 | HA_RWLOCK_WRLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 335 | } |
| 336 | else { |
| 337 | if (mode & CRYPTO_READ) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 338 | HA_RWLOCK_RDUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 339 | else |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 340 | HA_RWLOCK_WRUNLOCK(SSL_LOCK, &ssl_rwlocks[n]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 341 | } |
| 342 | } |
| 343 | |
| 344 | static int ssl_locking_init(void) |
| 345 | { |
| 346 | int i; |
| 347 | |
| 348 | ssl_rwlocks = malloc(sizeof(HA_RWLOCK_T)*CRYPTO_num_locks()); |
| 349 | if (!ssl_rwlocks) |
| 350 | return -1; |
| 351 | |
| 352 | for (i = 0 ; i < CRYPTO_num_locks() ; i++) |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 353 | HA_RWLOCK_INIT(&ssl_rwlocks[i]); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 354 | |
| 355 | CRYPTO_set_id_callback(ssl_id_function); |
| 356 | CRYPTO_set_locking_callback(ssl_locking_function); |
| 357 | |
| 358 | return 0; |
| 359 | } |
Christopher Faulet | 9dcf9b6 | 2017-11-13 10:34:01 +0100 | [diff] [blame] | 360 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 361 | #endif |
| 362 | |
| 363 | |
| 364 | |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 365 | /* This memory pool is used for capturing clienthello parameters. */ |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 366 | struct ssl_capture { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 367 | unsigned long long int xxh64; |
| 368 | unsigned char ciphersuite_len; |
| 369 | char ciphersuite[0]; |
| 370 | }; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 371 | struct pool_head *pool_head_ssl_capture = NULL; |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 372 | static int ssl_capture_ptr_index = -1; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 373 | static int ssl_app_data_index = -1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 374 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 375 | static int ssl_pkey_info_index = -1; |
| 376 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 377 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 378 | struct list tlskeys_reference = LIST_HEAD_INIT(tlskeys_reference); |
| 379 | #endif |
| 380 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 381 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 382 | static unsigned int openssl_engines_initialized; |
| 383 | struct list openssl_engines = LIST_HEAD_INIT(openssl_engines); |
| 384 | struct ssl_engine_list { |
| 385 | struct list list; |
| 386 | ENGINE *e; |
| 387 | }; |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 388 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 389 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 390 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 391 | static int ssl_dh_ptr_index = -1; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 392 | static DH *global_dh = NULL; |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 393 | static DH *local_dh_1024 = NULL; |
| 394 | static DH *local_dh_2048 = NULL; |
| 395 | static DH *local_dh_4096 = NULL; |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 396 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen); |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 397 | #endif /* OPENSSL_NO_DH */ |
| 398 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 399 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 400 | /* X509V3 Extensions that will be added on generated certificates */ |
| 401 | #define X509V3_EXT_SIZE 5 |
| 402 | static char *x509v3_ext_names[X509V3_EXT_SIZE] = { |
| 403 | "basicConstraints", |
| 404 | "nsComment", |
| 405 | "subjectKeyIdentifier", |
| 406 | "authorityKeyIdentifier", |
| 407 | "keyUsage", |
| 408 | }; |
| 409 | static char *x509v3_ext_values[X509V3_EXT_SIZE] = { |
| 410 | "CA:FALSE", |
| 411 | "\"OpenSSL Generated Certificate\"", |
| 412 | "hash", |
| 413 | "keyid,issuer:always", |
| 414 | "nonRepudiation,digitalSignature,keyEncipherment" |
| 415 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 416 | /* LRU cache to store generated certificate */ |
| 417 | static struct lru64_head *ssl_ctx_lru_tree = NULL; |
| 418 | static unsigned int ssl_ctx_lru_seed = 0; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 419 | static unsigned int ssl_ctx_serial; |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 420 | __decl_rwlock(ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 421 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 422 | #endif // SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 423 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 424 | static struct ssl_bind_kw ssl_bind_kws[]; |
| 425 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 426 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 427 | /* The order here matters for picking a default context, |
| 428 | * keep the most common keytype at the bottom of the list |
| 429 | */ |
| 430 | const char *SSL_SOCK_KEYTYPE_NAMES[] = { |
| 431 | "dsa", |
| 432 | "ecdsa", |
| 433 | "rsa" |
| 434 | }; |
| 435 | #define SSL_SOCK_NUM_KEYTYPES 3 |
Willy Tarreau | 30da7ad | 2015-12-14 11:28:33 +0100 | [diff] [blame] | 436 | #else |
| 437 | #define SSL_SOCK_NUM_KEYTYPES 1 |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 438 | #endif |
| 439 | |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 440 | static struct shared_context *ssl_shctx = NULL; /* ssl shared session cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 441 | static struct eb_root *sh_ssl_sess_tree; /* ssl shared session tree */ |
| 442 | |
| 443 | #define sh_ssl_sess_tree_delete(s) ebmb_delete(&(s)->key); |
| 444 | |
| 445 | #define sh_ssl_sess_tree_insert(s) (struct sh_ssl_sess_hdr *)ebmb_insert(sh_ssl_sess_tree, \ |
| 446 | &(s)->key, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 447 | |
| 448 | #define sh_ssl_sess_tree_lookup(k) (struct sh_ssl_sess_hdr *)ebmb_lookup(sh_ssl_sess_tree, \ |
| 449 | (k), SSL_MAX_SSL_SESSION_ID_LENGTH); |
William Lallemand | 3f85c9a | 2017-10-09 16:30:50 +0200 | [diff] [blame] | 450 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 451 | /* |
| 452 | * This function gives the detail of the SSL error. It is used only |
| 453 | * if the debug mode and the verbose mode are activated. It dump all |
| 454 | * the SSL error until the stack was empty. |
| 455 | */ |
| 456 | static forceinline void ssl_sock_dump_errors(struct connection *conn) |
| 457 | { |
| 458 | unsigned long ret; |
| 459 | |
| 460 | if (unlikely(global.mode & MODE_DEBUG)) { |
| 461 | while(1) { |
| 462 | ret = ERR_get_error(); |
| 463 | if (ret == 0) |
| 464 | return; |
| 465 | fprintf(stderr, "fd[%04x] OpenSSL error[0x%lx] %s: %s\n", |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 466 | (unsigned short)conn->handle.fd, ret, |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 467 | ERR_func_error_string(ret), ERR_reason_error_string(ret)); |
| 468 | } |
| 469 | } |
| 470 | } |
| 471 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 472 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 473 | /* |
| 474 | * struct alignment works here such that the key.key is the same as key_data |
| 475 | * Do not change the placement of key_data |
| 476 | */ |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 477 | struct certificate_ocsp { |
| 478 | struct ebmb_node key; |
| 479 | unsigned char key_data[OCSP_MAX_CERTID_ASN1_LENGTH]; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 480 | struct buffer response; |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 481 | long expire; |
| 482 | }; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 483 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 484 | struct ocsp_cbk_arg { |
| 485 | int is_single; |
| 486 | int single_kt; |
| 487 | union { |
| 488 | struct certificate_ocsp *s_ocsp; |
| 489 | /* |
| 490 | * m_ocsp will have multiple entries dependent on key type |
| 491 | * Entry 0 - DSA |
| 492 | * Entry 1 - ECDSA |
| 493 | * Entry 2 - RSA |
| 494 | */ |
| 495 | struct certificate_ocsp *m_ocsp[SSL_SOCK_NUM_KEYTYPES]; |
| 496 | }; |
| 497 | }; |
| 498 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 499 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 500 | static int ssl_init_single_engine(const char *engine_id, const char *def_algorithms) |
| 501 | { |
| 502 | int err_code = ERR_ABORT; |
| 503 | ENGINE *engine; |
| 504 | struct ssl_engine_list *el; |
| 505 | |
| 506 | /* grab the structural reference to the engine */ |
| 507 | engine = ENGINE_by_id(engine_id); |
| 508 | if (engine == NULL) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 509 | 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] | 510 | goto fail_get; |
| 511 | } |
| 512 | |
| 513 | if (!ENGINE_init(engine)) { |
| 514 | /* the engine couldn't initialise, release it */ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 515 | ha_alert("ssl-engine %s: failed to initialize\n", engine_id); |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 516 | goto fail_init; |
| 517 | } |
| 518 | |
| 519 | if (ENGINE_set_default_string(engine, def_algorithms) == 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 520 | 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] | 521 | goto fail_set_method; |
| 522 | } |
| 523 | |
| 524 | el = calloc(1, sizeof(*el)); |
| 525 | el->e = engine; |
| 526 | LIST_ADD(&openssl_engines, &el->list); |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 527 | nb_engines++; |
| 528 | if (global_ssl.async) |
| 529 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 530 | return 0; |
| 531 | |
| 532 | fail_set_method: |
| 533 | /* release the functional reference from ENGINE_init() */ |
| 534 | ENGINE_finish(engine); |
| 535 | |
| 536 | fail_init: |
| 537 | /* release the structural reference from ENGINE_by_id() */ |
| 538 | ENGINE_free(engine); |
| 539 | |
| 540 | fail_get: |
| 541 | return err_code; |
| 542 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 543 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 544 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 545 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 546 | /* |
| 547 | * openssl async fd handler |
| 548 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 549 | void ssl_async_fd_handler(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 550 | { |
| 551 | struct connection *conn = fdtab[fd].owner; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 552 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 553 | /* fd is an async enfine fd, we must stop |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 554 | * to poll this fd until it is requested |
| 555 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 556 | fd_stop_recv(fd); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 557 | fd_cant_recv(fd); |
| 558 | |
| 559 | /* crypto engine is available, let's notify the associated |
| 560 | * connection that it can pursue its processing. |
| 561 | */ |
Emeric Brun | bbc1654 | 2017-06-02 15:54:06 +0000 | [diff] [blame] | 562 | __conn_sock_want_recv(conn); |
| 563 | __conn_sock_want_send(conn); |
| 564 | conn_update_sock_polling(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 567 | /* |
| 568 | * openssl async delayed SSL_free handler |
| 569 | */ |
Emeric Brun | d0e095c | 2019-04-19 17:15:28 +0200 | [diff] [blame] | 570 | void ssl_async_fd_free(int fd) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 571 | { |
| 572 | SSL *ssl = fdtab[fd].owner; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 573 | OSSL_ASYNC_FD all_fd[32]; |
| 574 | size_t num_all_fds = 0; |
| 575 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 576 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 577 | /* We suppose that the async job for a same SSL * |
| 578 | * are serialized. So if we are awake it is |
| 579 | * because the running job has just finished |
| 580 | * and we can remove all async fds safely |
| 581 | */ |
| 582 | SSL_get_all_async_fds(ssl, NULL, &num_all_fds); |
| 583 | if (num_all_fds > 32) { |
| 584 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 585 | return; |
| 586 | } |
| 587 | |
| 588 | SSL_get_all_async_fds(ssl, all_fd, &num_all_fds); |
| 589 | for (i=0 ; i < num_all_fds ; i++) |
| 590 | fd_remove(all_fd[i]); |
| 591 | |
| 592 | /* 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] | 593 | SSL_free(ssl); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 594 | _HA_ATOMIC_SUB(&sslconns, 1); |
| 595 | _HA_ATOMIC_SUB(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 596 | } |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 597 | /* |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 598 | * function used to manage a returned SSL_ERROR_WANT_ASYNC |
| 599 | * and enable/disable polling for async fds |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 600 | */ |
Willy Tarreau | 0e492e2 | 2019-04-15 21:25:03 +0200 | [diff] [blame] | 601 | static inline void ssl_async_process_fds(struct connection *conn, SSL *ssl) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 602 | { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 603 | OSSL_ASYNC_FD add_fd[32]; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 604 | OSSL_ASYNC_FD del_fd[32]; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 605 | size_t num_add_fds = 0; |
| 606 | size_t num_del_fds = 0; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 607 | int i; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 608 | |
| 609 | SSL_get_changed_async_fds(ssl, NULL, &num_add_fds, NULL, |
| 610 | &num_del_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 611 | if (num_add_fds > 32 || num_del_fds > 32) { |
| 612 | 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] | 613 | return; |
| 614 | } |
| 615 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 616 | 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] | 617 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 618 | /* We remove unused fds from the fdtab */ |
| 619 | for (i=0 ; i < num_del_fds ; i++) |
| 620 | fd_remove(del_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 621 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 622 | /* We add new fds to the fdtab */ |
| 623 | for (i=0 ; i < num_add_fds ; i++) { |
Willy Tarreau | a9786b6 | 2018-01-25 07:22:13 +0100 | [diff] [blame] | 624 | fd_insert(add_fd[i], conn, ssl_async_fd_handler, tid_bit); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 625 | } |
| 626 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 627 | num_add_fds = 0; |
| 628 | SSL_get_all_async_fds(ssl, NULL, &num_add_fds); |
| 629 | if (num_add_fds > 32) { |
| 630 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 631 | return; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 632 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 633 | |
| 634 | /* We activate the polling for all known async fds */ |
| 635 | SSL_get_all_async_fds(ssl, add_fd, &num_add_fds); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 636 | for (i=0 ; i < num_add_fds ; i++) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 637 | fd_want_recv(add_fd[i]); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 638 | /* To ensure that the fd cache won't be used |
| 639 | * We'll prefer to catch a real RD event |
| 640 | * because handling an EAGAIN on this fd will |
| 641 | * result in a context switch and also |
| 642 | * some engines uses a fd in blocking mode. |
| 643 | */ |
| 644 | fd_cant_recv(add_fd[i]); |
| 645 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 646 | |
| 647 | /* We must also prevent the conn_handler |
| 648 | * to be called until a read event was |
| 649 | * polled on an async fd |
| 650 | */ |
| 651 | __conn_sock_stop_both(conn); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 652 | } |
| 653 | #endif |
| 654 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 655 | /* |
| 656 | * This function returns the number of seconds elapsed |
| 657 | * since the Epoch, 1970-01-01 00:00:00 +0000 (UTC) and the |
| 658 | * date presented un ASN1_GENERALIZEDTIME. |
| 659 | * |
| 660 | * In parsing error case, it returns -1. |
| 661 | */ |
| 662 | static long asn1_generalizedtime_to_epoch(ASN1_GENERALIZEDTIME *d) |
| 663 | { |
| 664 | long epoch; |
| 665 | char *p, *end; |
| 666 | const unsigned short month_offset[12] = { |
| 667 | 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 |
| 668 | }; |
| 669 | int year, month; |
| 670 | |
| 671 | if (!d || (d->type != V_ASN1_GENERALIZEDTIME)) return -1; |
| 672 | |
| 673 | p = (char *)d->data; |
| 674 | end = p + d->length; |
| 675 | |
| 676 | if (end - p < 4) return -1; |
| 677 | year = 1000 * (p[0] - '0') + 100 * (p[1] - '0') + 10 * (p[2] - '0') + p[3] - '0'; |
| 678 | p += 4; |
| 679 | if (end - p < 2) return -1; |
| 680 | month = 10 * (p[0] - '0') + p[1] - '0'; |
| 681 | if (month < 1 || month > 12) return -1; |
| 682 | /* Compute the number of seconds since 1 jan 1970 and the beginning of current month |
| 683 | We consider leap years and the current month (<marsh or not) */ |
| 684 | epoch = ( ((year - 1970) * 365) |
| 685 | + ((year - (month < 3)) / 4 - (year - (month < 3)) / 100 + (year - (month < 3)) / 400) |
| 686 | - ((1970 - 1) / 4 - (1970 - 1) / 100 + (1970 - 1) / 400) |
| 687 | + month_offset[month-1] |
| 688 | ) * 24 * 60 * 60; |
| 689 | p += 2; |
| 690 | if (end - p < 2) return -1; |
| 691 | /* Add the number of seconds of completed days of current month */ |
| 692 | epoch += (10 * (p[0] - '0') + p[1] - '0' - 1) * 24 * 60 * 60; |
| 693 | p += 2; |
| 694 | if (end - p < 2) return -1; |
| 695 | /* Add the completed hours of the current day */ |
| 696 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60 * 60; |
| 697 | p += 2; |
| 698 | if (end - p < 2) return -1; |
| 699 | /* Add the completed minutes of the current hour */ |
| 700 | epoch += (10 * (p[0] - '0') + p[1] - '0') * 60; |
| 701 | p += 2; |
| 702 | if (p == end) return -1; |
| 703 | /* Test if there is available seconds */ |
| 704 | if (p[0] < '0' || p[0] > '9') |
| 705 | goto nosec; |
| 706 | if (end - p < 2) return -1; |
| 707 | /* Add the seconds of the current minute */ |
| 708 | epoch += 10 * (p[0] - '0') + p[1] - '0'; |
| 709 | p += 2; |
| 710 | if (p == end) return -1; |
| 711 | /* Ignore seconds float part if present */ |
| 712 | if (p[0] == '.') { |
| 713 | do { |
| 714 | if (++p == end) return -1; |
| 715 | } while (p[0] >= '0' && p[0] <= '9'); |
| 716 | } |
| 717 | |
| 718 | nosec: |
| 719 | if (p[0] == 'Z') { |
| 720 | if (end - p != 1) return -1; |
| 721 | return epoch; |
| 722 | } |
| 723 | else if (p[0] == '+') { |
| 724 | if (end - p != 5) return -1; |
| 725 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 726 | 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] | 727 | } |
| 728 | else if (p[0] == '-') { |
| 729 | if (end - p != 5) return -1; |
| 730 | /* Apply timezone offset */ |
Frederik Deweerdt | 953917a | 2017-10-16 07:37:31 -0700 | [diff] [blame] | 731 | 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] | 732 | } |
| 733 | |
| 734 | return -1; |
| 735 | } |
| 736 | |
Emeric Brun | 1d3865b | 2014-06-20 15:37:32 +0200 | [diff] [blame] | 737 | static struct eb_root cert_ocsp_tree = EB_ROOT_UNIQUE; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 738 | |
| 739 | /* This function starts to check if the OCSP response (in DER format) contained |
| 740 | * in chunk 'ocsp_response' is valid (else exits on error). |
| 741 | * If 'cid' is not NULL, it will be compared to the OCSP certificate ID |
| 742 | * contained in the OCSP Response and exits on error if no match. |
| 743 | * If it's a valid OCSP Response: |
| 744 | * If 'ocsp' is not NULL, the chunk is copied in the OCSP response's container |
| 745 | * pointed by 'ocsp'. |
| 746 | * If 'ocsp' is NULL, the function looks up into the OCSP response's |
| 747 | * containers tree (using as index the ASN1 form of the OCSP Certificate ID extracted |
| 748 | * from the response) and exits on error if not found. Finally, If an OCSP response is |
| 749 | * already present in the container, it will be overwritten. |
| 750 | * |
| 751 | * Note: OCSP response containing more than one OCSP Single response is not |
| 752 | * considered valid. |
| 753 | * |
| 754 | * Returns 0 on success, 1 in error case. |
| 755 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 756 | static int ssl_sock_load_ocsp_response(struct buffer *ocsp_response, |
| 757 | struct certificate_ocsp *ocsp, |
| 758 | OCSP_CERTID *cid, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 759 | { |
| 760 | OCSP_RESPONSE *resp; |
| 761 | OCSP_BASICRESP *bs = NULL; |
| 762 | OCSP_SINGLERESP *sr; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 763 | OCSP_CERTID *id; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 764 | unsigned char *p = (unsigned char *) ocsp_response->area; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 765 | int rc , count_sr; |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 766 | ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd = NULL; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 767 | int reason; |
| 768 | int ret = 1; |
| 769 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 770 | resp = d2i_OCSP_RESPONSE(NULL, (const unsigned char **)&p, |
| 771 | ocsp_response->data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 772 | if (!resp) { |
| 773 | memprintf(err, "Unable to parse OCSP response"); |
| 774 | goto out; |
| 775 | } |
| 776 | |
| 777 | rc = OCSP_response_status(resp); |
| 778 | if (rc != OCSP_RESPONSE_STATUS_SUCCESSFUL) { |
| 779 | memprintf(err, "OCSP response status not successful"); |
| 780 | goto out; |
| 781 | } |
| 782 | |
| 783 | bs = OCSP_response_get1_basic(resp); |
| 784 | if (!bs) { |
| 785 | memprintf(err, "Failed to get basic response from OCSP Response"); |
| 786 | goto out; |
| 787 | } |
| 788 | |
| 789 | count_sr = OCSP_resp_count(bs); |
| 790 | if (count_sr > 1) { |
| 791 | memprintf(err, "OCSP response ignored because contains multiple single responses (%d)", count_sr); |
| 792 | goto out; |
| 793 | } |
| 794 | |
| 795 | sr = OCSP_resp_get0(bs, 0); |
| 796 | if (!sr) { |
| 797 | memprintf(err, "Failed to get OCSP single response"); |
| 798 | goto out; |
| 799 | } |
| 800 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 801 | id = (OCSP_CERTID*)OCSP_SINGLERESP_get0_id(sr); |
| 802 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 803 | rc = OCSP_single_get0_status(sr, &reason, &revtime, &thisupd, &nextupd); |
Emmanuel Hocdet | ef60705 | 2017-10-24 14:57:16 +0200 | [diff] [blame] | 804 | if (rc != V_OCSP_CERTSTATUS_GOOD && rc != V_OCSP_CERTSTATUS_REVOKED) { |
Emmanuel Hocdet | 872085c | 2017-10-10 15:18:52 +0200 | [diff] [blame] | 805 | memprintf(err, "OCSP single response: certificate status is unknown"); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 806 | goto out; |
| 807 | } |
| 808 | |
Emeric Brun | 13a6b48 | 2014-06-20 15:44:34 +0200 | [diff] [blame] | 809 | if (!nextupd) { |
| 810 | memprintf(err, "OCSP single response: missing nextupdate"); |
| 811 | goto out; |
| 812 | } |
| 813 | |
Emeric Brun | c8b27b6 | 2014-06-19 14:16:17 +0200 | [diff] [blame] | 814 | rc = OCSP_check_validity(thisupd, nextupd, OCSP_MAX_RESPONSE_TIME_SKEW, -1); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 815 | if (!rc) { |
| 816 | memprintf(err, "OCSP single response: no longer valid."); |
| 817 | goto out; |
| 818 | } |
| 819 | |
| 820 | if (cid) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 821 | if (OCSP_id_cmp(id, cid)) { |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 822 | memprintf(err, "OCSP single response: Certificate ID does not match certificate and issuer"); |
| 823 | goto out; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | if (!ocsp) { |
| 828 | unsigned char key[OCSP_MAX_CERTID_ASN1_LENGTH]; |
| 829 | unsigned char *p; |
| 830 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 831 | rc = i2d_OCSP_CERTID(id, NULL); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 832 | if (!rc) { |
| 833 | memprintf(err, "OCSP single response: Unable to encode Certificate ID"); |
| 834 | goto out; |
| 835 | } |
| 836 | |
| 837 | if (rc > OCSP_MAX_CERTID_ASN1_LENGTH) { |
| 838 | memprintf(err, "OCSP single response: Certificate ID too long"); |
| 839 | goto out; |
| 840 | } |
| 841 | |
| 842 | p = key; |
| 843 | memset(key, 0, OCSP_MAX_CERTID_ASN1_LENGTH); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 844 | i2d_OCSP_CERTID(id, &p); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 845 | ocsp = (struct certificate_ocsp *)ebmb_lookup(&cert_ocsp_tree, key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 846 | if (!ocsp) { |
| 847 | memprintf(err, "OCSP single response: Certificate ID does not match any certificate or issuer"); |
| 848 | goto out; |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | /* According to comments on "chunk_dup", the |
| 853 | previous chunk buffer will be freed */ |
| 854 | if (!chunk_dup(&ocsp->response, ocsp_response)) { |
| 855 | memprintf(err, "OCSP response: Memory allocation error"); |
| 856 | goto out; |
| 857 | } |
| 858 | |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 859 | ocsp->expire = asn1_generalizedtime_to_epoch(nextupd) - OCSP_MAX_RESPONSE_TIME_SKEW; |
| 860 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 861 | ret = 0; |
| 862 | out: |
Janusz Dziemidowicz | 8d71049 | 2017-03-08 16:59:41 +0100 | [diff] [blame] | 863 | ERR_clear_error(); |
| 864 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 865 | if (bs) |
| 866 | OCSP_BASICRESP_free(bs); |
| 867 | |
| 868 | if (resp) |
| 869 | OCSP_RESPONSE_free(resp); |
| 870 | |
| 871 | return ret; |
| 872 | } |
| 873 | /* |
| 874 | * External function use to update the OCSP response in the OCSP response's |
| 875 | * containers tree. The chunk 'ocsp_response' must contain the OCSP response |
| 876 | * to update in DER format. |
| 877 | * |
| 878 | * Returns 0 on success, 1 in error case. |
| 879 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 880 | int ssl_sock_update_ocsp_response(struct buffer *ocsp_response, char **err) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 881 | { |
| 882 | return ssl_sock_load_ocsp_response(ocsp_response, NULL, NULL, err); |
| 883 | } |
| 884 | |
| 885 | /* |
| 886 | * This function load the OCSP Resonse in DER format contained in file at |
| 887 | * path 'ocsp_path' and call 'ssl_sock_load_ocsp_response' |
| 888 | * |
| 889 | * Returns 0 on success, 1 in error case. |
| 890 | */ |
| 891 | static int ssl_sock_load_ocsp_response_from_file(const char *ocsp_path, struct certificate_ocsp *ocsp, OCSP_CERTID *cid, char **err) |
| 892 | { |
| 893 | int fd = -1; |
| 894 | int r = 0; |
| 895 | int ret = 1; |
| 896 | |
| 897 | fd = open(ocsp_path, O_RDONLY); |
| 898 | if (fd == -1) { |
| 899 | memprintf(err, "Error opening OCSP response file"); |
| 900 | goto end; |
| 901 | } |
| 902 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 903 | trash.data = 0; |
| 904 | while (trash.data < trash.size) { |
| 905 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 906 | if (r < 0) { |
| 907 | if (errno == EINTR) |
| 908 | continue; |
| 909 | |
| 910 | memprintf(err, "Error reading OCSP response from file"); |
| 911 | goto end; |
| 912 | } |
| 913 | else if (r == 0) { |
| 914 | break; |
| 915 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 916 | trash.data += r; |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | close(fd); |
| 920 | fd = -1; |
| 921 | |
| 922 | ret = ssl_sock_load_ocsp_response(&trash, ocsp, cid, err); |
| 923 | end: |
| 924 | if (fd != -1) |
| 925 | close(fd); |
| 926 | |
| 927 | return ret; |
| 928 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 929 | #endif |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 930 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 931 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 932 | 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) |
| 933 | { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 934 | struct tls_keys_ref *ref; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 935 | union tls_sess_key *keys; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 936 | struct connection *conn; |
| 937 | int head; |
| 938 | int i; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 939 | int ret = -1; /* error by default */ |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 940 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 941 | conn = SSL_get_ex_data(s, ssl_app_data_index); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 942 | ref = __objt_listener(conn->target)->bind_conf->keys_ref; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 943 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 944 | |
| 945 | keys = ref->tlskeys; |
| 946 | head = ref->tls_ticket_enc_index; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 947 | |
| 948 | if (enc) { |
| 949 | memcpy(key_name, keys[head].name, 16); |
| 950 | |
| 951 | if(!RAND_pseudo_bytes(iv, EVP_MAX_IV_LENGTH)) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 952 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 953 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 954 | if (ref->key_size_bits == 128) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 955 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 956 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[head].key_128.aes_key, iv)) |
| 957 | goto end; |
| 958 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 959 | 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] | 960 | ret = 1; |
| 961 | } |
| 962 | else if (ref->key_size_bits == 256 ) { |
| 963 | |
| 964 | if(!EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[head].key_256.aes_key, iv)) |
| 965 | goto end; |
| 966 | |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 967 | 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] | 968 | ret = 1; |
| 969 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 970 | } else { |
| 971 | for (i = 0; i < TLS_TICKETS_NO; i++) { |
| 972 | if (!memcmp(key_name, keys[(head + i) % TLS_TICKETS_NO].name, 16)) |
| 973 | goto found; |
| 974 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 975 | ret = 0; |
| 976 | goto end; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 977 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 978 | found: |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 979 | if (ref->key_size_bits == 128) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 980 | 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] | 981 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_128_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_128.aes_key, iv)) |
| 982 | goto end; |
| 983 | /* 2 for key renewal, 1 if current key is still valid */ |
| 984 | ret = i ? 2 : 1; |
| 985 | } |
| 986 | else if (ref->key_size_bits == 256) { |
Willy Tarreau | 9356dac | 2019-05-10 09:22:53 +0200 | [diff] [blame] | 987 | 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] | 988 | if(!EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, keys[(head + i) % TLS_TICKETS_NO].key_256.aes_key, iv)) |
| 989 | goto end; |
| 990 | /* 2 for key renewal, 1 if current key is still valid */ |
| 991 | ret = i ? 2 : 1; |
| 992 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 993 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 994 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 995 | end: |
| 996 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 997 | return ret; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | struct tls_keys_ref *tlskeys_ref_lookup(const char *filename) |
| 1001 | { |
| 1002 | struct tls_keys_ref *ref; |
| 1003 | |
| 1004 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1005 | if (ref->filename && strcmp(filename, ref->filename) == 0) |
| 1006 | return ref; |
| 1007 | return NULL; |
| 1008 | } |
| 1009 | |
| 1010 | struct tls_keys_ref *tlskeys_ref_lookupid(int unique_id) |
| 1011 | { |
| 1012 | struct tls_keys_ref *ref; |
| 1013 | |
| 1014 | list_for_each_entry(ref, &tlskeys_reference, list) |
| 1015 | if (ref->unique_id == unique_id) |
| 1016 | return ref; |
| 1017 | return NULL; |
| 1018 | } |
| 1019 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1020 | /* Update the key into ref: if keysize doesnt |
| 1021 | * match existing ones, this function returns -1 |
| 1022 | * else it returns 0 on success. |
| 1023 | */ |
| 1024 | int ssl_sock_update_tlskey_ref(struct tls_keys_ref *ref, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1025 | struct buffer *tlskey) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1026 | { |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1027 | if (ref->key_size_bits == 128) { |
| 1028 | if (tlskey->data != sizeof(struct tls_sess_key_128)) |
| 1029 | return -1; |
| 1030 | } |
| 1031 | else if (ref->key_size_bits == 256) { |
| 1032 | if (tlskey->data != sizeof(struct tls_sess_key_256)) |
| 1033 | return -1; |
| 1034 | } |
| 1035 | else |
| 1036 | return -1; |
| 1037 | |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1038 | HA_RWLOCK_WRLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1039 | memcpy((char *) (ref->tlskeys + ((ref->tls_ticket_enc_index + 2) % TLS_TICKETS_NO)), |
| 1040 | tlskey->area, tlskey->data); |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1041 | ref->tls_ticket_enc_index = (ref->tls_ticket_enc_index + 1) % TLS_TICKETS_NO; |
| 1042 | HA_RWLOCK_WRUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1043 | |
| 1044 | return 0; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1045 | } |
| 1046 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1047 | int ssl_sock_update_tlskey(char *filename, struct buffer *tlskey, char **err) |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 1048 | { |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1049 | struct tls_keys_ref *ref = tlskeys_ref_lookup(filename); |
| 1050 | |
| 1051 | if(!ref) { |
| 1052 | memprintf(err, "Unable to locate the referenced filename: %s", filename); |
| 1053 | return 1; |
| 1054 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 1055 | if (ssl_sock_update_tlskey_ref(ref, tlskey) < 0) { |
| 1056 | memprintf(err, "Invalid key size"); |
| 1057 | return 1; |
| 1058 | } |
| 1059 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1060 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1061 | } |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1062 | |
| 1063 | /* This function finalize the configuration parsing. Its set all the |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1064 | * automatic ids. It's called just after the basic checks. It returns |
| 1065 | * 0 on success otherwise ERR_*. |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1066 | */ |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1067 | static int tlskeys_finalize_config(void) |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1068 | { |
| 1069 | int i = 0; |
| 1070 | struct tls_keys_ref *ref, *ref2, *ref3; |
| 1071 | struct list tkr = LIST_HEAD_INIT(tkr); |
| 1072 | |
| 1073 | list_for_each_entry(ref, &tlskeys_reference, list) { |
| 1074 | if (ref->unique_id == -1) { |
| 1075 | /* Look for the first free id. */ |
| 1076 | while (1) { |
| 1077 | list_for_each_entry(ref2, &tlskeys_reference, list) { |
| 1078 | if (ref2->unique_id == i) { |
| 1079 | i++; |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | if (&ref2->list == &tlskeys_reference) |
| 1084 | break; |
| 1085 | } |
| 1086 | |
| 1087 | /* Uses the unique id and increment it for the next entry. */ |
| 1088 | ref->unique_id = i; |
| 1089 | i++; |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | /* This sort the reference list by id. */ |
| 1094 | list_for_each_entry_safe(ref, ref2, &tlskeys_reference, list) { |
| 1095 | LIST_DEL(&ref->list); |
| 1096 | list_for_each_entry(ref3, &tkr, list) { |
| 1097 | if (ref->unique_id < ref3->unique_id) { |
| 1098 | LIST_ADDQ(&ref3->list, &ref->list); |
| 1099 | break; |
| 1100 | } |
| 1101 | } |
| 1102 | if (&ref3->list == &tkr) |
| 1103 | LIST_ADDQ(&tkr, &ref->list); |
| 1104 | } |
| 1105 | |
| 1106 | /* swap root */ |
| 1107 | LIST_ADD(&tkr, &tlskeys_reference); |
| 1108 | LIST_DEL(&tkr); |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 1109 | return 0; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 1110 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 1111 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
| 1112 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1113 | #ifndef OPENSSL_NO_OCSP |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1114 | int ssl_sock_get_ocsp_arg_kt_index(int evp_keytype) |
| 1115 | { |
| 1116 | switch (evp_keytype) { |
| 1117 | case EVP_PKEY_RSA: |
| 1118 | return 2; |
| 1119 | case EVP_PKEY_DSA: |
| 1120 | return 0; |
| 1121 | case EVP_PKEY_EC: |
| 1122 | return 1; |
| 1123 | } |
| 1124 | |
| 1125 | return -1; |
| 1126 | } |
| 1127 | |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1128 | /* |
| 1129 | * Callback used to set OCSP status extension content in server hello. |
| 1130 | */ |
| 1131 | int ssl_sock_ocsp_stapling_cbk(SSL *ssl, void *arg) |
| 1132 | { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1133 | struct certificate_ocsp *ocsp; |
| 1134 | struct ocsp_cbk_arg *ocsp_arg; |
| 1135 | char *ssl_buf; |
| 1136 | EVP_PKEY *ssl_pkey; |
| 1137 | int key_type; |
| 1138 | int index; |
| 1139 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1140 | ocsp_arg = arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1141 | |
| 1142 | ssl_pkey = SSL_get_privatekey(ssl); |
| 1143 | if (!ssl_pkey) |
| 1144 | return SSL_TLSEXT_ERR_NOACK; |
| 1145 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1146 | key_type = EVP_PKEY_base_id(ssl_pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1147 | |
| 1148 | if (ocsp_arg->is_single && ocsp_arg->single_kt == key_type) |
| 1149 | ocsp = ocsp_arg->s_ocsp; |
| 1150 | else { |
| 1151 | /* For multiple certs per context, we have to find the correct OCSP response based on |
| 1152 | * the certificate type |
| 1153 | */ |
| 1154 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
| 1155 | |
| 1156 | if (index < 0) |
| 1157 | return SSL_TLSEXT_ERR_NOACK; |
| 1158 | |
| 1159 | ocsp = ocsp_arg->m_ocsp[index]; |
| 1160 | |
| 1161 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1162 | |
| 1163 | if (!ocsp || |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1164 | !ocsp->response.area || |
| 1165 | !ocsp->response.data || |
Emeric Brun | 4f3c87a | 2014-06-20 15:46:13 +0200 | [diff] [blame] | 1166 | (ocsp->expire < now.tv_sec)) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1167 | return SSL_TLSEXT_ERR_NOACK; |
| 1168 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1169 | ssl_buf = OPENSSL_malloc(ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1170 | if (!ssl_buf) |
| 1171 | return SSL_TLSEXT_ERR_NOACK; |
| 1172 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1173 | memcpy(ssl_buf, ocsp->response.area, ocsp->response.data); |
| 1174 | SSL_set_tlsext_status_ocsp_resp(ssl, ssl_buf, ocsp->response.data); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1175 | |
| 1176 | return SSL_TLSEXT_ERR_OK; |
| 1177 | } |
| 1178 | |
| 1179 | /* |
| 1180 | * This function enables the handling of OCSP status extension on 'ctx' if a |
| 1181 | * file name 'cert_path' suffixed using ".ocsp" is present. |
| 1182 | * To enable OCSP status extension, the issuer's certificate is mandatory. |
| 1183 | * It should be present in the certificate's extra chain builded from file |
| 1184 | * 'cert_path'. If not found, the issuer certificate is loaded from a file |
| 1185 | * named 'cert_path' suffixed using '.issuer'. |
| 1186 | * |
| 1187 | * In addition, ".ocsp" file content is loaded as a DER format of an OCSP |
| 1188 | * response. If file is empty or content is not a valid OCSP response, |
| 1189 | * OCSP status extension is enabled but OCSP response is ignored (a warning |
| 1190 | * is displayed). |
| 1191 | * |
| 1192 | * 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] | 1193 | * successfully enabled, or -1 in other error case. |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1194 | */ |
| 1195 | static int ssl_sock_load_ocsp(SSL_CTX *ctx, const char *cert_path) |
| 1196 | { |
| 1197 | |
| 1198 | BIO *in = NULL; |
| 1199 | X509 *x, *xi = NULL, *issuer = NULL; |
| 1200 | STACK_OF(X509) *chain = NULL; |
| 1201 | OCSP_CERTID *cid = NULL; |
| 1202 | SSL *ssl; |
| 1203 | char ocsp_path[MAXPATHLEN+1]; |
| 1204 | int i, ret = -1; |
| 1205 | struct stat st; |
| 1206 | struct certificate_ocsp *ocsp = NULL, *iocsp; |
| 1207 | char *warn = NULL; |
| 1208 | unsigned char *p; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1209 | pem_password_cb *passwd_cb; |
| 1210 | void *passwd_cb_userdata; |
| 1211 | void (*callback) (void); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1212 | |
| 1213 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1214 | |
| 1215 | if (stat(ocsp_path, &st)) |
| 1216 | return 1; |
| 1217 | |
| 1218 | ssl = SSL_new(ctx); |
| 1219 | if (!ssl) |
| 1220 | goto out; |
| 1221 | |
| 1222 | x = SSL_get_certificate(ssl); |
| 1223 | if (!x) |
| 1224 | goto out; |
| 1225 | |
| 1226 | /* Try to lookup for issuer in certificate extra chain */ |
| 1227 | #ifdef SSL_CTRL_GET_EXTRA_CHAIN_CERTS |
| 1228 | SSL_CTX_get_extra_chain_certs(ctx, &chain); |
| 1229 | #else |
| 1230 | chain = ctx->extra_certs; |
| 1231 | #endif |
| 1232 | for (i = 0; i < sk_X509_num(chain); i++) { |
| 1233 | issuer = sk_X509_value(chain, i); |
| 1234 | if (X509_check_issued(issuer, x) == X509_V_OK) |
| 1235 | break; |
| 1236 | else |
| 1237 | issuer = NULL; |
| 1238 | } |
| 1239 | |
| 1240 | /* If not found try to load issuer from a suffixed file */ |
| 1241 | if (!issuer) { |
| 1242 | char issuer_path[MAXPATHLEN+1]; |
| 1243 | |
| 1244 | in = BIO_new(BIO_s_file()); |
| 1245 | if (!in) |
| 1246 | goto out; |
| 1247 | |
| 1248 | snprintf(issuer_path, MAXPATHLEN+1, "%s.issuer", cert_path); |
| 1249 | if (BIO_read_filename(in, issuer_path) <= 0) |
| 1250 | goto out; |
| 1251 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1252 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 1253 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 1254 | |
| 1255 | 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] | 1256 | if (!xi) |
| 1257 | goto out; |
| 1258 | |
| 1259 | if (X509_check_issued(xi, x) != X509_V_OK) |
| 1260 | goto out; |
| 1261 | |
| 1262 | issuer = xi; |
| 1263 | } |
| 1264 | |
| 1265 | cid = OCSP_cert_to_id(0, x, issuer); |
| 1266 | if (!cid) |
| 1267 | goto out; |
| 1268 | |
| 1269 | i = i2d_OCSP_CERTID(cid, NULL); |
| 1270 | if (!i || (i > OCSP_MAX_CERTID_ASN1_LENGTH)) |
| 1271 | goto out; |
| 1272 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1273 | ocsp = calloc(1, sizeof(*ocsp)); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1274 | if (!ocsp) |
| 1275 | goto out; |
| 1276 | |
| 1277 | p = ocsp->key_data; |
| 1278 | i2d_OCSP_CERTID(cid, &p); |
| 1279 | |
| 1280 | iocsp = (struct certificate_ocsp *)ebmb_insert(&cert_ocsp_tree, &ocsp->key, OCSP_MAX_CERTID_ASN1_LENGTH); |
| 1281 | if (iocsp == ocsp) |
| 1282 | ocsp = NULL; |
| 1283 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1284 | #ifndef SSL_CTX_get_tlsext_status_cb |
| 1285 | # define SSL_CTX_get_tlsext_status_cb(ctx, cb) \ |
| 1286 | *cb = (void (*) (void))ctx->tlsext_status_cb; |
| 1287 | #endif |
| 1288 | SSL_CTX_get_tlsext_status_cb(ctx, &callback); |
| 1289 | |
| 1290 | if (!callback) { |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1291 | struct ocsp_cbk_arg *cb_arg = calloc(1, sizeof(*cb_arg)); |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1292 | EVP_PKEY *pkey; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1293 | |
| 1294 | cb_arg->is_single = 1; |
| 1295 | cb_arg->s_ocsp = iocsp; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1296 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1297 | pkey = X509_get_pubkey(x); |
| 1298 | cb_arg->single_kt = EVP_PKEY_base_id(pkey); |
| 1299 | EVP_PKEY_free(pkey); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1300 | |
| 1301 | SSL_CTX_set_tlsext_status_cb(ctx, ssl_sock_ocsp_stapling_cbk); |
| 1302 | SSL_CTX_set_tlsext_status_arg(ctx, cb_arg); |
| 1303 | } else { |
| 1304 | /* |
| 1305 | * If the ctx has a status CB, then we have previously set an OCSP staple for this ctx |
| 1306 | * Update that cb_arg with the new cert's staple |
| 1307 | */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1308 | struct ocsp_cbk_arg *cb_arg; |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1309 | struct certificate_ocsp *tmp_ocsp; |
| 1310 | int index; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1311 | int key_type; |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1312 | EVP_PKEY *pkey; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1313 | |
| 1314 | #ifdef SSL_CTX_get_tlsext_status_arg |
| 1315 | SSL_CTX_ctrl(ctx, SSL_CTRL_GET_TLSEXT_STATUS_REQ_CB_ARG, 0, &cb_arg); |
| 1316 | #else |
| 1317 | cb_arg = ctx->tlsext_status_arg; |
| 1318 | #endif |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1319 | |
| 1320 | /* |
| 1321 | * The following few lines will convert cb_arg from a single ocsp to multi ocsp |
| 1322 | * the order of operations below matter, take care when changing it |
| 1323 | */ |
| 1324 | tmp_ocsp = cb_arg->s_ocsp; |
| 1325 | index = ssl_sock_get_ocsp_arg_kt_index(cb_arg->single_kt); |
| 1326 | cb_arg->s_ocsp = NULL; |
| 1327 | cb_arg->m_ocsp[index] = tmp_ocsp; |
| 1328 | cb_arg->is_single = 0; |
| 1329 | cb_arg->single_kt = 0; |
| 1330 | |
Emmanuel Hocdet | b7a4c34 | 2017-01-06 12:57:46 +0100 | [diff] [blame] | 1331 | pkey = X509_get_pubkey(x); |
| 1332 | key_type = EVP_PKEY_base_id(pkey); |
| 1333 | EVP_PKEY_free(pkey); |
| 1334 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1335 | index = ssl_sock_get_ocsp_arg_kt_index(key_type); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 1336 | if (index >= 0 && !cb_arg->m_ocsp[index]) |
| 1337 | cb_arg->m_ocsp[index] = iocsp; |
| 1338 | |
| 1339 | } |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1340 | |
| 1341 | ret = 0; |
| 1342 | |
| 1343 | warn = NULL; |
| 1344 | if (ssl_sock_load_ocsp_response_from_file(ocsp_path, iocsp, cid, &warn)) { |
| 1345 | 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] | 1346 | ha_warning("%s.\n", warn); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | out: |
| 1350 | if (ssl) |
| 1351 | SSL_free(ssl); |
| 1352 | |
| 1353 | if (in) |
| 1354 | BIO_free(in); |
| 1355 | |
| 1356 | if (xi) |
| 1357 | X509_free(xi); |
| 1358 | |
| 1359 | if (cid) |
| 1360 | OCSP_CERTID_free(cid); |
| 1361 | |
| 1362 | if (ocsp) |
| 1363 | free(ocsp); |
| 1364 | |
| 1365 | if (warn) |
| 1366 | free(warn); |
| 1367 | |
| 1368 | |
| 1369 | return ret; |
| 1370 | } |
| 1371 | |
| 1372 | #endif |
| 1373 | |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1374 | #ifdef OPENSSL_IS_BORINGSSL |
| 1375 | static int ssl_sock_set_ocsp_response_from_file(SSL_CTX *ctx, const char *cert_path) |
| 1376 | { |
| 1377 | char ocsp_path[MAXPATHLEN+1]; |
| 1378 | struct stat st; |
| 1379 | int fd = -1, r = 0; |
| 1380 | |
| 1381 | snprintf(ocsp_path, MAXPATHLEN+1, "%s.ocsp", cert_path); |
| 1382 | if (stat(ocsp_path, &st)) |
| 1383 | return 0; |
| 1384 | |
| 1385 | fd = open(ocsp_path, O_RDONLY); |
| 1386 | if (fd == -1) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1387 | ha_warning("Error opening OCSP response file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1388 | return -1; |
| 1389 | } |
| 1390 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1391 | trash.data = 0; |
| 1392 | while (trash.data < trash.size) { |
| 1393 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1394 | if (r < 0) { |
| 1395 | if (errno == EINTR) |
| 1396 | continue; |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 1397 | ha_warning("Error reading OCSP response from file %s.\n", ocsp_path); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1398 | close(fd); |
| 1399 | return -1; |
| 1400 | } |
| 1401 | else if (r == 0) { |
| 1402 | break; |
| 1403 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1404 | trash.data += r; |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1405 | } |
| 1406 | close(fd); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1407 | return SSL_CTX_set_ocsp_response(ctx, (const uint8_t *) trash.area, |
| 1408 | trash.data); |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 1409 | } |
| 1410 | #endif |
| 1411 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1412 | #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] | 1413 | |
| 1414 | #define CT_EXTENSION_TYPE 18 |
| 1415 | |
| 1416 | static int sctl_ex_index = -1; |
| 1417 | |
| 1418 | /* |
| 1419 | * Try to parse Signed Certificate Timestamp List structure. This function |
| 1420 | * makes only basic test if the data seems like SCTL. No signature validation |
| 1421 | * is performed. |
| 1422 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1423 | static int ssl_sock_parse_sctl(struct buffer *sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1424 | { |
| 1425 | int ret = 1; |
| 1426 | int len, pos, sct_len; |
| 1427 | unsigned char *data; |
| 1428 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1429 | if (sctl->data < 2) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1430 | goto out; |
| 1431 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1432 | data = (unsigned char *) sctl->area; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1433 | len = (data[0] << 8) | data[1]; |
| 1434 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1435 | if (len + 2 != sctl->data) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1436 | goto out; |
| 1437 | |
| 1438 | data = data + 2; |
| 1439 | pos = 0; |
| 1440 | while (pos < len) { |
| 1441 | if (len - pos < 2) |
| 1442 | goto out; |
| 1443 | |
| 1444 | sct_len = (data[pos] << 8) | data[pos + 1]; |
| 1445 | if (pos + sct_len + 2 > len) |
| 1446 | goto out; |
| 1447 | |
| 1448 | pos += sct_len + 2; |
| 1449 | } |
| 1450 | |
| 1451 | ret = 0; |
| 1452 | |
| 1453 | out: |
| 1454 | return ret; |
| 1455 | } |
| 1456 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1457 | static int ssl_sock_load_sctl_from_file(const char *sctl_path, |
| 1458 | struct buffer **sctl) |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1459 | { |
| 1460 | int fd = -1; |
| 1461 | int r = 0; |
| 1462 | int ret = 1; |
| 1463 | |
| 1464 | *sctl = NULL; |
| 1465 | |
| 1466 | fd = open(sctl_path, O_RDONLY); |
| 1467 | if (fd == -1) |
| 1468 | goto end; |
| 1469 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1470 | trash.data = 0; |
| 1471 | while (trash.data < trash.size) { |
| 1472 | r = read(fd, trash.area + trash.data, trash.size - trash.data); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1473 | if (r < 0) { |
| 1474 | if (errno == EINTR) |
| 1475 | continue; |
| 1476 | |
| 1477 | goto end; |
| 1478 | } |
| 1479 | else if (r == 0) { |
| 1480 | break; |
| 1481 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1482 | trash.data += r; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | ret = ssl_sock_parse_sctl(&trash); |
| 1486 | if (ret) |
| 1487 | goto end; |
| 1488 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 1489 | *sctl = calloc(1, sizeof(**sctl)); |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1490 | if (!chunk_dup(*sctl, &trash)) { |
| 1491 | free(*sctl); |
| 1492 | *sctl = NULL; |
| 1493 | goto end; |
| 1494 | } |
| 1495 | |
| 1496 | end: |
| 1497 | if (fd != -1) |
| 1498 | close(fd); |
| 1499 | |
| 1500 | return ret; |
| 1501 | } |
| 1502 | |
| 1503 | int ssl_sock_sctl_add_cbk(SSL *ssl, unsigned ext_type, const unsigned char **out, size_t *outlen, int *al, void *add_arg) |
| 1504 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1505 | struct buffer *sctl = add_arg; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1506 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1507 | *out = (unsigned char *) sctl->area; |
| 1508 | *outlen = sctl->data; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1509 | |
| 1510 | return 1; |
| 1511 | } |
| 1512 | |
| 1513 | int ssl_sock_sctl_parse_cbk(SSL *s, unsigned int ext_type, const unsigned char *in, size_t inlen, int *al, void *parse_arg) |
| 1514 | { |
| 1515 | return 1; |
| 1516 | } |
| 1517 | |
| 1518 | static int ssl_sock_load_sctl(SSL_CTX *ctx, const char *cert_path) |
| 1519 | { |
| 1520 | char sctl_path[MAXPATHLEN+1]; |
| 1521 | int ret = -1; |
| 1522 | struct stat st; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1523 | struct buffer *sctl = NULL; |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 1524 | |
| 1525 | snprintf(sctl_path, MAXPATHLEN+1, "%s.sctl", cert_path); |
| 1526 | |
| 1527 | if (stat(sctl_path, &st)) |
| 1528 | return 1; |
| 1529 | |
| 1530 | if (ssl_sock_load_sctl_from_file(sctl_path, &sctl)) |
| 1531 | goto out; |
| 1532 | |
| 1533 | if (!SSL_CTX_add_server_custom_ext(ctx, CT_EXTENSION_TYPE, ssl_sock_sctl_add_cbk, NULL, sctl, ssl_sock_sctl_parse_cbk, NULL)) { |
| 1534 | free(sctl); |
| 1535 | goto out; |
| 1536 | } |
| 1537 | |
| 1538 | SSL_CTX_set_ex_data(ctx, sctl_ex_index, sctl); |
| 1539 | |
| 1540 | ret = 0; |
| 1541 | |
| 1542 | out: |
| 1543 | return ret; |
| 1544 | } |
| 1545 | |
| 1546 | #endif |
| 1547 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1548 | void ssl_sock_infocbk(const SSL *ssl, int where, int ret) |
| 1549 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1550 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1551 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1552 | BIO *write_bio; |
Willy Tarreau | 622317d | 2015-02-27 16:36:16 +0100 | [diff] [blame] | 1553 | (void)ret; /* shut gcc stupid warning */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1554 | |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1555 | #ifndef SSL_OP_NO_RENEGOTIATION |
| 1556 | /* Please note that BoringSSL defines this macro to zero so don't |
| 1557 | * change this to #if and do not assign a default value to this macro! |
| 1558 | */ |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1559 | if (where & SSL_CB_HANDSHAKE_START) { |
| 1560 | /* Disable renegotiation (CVE-2009-3555) */ |
Olivier Houchard | 90084a1 | 2017-11-23 18:21:29 +0100 | [diff] [blame] | 1561 | 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] | 1562 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1563 | conn->err_code = CO_ER_SSL_RENEG; |
| 1564 | } |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 1565 | } |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 1566 | #endif |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1567 | |
| 1568 | if ((where & SSL_CB_ACCEPT_LOOP) == SSL_CB_ACCEPT_LOOP) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1569 | if (!(ctx->xprt_st & SSL_SOCK_ST_FL_16K_WBFSIZE)) { |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1570 | /* Long certificate chains optimz |
| 1571 | If write and read bios are differents, we |
| 1572 | consider that the buffering was activated, |
| 1573 | so we rise the output buffer size from 4k |
| 1574 | to 16k */ |
| 1575 | write_bio = SSL_get_wbio(ssl); |
| 1576 | if (write_bio != SSL_get_rbio(ssl)) { |
| 1577 | BIO_set_write_buffer_size(write_bio, 16384); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1578 | ctx->xprt_st |= SSL_SOCK_ST_FL_16K_WBFSIZE; |
Emeric Brun | d8b2bb5 | 2014-01-28 15:43:53 +0100 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 1582 | } |
| 1583 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1584 | /* Callback is called for each certificate of the chain during a verify |
| 1585 | ok is set to 1 if preverify detect no error on current certificate. |
| 1586 | Returns 0 to break the handshake, 1 otherwise. */ |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 1587 | int ssl_sock_bind_verifycbk(int ok, X509_STORE_CTX *x_store) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1588 | { |
| 1589 | SSL *ssl; |
| 1590 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1591 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1592 | int err, depth; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1593 | |
| 1594 | 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] | 1595 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1596 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1597 | ctx = conn->xprt_ctx; |
| 1598 | |
| 1599 | ctx->xprt_st |= SSL_SOCK_ST_FL_VERIFY_DONE; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1600 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1601 | if (ok) /* no errors */ |
| 1602 | return ok; |
| 1603 | |
| 1604 | depth = X509_STORE_CTX_get_error_depth(x_store); |
| 1605 | err = X509_STORE_CTX_get_error(x_store); |
| 1606 | |
| 1607 | /* check if CA error needs to be ignored */ |
| 1608 | if (depth > 0) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1609 | if (!SSL_SOCK_ST_TO_CA_ERROR(ctx->xprt_st)) { |
| 1610 | ctx->xprt_st |= SSL_SOCK_CA_ERROR_TO_ST(err); |
| 1611 | ctx->xprt_st |= SSL_SOCK_CAEDEPTH_TO_ST(depth); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1612 | } |
| 1613 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1614 | 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] | 1615 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1616 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1617 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1618 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1619 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1620 | conn->err_code = CO_ER_SSL_CA_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1621 | return 0; |
| 1622 | } |
| 1623 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1624 | if (!SSL_SOCK_ST_TO_CRTERROR(ctx->xprt_st)) |
| 1625 | ctx->xprt_st |= SSL_SOCK_CRTERROR_TO_ST(err); |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 1626 | |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1627 | /* check if certificate error needs to be ignored */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1628 | 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] | 1629 | ssl_sock_dump_errors(conn); |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1630 | ERR_clear_error(); |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1631 | return 1; |
Emeric Brun | 1eb20ef | 2012-12-03 13:24:29 +0100 | [diff] [blame] | 1632 | } |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1633 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 1634 | conn->err_code = CO_ER_SSL_CRT_FAIL; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 1635 | return 0; |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 1636 | } |
| 1637 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1638 | static inline |
| 1639 | 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] | 1640 | const void *buf, size_t len, SSL *ssl) |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1641 | { |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1642 | struct ssl_capture *capture; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1643 | unsigned char *msg; |
| 1644 | unsigned char *end; |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1645 | size_t rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1646 | |
| 1647 | /* This function is called for "from client" and "to server" |
| 1648 | * connections. The combination of write_p == 0 and content_type == 22 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 1649 | * is only available during "from client" connection. |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1650 | */ |
| 1651 | |
| 1652 | /* "write_p" is set to 0 is the bytes are received messages, |
| 1653 | * otherwise it is set to 1. |
| 1654 | */ |
| 1655 | if (write_p != 0) |
| 1656 | return; |
| 1657 | |
| 1658 | /* content_type contains the type of message received or sent |
| 1659 | * according with the SSL/TLS protocol spec. This message is |
| 1660 | * encoded with one byte. The value 256 (two bytes) is used |
| 1661 | * for designing the SSL/TLS record layer. According with the |
| 1662 | * rfc6101, the expected message (other than 256) are: |
| 1663 | * - change_cipher_spec(20) |
| 1664 | * - alert(21) |
| 1665 | * - handshake(22) |
| 1666 | * - application_data(23) |
| 1667 | * - (255) |
| 1668 | * We are interessed by the handshake and specially the client |
| 1669 | * hello. |
| 1670 | */ |
| 1671 | if (content_type != 22) |
| 1672 | return; |
| 1673 | |
| 1674 | /* The message length is at least 4 bytes, containing the |
| 1675 | * message type and the message length. |
| 1676 | */ |
| 1677 | if (len < 4) |
| 1678 | return; |
| 1679 | |
| 1680 | /* First byte of the handshake message id the type of |
| 1681 | * message. The konwn types are: |
| 1682 | * - hello_request(0) |
| 1683 | * - client_hello(1) |
| 1684 | * - server_hello(2) |
| 1685 | * - certificate(11) |
| 1686 | * - server_key_exchange (12) |
| 1687 | * - certificate_request(13) |
| 1688 | * - server_hello_done(14) |
| 1689 | * We are interested by the client hello. |
| 1690 | */ |
| 1691 | msg = (unsigned char *)buf; |
| 1692 | if (msg[0] != 1) |
| 1693 | return; |
| 1694 | |
| 1695 | /* Next three bytes are the length of the message. The total length |
| 1696 | * must be this decoded length + 4. If the length given as argument |
| 1697 | * is not the same, we abort the protocol dissector. |
| 1698 | */ |
| 1699 | rec_len = (msg[1] << 16) + (msg[2] << 8) + msg[3]; |
| 1700 | if (len < rec_len + 4) |
| 1701 | return; |
| 1702 | msg += 4; |
| 1703 | end = msg + rec_len; |
| 1704 | if (end < msg) |
| 1705 | return; |
| 1706 | |
| 1707 | /* Expect 2 bytes for protocol version (1 byte for major and 1 byte |
| 1708 | * 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] | 1709 | * 28 bytes for unix payload. So we jump 1 + 1 + 4 + 28. |
| 1710 | */ |
| 1711 | msg += 1 + 1 + 4 + 28; |
| 1712 | if (msg > end) |
| 1713 | return; |
| 1714 | |
| 1715 | /* Next, is session id: |
| 1716 | * if present, we have to jump by length + 1 for the size information |
| 1717 | * if not present, we have to jump by 1 only |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1718 | */ |
Baptiste Assmann | 6be139f | 2018-11-28 15:20:25 +0100 | [diff] [blame] | 1719 | if (msg[0] > 0) |
| 1720 | msg += msg[0]; |
| 1721 | msg += 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1722 | if (msg > end) |
| 1723 | return; |
| 1724 | |
| 1725 | /* Next two bytes are the ciphersuite length. */ |
| 1726 | if (msg + 2 > end) |
| 1727 | return; |
| 1728 | rec_len = (msg[0] << 8) + msg[1]; |
| 1729 | msg += 2; |
| 1730 | if (msg + rec_len > end || msg + rec_len < msg) |
| 1731 | return; |
| 1732 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1733 | capture = pool_alloc_dirty(pool_head_ssl_capture); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1734 | if (!capture) |
| 1735 | return; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1736 | /* Compute the xxh64 of the ciphersuite. */ |
| 1737 | capture->xxh64 = XXH64(msg, rec_len, 0); |
| 1738 | |
| 1739 | /* Capture the ciphersuite. */ |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1740 | capture->ciphersuite_len = (global_ssl.capture_cipherlist < rec_len) ? |
| 1741 | global_ssl.capture_cipherlist : rec_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1742 | memcpy(capture->ciphersuite, msg, capture->ciphersuite_len); |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1743 | |
| 1744 | SSL_set_ex_data(ssl, ssl_capture_ptr_index, capture); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 1745 | } |
| 1746 | |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1747 | /* Callback is called for ssl protocol analyse */ |
| 1748 | void ssl_sock_msgcbk(int write_p, int version, int content_type, const void *buf, size_t len, SSL *ssl, void *arg) |
| 1749 | { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1750 | #ifdef TLS1_RT_HEARTBEAT |
| 1751 | /* test heartbeat received (write_p is set to 0 |
| 1752 | for a received record) */ |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1753 | if ((content_type == TLS1_RT_HEARTBEAT) && (write_p == 0)) { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 1754 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1755 | struct ssl_sock_ctx *ctx = conn->ctx; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1756 | const unsigned char *p = buf; |
| 1757 | unsigned int payload; |
| 1758 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 1759 | ctx->xprt_st |= SSL_SOCK_RECV_HEARTBEAT; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1760 | |
| 1761 | /* Check if this is a CVE-2014-0160 exploitation attempt. */ |
| 1762 | if (*p != TLS1_HB_REQUEST) |
| 1763 | return; |
| 1764 | |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1765 | 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] | 1766 | goto kill_it; |
| 1767 | |
| 1768 | payload = (p[1] * 256) + p[2]; |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1769 | if (3 + payload + 16 <= len) |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1770 | return; /* OK no problem */ |
Willy Tarreau | aeed672 | 2014-04-25 23:59:58 +0200 | [diff] [blame] | 1771 | kill_it: |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1772 | /* We have a clear heartbleed attack (CVE-2014-0160), the |
| 1773 | * advertised payload is larger than the advertised packet |
| 1774 | * length, so we have garbage in the buffer between the |
| 1775 | * payload and the end of the buffer (p+len). We can't know |
| 1776 | * if the SSL stack is patched, and we don't know if we can |
| 1777 | * safely wipe out the area between p+3+len and payload. |
| 1778 | * So instead, we prevent the response from being sent by |
| 1779 | * setting the max_send_fragment to 0 and we report an SSL |
| 1780 | * error, which will kill this connection. It will be reported |
| 1781 | * above as SSL_ERROR_SSL while an other handshake failure with |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1782 | * a heartbeat message will be reported as SSL_ERROR_SYSCALL. |
| 1783 | */ |
Willy Tarreau | 3b2fdb6 | 2014-04-25 23:44:22 +0200 | [diff] [blame] | 1784 | ssl->max_send_fragment = 0; |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 1785 | SSLerr(SSL_F_TLS1_HEARTBEAT, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 1786 | return; |
| 1787 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1788 | #endif |
Emmanuel Hocdet | e380474 | 2017-03-08 11:07:10 +0100 | [diff] [blame] | 1789 | if (global_ssl.capture_cipherlist > 0) |
| 1790 | ssl_sock_parse_clienthello(write_p, version, content_type, buf, len, ssl); |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 1791 | } |
| 1792 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 1793 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 1794 | static int ssl_sock_srv_select_protos(SSL *s, unsigned char **out, unsigned char *outlen, |
| 1795 | const unsigned char *in, unsigned int inlen, |
| 1796 | void *arg) |
| 1797 | { |
| 1798 | struct server *srv = arg; |
| 1799 | |
| 1800 | if (SSL_select_next_proto(out, outlen, in, inlen, (unsigned char *)srv->ssl_ctx.npn_str, |
| 1801 | srv->ssl_ctx.npn_len) == OPENSSL_NPN_NEGOTIATED) |
| 1802 | return SSL_TLSEXT_ERR_OK; |
| 1803 | return SSL_TLSEXT_ERR_NOACK; |
| 1804 | } |
| 1805 | #endif |
| 1806 | |
| 1807 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1808 | /* This callback is used so that the server advertises the list of |
| 1809 | * negociable protocols for NPN. |
| 1810 | */ |
| 1811 | static int ssl_sock_advertise_npn_protos(SSL *s, const unsigned char **data, |
| 1812 | unsigned int *len, void *arg) |
| 1813 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1814 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 1815 | |
| 1816 | *data = (const unsigned char *)conf->npn_str; |
| 1817 | *len = conf->npn_len; |
| 1818 | return SSL_TLSEXT_ERR_OK; |
| 1819 | } |
| 1820 | #endif |
| 1821 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1822 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1823 | /* This callback is used so that the server advertises the list of |
| 1824 | * negociable protocols for ALPN. |
| 1825 | */ |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1826 | static int ssl_sock_advertise_alpn_protos(SSL *s, const unsigned char **out, |
| 1827 | unsigned char *outlen, |
| 1828 | const unsigned char *server, |
| 1829 | unsigned int server_len, void *arg) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1830 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1831 | struct ssl_bind_conf *conf = arg; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1832 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 1833 | if (SSL_select_next_proto((unsigned char**) out, outlen, (const unsigned char *)conf->alpn_str, |
| 1834 | conf->alpn_len, server, server_len) != OPENSSL_NPN_NEGOTIATED) { |
| 1835 | return SSL_TLSEXT_ERR_NOACK; |
| 1836 | } |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 1837 | return SSL_TLSEXT_ERR_OK; |
| 1838 | } |
| 1839 | #endif |
| 1840 | |
Willy Tarreau | c8ad3be | 2015-06-17 15:48:26 +0200 | [diff] [blame] | 1841 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 1842 | #ifndef SSL_NO_GENERATE_CERTIFICATES |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1843 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 1844 | /* Create a X509 certificate with the specified servername and serial. This |
| 1845 | * function returns a SSL_CTX object or NULL if an error occurs. */ |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1846 | static SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1847 | 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] | 1848 | { |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1849 | X509 *cacert = bind_conf->ca_sign_cert; |
| 1850 | EVP_PKEY *capkey = bind_conf->ca_sign_pkey; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1851 | SSL_CTX *ssl_ctx = NULL; |
| 1852 | X509 *newcrt = NULL; |
| 1853 | EVP_PKEY *pkey = NULL; |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1854 | SSL *tmp_ssl = NULL; |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1855 | CONF *ctmp = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1856 | X509_NAME *name; |
| 1857 | const EVP_MD *digest; |
| 1858 | X509V3_CTX ctx; |
| 1859 | unsigned int i; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1860 | int key_type; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1861 | |
Christopher Faulet | 48a8332 | 2017-07-28 16:56:09 +0200 | [diff] [blame] | 1862 | /* Get the private key of the default certificate and use it */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 1863 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10002000L) |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1864 | pkey = SSL_CTX_get0_privatekey(bind_conf->default_ctx); |
| 1865 | #else |
| 1866 | tmp_ssl = SSL_new(bind_conf->default_ctx); |
| 1867 | if (tmp_ssl) |
| 1868 | pkey = SSL_get_privatekey(tmp_ssl); |
| 1869 | #endif |
| 1870 | if (!pkey) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1871 | goto mkcert_error; |
| 1872 | |
| 1873 | /* Create the certificate */ |
| 1874 | if (!(newcrt = X509_new())) |
| 1875 | goto mkcert_error; |
| 1876 | |
| 1877 | /* Set version number for the certificate (X509v3) and the serial |
| 1878 | * number */ |
| 1879 | if (X509_set_version(newcrt, 2L) != 1) |
| 1880 | goto mkcert_error; |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 1881 | 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] | 1882 | |
| 1883 | /* Set duration for the certificate */ |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 1884 | if (!X509_gmtime_adj(X509_getm_notBefore(newcrt), (long)-60*60*24) || |
| 1885 | !X509_gmtime_adj(X509_getm_notAfter(newcrt),(long)60*60*24*365)) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1886 | goto mkcert_error; |
| 1887 | |
| 1888 | /* set public key in the certificate */ |
| 1889 | if (X509_set_pubkey(newcrt, pkey) != 1) |
| 1890 | goto mkcert_error; |
| 1891 | |
| 1892 | /* Set issuer name from the CA */ |
| 1893 | if (!(name = X509_get_subject_name(cacert))) |
| 1894 | goto mkcert_error; |
| 1895 | if (X509_set_issuer_name(newcrt, name) != 1) |
| 1896 | goto mkcert_error; |
| 1897 | |
| 1898 | /* Set the subject name using the same, but the CN */ |
| 1899 | name = X509_NAME_dup(name); |
| 1900 | if (X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, |
| 1901 | (const unsigned char *)servername, |
| 1902 | -1, -1, 0) != 1) { |
| 1903 | X509_NAME_free(name); |
| 1904 | goto mkcert_error; |
| 1905 | } |
| 1906 | if (X509_set_subject_name(newcrt, name) != 1) { |
| 1907 | X509_NAME_free(name); |
| 1908 | goto mkcert_error; |
| 1909 | } |
| 1910 | X509_NAME_free(name); |
| 1911 | |
| 1912 | /* Add x509v3 extensions as specified */ |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1913 | ctmp = NCONF_new(NULL); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1914 | X509V3_set_ctx(&ctx, cacert, newcrt, NULL, NULL, 0); |
| 1915 | for (i = 0; i < X509V3_EXT_SIZE; i++) { |
| 1916 | X509_EXTENSION *ext; |
| 1917 | |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1918 | 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] | 1919 | goto mkcert_error; |
| 1920 | if (!X509_add_ext(newcrt, ext, -1)) { |
| 1921 | X509_EXTENSION_free(ext); |
| 1922 | goto mkcert_error; |
| 1923 | } |
| 1924 | X509_EXTENSION_free(ext); |
| 1925 | } |
| 1926 | |
| 1927 | /* Sign the certificate with the CA private key */ |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1928 | |
| 1929 | key_type = EVP_PKEY_base_id(capkey); |
| 1930 | |
| 1931 | if (key_type == EVP_PKEY_DSA) |
| 1932 | digest = EVP_sha1(); |
| 1933 | else if (key_type == EVP_PKEY_RSA) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1934 | digest = EVP_sha256(); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 1935 | else if (key_type == EVP_PKEY_EC) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1936 | digest = EVP_sha256(); |
| 1937 | else { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 1938 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000000fL) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1939 | int nid; |
| 1940 | |
| 1941 | if (EVP_PKEY_get_default_digest_nid(capkey, &nid) <= 0) |
| 1942 | goto mkcert_error; |
| 1943 | if (!(digest = EVP_get_digestbynid(nid))) |
| 1944 | goto mkcert_error; |
Christopher Faulet | e7db216 | 2015-10-19 13:59:24 +0200 | [diff] [blame] | 1945 | #else |
| 1946 | goto mkcert_error; |
| 1947 | #endif |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1948 | } |
| 1949 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1950 | if (!(X509_sign(newcrt, capkey, digest))) |
| 1951 | goto mkcert_error; |
| 1952 | |
| 1953 | /* Create and set the new SSL_CTX */ |
| 1954 | if (!(ssl_ctx = SSL_CTX_new(SSLv23_server_method()))) |
| 1955 | goto mkcert_error; |
| 1956 | if (!SSL_CTX_use_PrivateKey(ssl_ctx, pkey)) |
| 1957 | goto mkcert_error; |
| 1958 | if (!SSL_CTX_use_certificate(ssl_ctx, newcrt)) |
| 1959 | goto mkcert_error; |
| 1960 | if (!SSL_CTX_check_private_key(ssl_ctx)) |
| 1961 | goto mkcert_error; |
| 1962 | |
| 1963 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1964 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1965 | #ifndef OPENSSL_NO_DH |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1966 | SSL_CTX_set_tmp_dh_callback(ssl_ctx, ssl_get_tmp_dh); |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 1967 | #endif |
Christopher Faulet | 85b5a1a | 2015-10-09 11:46:32 +0200 | [diff] [blame] | 1968 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
| 1969 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 1970 | 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] | 1971 | EC_KEY *ecc; |
| 1972 | int nid; |
| 1973 | |
| 1974 | if ((nid = OBJ_sn2nid(ecdhe)) == NID_undef) |
| 1975 | goto end; |
| 1976 | if (!(ecc = EC_KEY_new_by_curve_name(nid))) |
| 1977 | goto end; |
| 1978 | SSL_CTX_set_tmp_ecdh(ssl_ctx, ecc); |
| 1979 | EC_KEY_free(ecc); |
| 1980 | } |
| 1981 | #endif |
| 1982 | end: |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1983 | return ssl_ctx; |
| 1984 | |
| 1985 | mkcert_error: |
Emmanuel Hocdet | a9b8402 | 2018-10-01 18:41:36 +0200 | [diff] [blame] | 1986 | if (ctmp) NCONF_free(ctmp); |
Emmanuel Hocdet | 1596929 | 2017-08-11 10:56:00 +0200 | [diff] [blame] | 1987 | if (tmp_ssl) SSL_free(tmp_ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1988 | if (ssl_ctx) SSL_CTX_free(ssl_ctx); |
| 1989 | if (newcrt) X509_free(newcrt); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 1990 | return NULL; |
| 1991 | } |
| 1992 | |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 1993 | SSL_CTX * |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1994 | 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] | 1995 | { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 1996 | struct bind_conf *bind_conf = __objt_listener(conn->target)->bind_conf; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1997 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 1998 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 1999 | return ssl_sock_do_create_cert(servername, bind_conf, ctx->ssl); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2000 | } |
| 2001 | |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2002 | /* 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] | 2003 | * certificates and immediately assign it to the SSL session if not null. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2004 | SSL_CTX * |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2005 | 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] | 2006 | { |
| 2007 | struct lru64 *lru = NULL; |
| 2008 | |
| 2009 | if (ssl_ctx_lru_tree) { |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2010 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2011 | 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] | 2012 | if (lru && lru->domain) { |
| 2013 | if (ssl) |
| 2014 | SSL_set_SSL_CTX(ssl, (SSL_CTX *)lru->data); |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2015 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2016 | return (SSL_CTX *)lru->data; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2017 | } |
Willy Tarreau | 03f4ec4 | 2018-05-17 10:56:47 +0200 | [diff] [blame] | 2018 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2019 | } |
| 2020 | return NULL; |
| 2021 | } |
| 2022 | |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2023 | /* Same as <ssl_sock_assign_generated_cert> but without SSL session. This |
| 2024 | * function is not thread-safe, it should only be used to check if a certificate |
| 2025 | * exists in the lru cache (with no warranty it will not be removed by another |
| 2026 | * thread). It is kept for backward compatibility. */ |
| 2027 | SSL_CTX * |
| 2028 | ssl_sock_get_generated_cert(unsigned int key, struct bind_conf *bind_conf) |
| 2029 | { |
| 2030 | return ssl_sock_assign_generated_cert(key, bind_conf, NULL); |
| 2031 | } |
| 2032 | |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2033 | /* Set a certificate int the LRU cache used to store generated |
| 2034 | * certificate. Return 0 on success, otherwise -1 */ |
| 2035 | int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2036 | 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] | 2037 | { |
| 2038 | struct lru64 *lru = NULL; |
| 2039 | |
| 2040 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2041 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2042 | 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] | 2043 | if (!lru) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2044 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2045 | return -1; |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2046 | } |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2047 | if (lru->domain && lru->data) |
| 2048 | lru->free((SSL_CTX *)lru->data); |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2049 | 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] | 2050 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2051 | return 0; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2052 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2053 | return -1; |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2054 | } |
| 2055 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2056 | /* Compute the key of the certificate. */ |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2057 | unsigned int |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2058 | ssl_sock_generated_cert_key(const void *data, size_t len) |
Christopher Faulet | 3054880 | 2015-06-11 13:39:32 +0200 | [diff] [blame] | 2059 | { |
| 2060 | return XXH32(data, len, ssl_ctx_lru_seed); |
| 2061 | } |
| 2062 | |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2063 | /* Generate a cert and immediately assign it to the SSL session so that the cert's |
| 2064 | * refcount is maintained regardless of the cert's presence in the LRU cache. |
| 2065 | */ |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2066 | static int |
Christopher Faulet | 7969a33 | 2015-10-09 11:15:03 +0200 | [diff] [blame] | 2067 | 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] | 2068 | { |
| 2069 | X509 *cacert = bind_conf->ca_sign_cert; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2070 | SSL_CTX *ssl_ctx = NULL; |
| 2071 | struct lru64 *lru = NULL; |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2072 | unsigned int key; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2073 | |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2074 | key = ssl_sock_generated_cert_key(servername, strlen(servername)); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2075 | if (ssl_ctx_lru_tree) { |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2076 | HA_RWLOCK_WRLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2077 | lru = lru64_get(key, ssl_ctx_lru_tree, cacert, 0); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2078 | if (lru && lru->domain) |
| 2079 | ssl_ctx = (SSL_CTX *)lru->data; |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2080 | if (!ssl_ctx && lru) { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2081 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2082 | lru64_commit(lru, ssl_ctx, cacert, 0, (void (*)(void *))SSL_CTX_free); |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 2083 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2084 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 2085 | HA_RWLOCK_WRUNLOCK(SSL_GEN_CERTS_LOCK, &ssl_ctx_lru_rwlock); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2086 | return 1; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2087 | } |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2088 | else { |
Christopher Faulet | 635c0ad | 2015-11-12 11:35:51 +0100 | [diff] [blame] | 2089 | ssl_ctx = ssl_sock_do_create_cert(servername, bind_conf, ssl); |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2090 | SSL_set_SSL_CTX(ssl, ssl_ctx); |
| 2091 | /* No LRU cache, this CTX will be released as soon as the session dies */ |
| 2092 | SSL_CTX_free(ssl_ctx); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2093 | return 1; |
Willy Tarreau | 2f63ef4 | 2015-10-20 15:16:01 +0200 | [diff] [blame] | 2094 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2095 | return 0; |
| 2096 | } |
| 2097 | static int |
| 2098 | ssl_sock_generate_certificate_from_conn(struct bind_conf *bind_conf, SSL *ssl) |
| 2099 | { |
| 2100 | unsigned int key; |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2101 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2102 | |
| 2103 | conn_get_to_addr(conn); |
| 2104 | if (conn->flags & CO_FL_ADDR_TO_SET) { |
| 2105 | key = ssl_sock_generated_cert_key(&conn->addr.to, get_addr_len(&conn->addr.to)); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 2106 | if (ssl_sock_assign_generated_cert(key, bind_conf, ssl)) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2107 | return 1; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2108 | } |
| 2109 | return 0; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2110 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2111 | #endif /* !defined SSL_NO_GENERATE_CERTIFICATES */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2112 | |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2113 | |
| 2114 | #ifndef SSL_OP_CIPHER_SERVER_PREFERENCE /* needs OpenSSL >= 0.9.7 */ |
| 2115 | #define SSL_OP_CIPHER_SERVER_PREFERENCE 0 |
| 2116 | #endif |
| 2117 | |
| 2118 | #ifndef SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION /* needs OpenSSL >= 0.9.7 */ |
| 2119 | #define SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION 0 |
| 2120 | #define SSL_renegotiate_pending(arg) 0 |
| 2121 | #endif |
| 2122 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 0.9.8 */ |
| 2123 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 2124 | #endif |
| 2125 | #ifndef SSL_OP_NO_TICKET /* needs OpenSSL >= 0.9.8 */ |
| 2126 | #define SSL_OP_NO_TICKET 0 |
| 2127 | #endif |
| 2128 | #ifndef SSL_OP_NO_COMPRESSION /* needs OpenSSL >= 0.9.9 */ |
| 2129 | #define SSL_OP_NO_COMPRESSION 0 |
| 2130 | #endif |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2131 | #ifdef OPENSSL_NO_SSL3 /* SSLv3 support removed */ |
| 2132 | #undef SSL_OP_NO_SSLv3 |
| 2133 | #define SSL_OP_NO_SSLv3 0 |
| 2134 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2135 | #ifndef SSL_OP_NO_TLSv1_1 /* needs OpenSSL >= 1.0.1 */ |
| 2136 | #define SSL_OP_NO_TLSv1_1 0 |
| 2137 | #endif |
| 2138 | #ifndef SSL_OP_NO_TLSv1_2 /* needs OpenSSL >= 1.0.1 */ |
| 2139 | #define SSL_OP_NO_TLSv1_2 0 |
| 2140 | #endif |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2141 | #ifndef SSL_OP_NO_TLSv1_3 /* needs OpenSSL >= 1.1.1 */ |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2142 | #define SSL_OP_NO_TLSv1_3 0 |
| 2143 | #endif |
| 2144 | #ifndef SSL_OP_SINGLE_DH_USE /* needs OpenSSL >= 0.9.6 */ |
| 2145 | #define SSL_OP_SINGLE_DH_USE 0 |
| 2146 | #endif |
| 2147 | #ifndef SSL_OP_SINGLE_ECDH_USE /* needs OpenSSL >= 1.0.0 */ |
| 2148 | #define SSL_OP_SINGLE_ECDH_USE 0 |
| 2149 | #endif |
| 2150 | #ifndef SSL_MODE_RELEASE_BUFFERS /* needs OpenSSL >= 1.0.0 */ |
| 2151 | #define SSL_MODE_RELEASE_BUFFERS 0 |
| 2152 | #endif |
| 2153 | #ifndef SSL_MODE_SMALL_BUFFERS /* needs small_records.patch */ |
| 2154 | #define SSL_MODE_SMALL_BUFFERS 0 |
| 2155 | #endif |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 2156 | #ifndef SSL_OP_PRIORITIZE_CHACHA /* needs OpenSSL >= 1.1.1 */ |
| 2157 | #define SSL_OP_PRIORITIZE_CHACHA 0 |
| 2158 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2159 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 2160 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2161 | typedef enum { SET_CLIENT, SET_SERVER } set_context_func; |
| 2162 | |
| 2163 | 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] | 2164 | { |
Emmanuel Hocdet | 23877ab | 2017-07-12 12:53:02 +0200 | [diff] [blame] | 2165 | #if SSL_OP_NO_SSLv3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2166 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, SSLv3_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2167 | : SSL_CTX_set_ssl_version(ctx, SSLv3_client_method()); |
| 2168 | #endif |
| 2169 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2170 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2171 | c == SET_SERVER ? SSL_CTX_set_ssl_version(ctx, TLSv1_server_method()) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2172 | : SSL_CTX_set_ssl_version(ctx, TLSv1_client_method()); |
| 2173 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2174 | 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] | 2175 | #if SSL_OP_NO_TLSv1_1 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2176 | 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] | 2177 | : SSL_CTX_set_ssl_version(ctx, TLSv1_1_client_method()); |
| 2178 | #endif |
| 2179 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2180 | 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] | 2181 | #if SSL_OP_NO_TLSv1_2 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2182 | 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] | 2183 | : SSL_CTX_set_ssl_version(ctx, TLSv1_2_client_method()); |
| 2184 | #endif |
| 2185 | } |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2186 | /* TLSv1.2 is the last supported version in this context. */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2187 | static void ctx_set_TLSv13_func(SSL_CTX *ctx, set_context_func c) {} |
| 2188 | /* Unusable in this context. */ |
| 2189 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) {} |
| 2190 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) {} |
| 2191 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) {} |
| 2192 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) {} |
| 2193 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) {} |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2194 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2195 | typedef enum { SET_MIN, SET_MAX } set_context_func; |
| 2196 | |
| 2197 | static void ctx_set_SSLv3_func(SSL_CTX *ctx, set_context_func c) { |
| 2198 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, SSL3_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2199 | : SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION); |
| 2200 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2201 | static void ssl_set_SSLv3_func(SSL *ssl, set_context_func c) { |
| 2202 | c == SET_MAX ? SSL_set_max_proto_version(ssl, SSL3_VERSION) |
| 2203 | : SSL_set_min_proto_version(ssl, SSL3_VERSION); |
| 2204 | } |
| 2205 | static void ctx_set_TLSv10_func(SSL_CTX *ctx, set_context_func c) { |
| 2206 | c == SET_MAX ? SSL_CTX_set_max_proto_version(ctx, TLS1_VERSION) |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2207 | : SSL_CTX_set_min_proto_version(ctx, TLS1_VERSION); |
| 2208 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2209 | static void ssl_set_TLSv10_func(SSL *ssl, set_context_func c) { |
| 2210 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_VERSION) |
| 2211 | : SSL_set_min_proto_version(ssl, TLS1_VERSION); |
| 2212 | } |
| 2213 | static void ctx_set_TLSv11_func(SSL_CTX *ctx, set_context_func c) { |
| 2214 | 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] | 2215 | : SSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION); |
| 2216 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2217 | static void ssl_set_TLSv11_func(SSL *ssl, set_context_func c) { |
| 2218 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_1_VERSION) |
| 2219 | : SSL_set_min_proto_version(ssl, TLS1_1_VERSION); |
| 2220 | } |
| 2221 | static void ctx_set_TLSv12_func(SSL_CTX *ctx, set_context_func c) { |
| 2222 | 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] | 2223 | : SSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 2224 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2225 | static void ssl_set_TLSv12_func(SSL *ssl, set_context_func c) { |
| 2226 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_2_VERSION) |
| 2227 | : SSL_set_min_proto_version(ssl, TLS1_2_VERSION); |
| 2228 | } |
| 2229 | 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] | 2230 | #if SSL_OP_NO_TLSv1_3 |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2231 | 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] | 2232 | : SSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION); |
| 2233 | #endif |
| 2234 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2235 | static void ssl_set_TLSv13_func(SSL *ssl, set_context_func c) { |
| 2236 | #if SSL_OP_NO_TLSv1_3 |
| 2237 | c == SET_MAX ? SSL_set_max_proto_version(ssl, TLS1_3_VERSION) |
| 2238 | : SSL_set_min_proto_version(ssl, TLS1_3_VERSION); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2239 | #endif |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2240 | } |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2241 | #endif |
| 2242 | static void ctx_set_None_func(SSL_CTX *ctx, set_context_func c) { } |
| 2243 | static void ssl_set_None_func(SSL *ssl, set_context_func c) { } |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2244 | |
| 2245 | static struct { |
| 2246 | int option; |
| 2247 | uint16_t flag; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2248 | void (*ctx_set_version)(SSL_CTX *, set_context_func); |
| 2249 | void (*ssl_set_version)(SSL *, set_context_func); |
Emmanuel Hocdet | ecb0e23 | 2017-05-18 11:56:58 +0200 | [diff] [blame] | 2250 | const char *name; |
| 2251 | } methodVersions[] = { |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 2252 | {0, 0, ctx_set_None_func, ssl_set_None_func, "NONE"}, /* CONF_TLSV_NONE */ |
| 2253 | {SSL_OP_NO_SSLv3, MC_SSL_O_NO_SSLV3, ctx_set_SSLv3_func, ssl_set_SSLv3_func, "SSLv3"}, /* CONF_SSLV3 */ |
| 2254 | {SSL_OP_NO_TLSv1, MC_SSL_O_NO_TLSV10, ctx_set_TLSv10_func, ssl_set_TLSv10_func, "TLSv1.0"}, /* CONF_TLSV10 */ |
| 2255 | {SSL_OP_NO_TLSv1_1, MC_SSL_O_NO_TLSV11, ctx_set_TLSv11_func, ssl_set_TLSv11_func, "TLSv1.1"}, /* CONF_TLSV11 */ |
| 2256 | {SSL_OP_NO_TLSv1_2, MC_SSL_O_NO_TLSV12, ctx_set_TLSv12_func, ssl_set_TLSv12_func, "TLSv1.2"}, /* CONF_TLSV12 */ |
| 2257 | {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] | 2258 | }; |
| 2259 | |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2260 | static void ssl_sock_switchctx_set(SSL *ssl, SSL_CTX *ctx) |
| 2261 | { |
| 2262 | SSL_set_verify(ssl, SSL_CTX_get_verify_mode(ctx), ssl_sock_bind_verifycbk); |
| 2263 | SSL_set_client_CA_list(ssl, SSL_dup_CA_list(SSL_CTX_get_client_CA_list(ctx))); |
| 2264 | SSL_set_SSL_CTX(ssl, ctx); |
| 2265 | } |
| 2266 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2267 | #if ((HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL)) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2268 | |
| 2269 | static int ssl_sock_switchctx_err_cbk(SSL *ssl, int *al, void *priv) |
| 2270 | { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2271 | struct bind_conf *s = priv; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2272 | (void)al; /* shut gcc stupid warning */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2273 | |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2274 | if (SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name) || s->generate_certs) |
| 2275 | return SSL_TLSEXT_ERR_OK; |
| 2276 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2277 | } |
| 2278 | |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2279 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2280 | static int ssl_sock_switchctx_cbk(const struct ssl_early_callback_ctx *ctx) |
| 2281 | { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2282 | SSL *ssl = ctx->ssl; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2283 | #else |
| 2284 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *arg) |
| 2285 | { |
| 2286 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2287 | struct connection *conn; |
| 2288 | struct bind_conf *s; |
| 2289 | const uint8_t *extension_data; |
| 2290 | size_t extension_len; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2291 | int has_rsa_sig = 0, has_ecdsa_sig = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2292 | |
| 2293 | char *wildp = NULL; |
| 2294 | const uint8_t *servername; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2295 | size_t servername_len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2296 | 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] | 2297 | int allow_early = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2298 | int i; |
| 2299 | |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 2300 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Willy Tarreau | a882552 | 2018-10-15 13:20:07 +0200 | [diff] [blame] | 2301 | s = __objt_listener(conn->target)->bind_conf; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2302 | |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 2303 | if (s->ssl_conf.early_data) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2304 | allow_early = 1; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2305 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2306 | if (SSL_early_callback_ctx_extension_get(ctx, TLSEXT_TYPE_server_name, |
| 2307 | &extension_data, &extension_len)) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2308 | #else |
| 2309 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_server_name, &extension_data, &extension_len)) { |
| 2310 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2311 | /* |
| 2312 | * The server_name extension was given too much extensibility when it |
| 2313 | * was written, so parsing the normal case is a bit complex. |
| 2314 | */ |
| 2315 | size_t len; |
| 2316 | if (extension_len <= 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2317 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2318 | /* Extract the length of the supplied list of names. */ |
| 2319 | len = (*extension_data++) << 8; |
| 2320 | len |= *extension_data++; |
| 2321 | if (len + 2 != extension_len) |
| 2322 | goto abort; |
| 2323 | /* |
| 2324 | * The list in practice only has a single element, so we only consider |
| 2325 | * the first one. |
| 2326 | */ |
| 2327 | if (len == 0 || *extension_data++ != TLSEXT_NAMETYPE_host_name) |
| 2328 | goto abort; |
| 2329 | extension_len = len - 1; |
| 2330 | /* Now we can finally pull out the byte array with the actual hostname. */ |
| 2331 | if (extension_len <= 2) |
| 2332 | goto abort; |
| 2333 | len = (*extension_data++) << 8; |
| 2334 | len |= *extension_data++; |
| 2335 | if (len == 0 || len + 2 > extension_len || len > TLSEXT_MAXLEN_host_name |
| 2336 | || memchr(extension_data, 0, len) != NULL) |
| 2337 | goto abort; |
| 2338 | servername = extension_data; |
| 2339 | servername_len = len; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2340 | } else { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2341 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
| 2342 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2343 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2344 | } |
| 2345 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2346 | /* without SNI extension, is the default_ctx (need SSL_TLSEXT_ERR_NOACK) */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2347 | if (!s->strict_sni) { |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2348 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2349 | goto allow_early; |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2350 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2351 | goto abort; |
| 2352 | } |
| 2353 | |
| 2354 | /* extract/check clientHello informations */ |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2355 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2356 | 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] | 2357 | #else |
| 2358 | if (SSL_client_hello_get0_ext(ssl, TLSEXT_TYPE_signature_algorithms, &extension_data, &extension_len)) { |
| 2359 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2360 | uint8_t sign; |
| 2361 | size_t len; |
| 2362 | if (extension_len < 2) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2363 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2364 | len = (*extension_data++) << 8; |
| 2365 | len |= *extension_data++; |
| 2366 | if (len + 2 != extension_len) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2367 | goto abort; |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2368 | if (len % 2 != 0) |
| 2369 | goto abort; |
| 2370 | for (; len > 0; len -= 2) { |
| 2371 | extension_data++; /* hash */ |
| 2372 | sign = *extension_data++; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2373 | switch (sign) { |
| 2374 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2375 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2376 | break; |
| 2377 | case TLSEXT_signature_ecdsa: |
| 2378 | has_ecdsa_sig = 1; |
| 2379 | break; |
| 2380 | default: |
| 2381 | continue; |
| 2382 | } |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2383 | if (has_ecdsa_sig && has_rsa_sig) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2384 | break; |
| 2385 | } |
| 2386 | } else { |
Bertrand Jacquin | a25282b | 2018-08-14 00:56:13 +0100 | [diff] [blame] | 2387 | /* without TLSEXT_TYPE_signature_algorithms extension (< TLSv1.2) */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2388 | has_rsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2389 | } |
| 2390 | 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] | 2391 | const SSL_CIPHER *cipher; |
| 2392 | size_t len; |
| 2393 | const uint8_t *cipher_suites; |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2394 | has_ecdsa_sig = 0; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2395 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2396 | len = ctx->cipher_suites_len; |
| 2397 | cipher_suites = ctx->cipher_suites; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2398 | #else |
| 2399 | len = SSL_client_hello_get0_ciphers(ssl, &cipher_suites); |
| 2400 | #endif |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2401 | if (len % 2 != 0) |
| 2402 | goto abort; |
| 2403 | for (; len != 0; len -= 2, cipher_suites += 2) { |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2404 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2405 | uint16_t cipher_suite = (cipher_suites[0] << 8) | cipher_suites[1]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2406 | cipher = SSL_get_cipher_by_value(cipher_suite); |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2407 | #else |
| 2408 | cipher = SSL_CIPHER_find(ssl, cipher_suites); |
| 2409 | #endif |
Emmanuel Hocdet | 019f9b1 | 2017-10-02 17:12:06 +0200 | [diff] [blame] | 2410 | if (cipher && SSL_CIPHER_get_auth_nid(cipher) == NID_auth_ecdsa) { |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2411 | has_ecdsa_sig = 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2412 | break; |
| 2413 | } |
| 2414 | } |
| 2415 | } |
| 2416 | |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2417 | for (i = 0; i < trash.size && i < servername_len; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2418 | trash.area[i] = tolower(servername[i]); |
| 2419 | if (!wildp && (trash.area[i] == '.')) |
| 2420 | wildp = &trash.area[i]; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2421 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2422 | trash.area[i] = 0; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2423 | |
| 2424 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2425 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2426 | |
| 2427 | /* lookup a not neg filter */ |
| 2428 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2429 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2430 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2431 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2432 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2433 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2434 | break; |
| 2435 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2436 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2437 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2438 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2439 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2440 | if (!node_anonymous) |
| 2441 | node_anonymous = n; |
| 2442 | break; |
| 2443 | } |
| 2444 | } |
| 2445 | } |
| 2446 | if (wildp) { |
| 2447 | /* lookup in wildcards names */ |
| 2448 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
| 2449 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2450 | if (!container_of(n, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2451 | switch(container_of(n, struct sni_ctx, name)->kinfo.sig) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2452 | case TLSEXT_signature_ecdsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2453 | if (!node_ecdsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2454 | node_ecdsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2455 | break; |
| 2456 | case TLSEXT_signature_rsa: |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2457 | if (!node_rsa) |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2458 | node_rsa = n; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2459 | break; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2460 | default: /* TLSEXT_signature_anonymous|dsa */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2461 | if (!node_anonymous) |
| 2462 | node_anonymous = n; |
| 2463 | break; |
| 2464 | } |
| 2465 | } |
| 2466 | } |
| 2467 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2468 | /* select by key_signature priority order */ |
Emmanuel Hocdet | 9f9b0c6 | 2018-09-03 16:29:16 +0200 | [diff] [blame] | 2469 | node = (has_ecdsa_sig && node_ecdsa) ? node_ecdsa |
| 2470 | : ((has_rsa_sig && node_rsa) ? node_rsa |
| 2471 | : (node_anonymous ? node_anonymous |
| 2472 | : (node_ecdsa ? node_ecdsa /* no ecdsa signature case (< TLSv1.2) */ |
| 2473 | : node_rsa /* no rsa signature case (far far away) */ |
| 2474 | ))); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2475 | if (node) { |
| 2476 | /* switch ctx */ |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 2477 | 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] | 2478 | 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] | 2479 | if (conf) { |
| 2480 | methodVersions[conf->ssl_methods.min].ssl_set_version(ssl, SET_MIN); |
| 2481 | methodVersions[conf->ssl_methods.max].ssl_set_version(ssl, SET_MAX); |
| 2482 | if (conf->early_data) |
| 2483 | allow_early = 1; |
| 2484 | } |
| 2485 | goto allow_early; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2486 | } |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2487 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2488 | if (s->generate_certs && ssl_sock_generate_certificate(trash.area, s, ssl)) { |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2489 | /* switch ctx done in ssl_sock_generate_certificate */ |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2490 | goto allow_early; |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2491 | } |
| 2492 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2493 | if (!s->strict_sni) { |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2494 | /* no certificate match, is the default_ctx */ |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2495 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2496 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 2497 | allow_early: |
| 2498 | #ifdef OPENSSL_IS_BORINGSSL |
| 2499 | if (allow_early) |
| 2500 | SSL_set_early_data_enabled(ssl, 1); |
| 2501 | #else |
| 2502 | if (!allow_early) |
| 2503 | SSL_set_max_early_data(ssl, 0); |
| 2504 | #endif |
| 2505 | return 1; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2506 | abort: |
| 2507 | /* abort handshake (was SSL_TLSEXT_ERR_ALERT_FATAL) */ |
| 2508 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2509 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 48e8755 | 2017-08-16 11:28:44 +0200 | [diff] [blame] | 2510 | return ssl_select_cert_error; |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 2511 | #else |
| 2512 | *al = SSL_AD_UNRECOGNIZED_NAME; |
| 2513 | return 0; |
| 2514 | #endif |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2515 | } |
| 2516 | |
| 2517 | #else /* OPENSSL_IS_BORINGSSL */ |
| 2518 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2519 | /* Sets the SSL ctx of <ssl> to match the advertised server name. Returns a |
| 2520 | * warning when no match is found, which implies the default (first) cert |
| 2521 | * will keep being used. |
| 2522 | */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2523 | static int ssl_sock_switchctx_cbk(SSL *ssl, int *al, void *priv) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2524 | { |
| 2525 | const char *servername; |
| 2526 | const char *wildp = NULL; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2527 | struct ebmb_node *node, *n; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2528 | struct bind_conf *s = priv; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2529 | int i; |
| 2530 | (void)al; /* shut gcc stupid warning */ |
| 2531 | |
| 2532 | servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2533 | if (!servername) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2534 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2535 | if (s->generate_certs && ssl_sock_generate_certificate_from_conn(s, ssl)) |
| 2536 | return SSL_TLSEXT_ERR_OK; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2537 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2538 | if (s->strict_sni) |
| 2539 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2540 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2541 | return SSL_TLSEXT_ERR_NOACK; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2542 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2543 | |
Willy Tarreau | 19d14ef | 2012-10-29 16:51:55 +0100 | [diff] [blame] | 2544 | for (i = 0; i < trash.size; i++) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2545 | if (!servername[i]) |
| 2546 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2547 | trash.area[i] = tolower(servername[i]); |
| 2548 | if (!wildp && (trash.area[i] == '.')) |
| 2549 | wildp = &trash.area[i]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2550 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2551 | trash.area[i] = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2552 | |
| 2553 | /* lookup in full qualified names */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2554 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2555 | |
| 2556 | /* lookup a not neg filter */ |
| 2557 | for (n = node; n; n = ebmb_next_dup(n)) { |
| 2558 | if (!container_of(n, struct sni_ctx, name)->neg) { |
| 2559 | node = n; |
| 2560 | break; |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 2561 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2562 | } |
| 2563 | if (!node && wildp) { |
| 2564 | /* lookup in wildcards names */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2565 | node = ebst_lookup(&s->sni_w_ctx, wildp); |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2566 | } |
| 2567 | if (!node || container_of(node, struct sni_ctx, name)->neg) { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2568 | #if (!defined SSL_NO_GENERATE_CERTIFICATES) |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 2569 | if (s->generate_certs && ssl_sock_generate_certificate(servername, s, ssl)) { |
| 2570 | /* switch ctx done in ssl_sock_generate_certificate */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 2571 | return SSL_TLSEXT_ERR_OK; |
| 2572 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 2573 | #endif |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 2574 | if (s->strict_sni) |
| 2575 | return SSL_TLSEXT_ERR_ALERT_FATAL; |
| 2576 | ssl_sock_switchctx_set(ssl, s->default_ctx); |
| 2577 | return SSL_TLSEXT_ERR_OK; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | /* switch ctx */ |
Emmanuel Hocdet | 530141f | 2017-03-01 18:54:56 +0100 | [diff] [blame] | 2581 | 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] | 2582 | return SSL_TLSEXT_ERR_OK; |
| 2583 | } |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2584 | #endif /* (!) OPENSSL_IS_BORINGSSL */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 2585 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 2586 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2587 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2588 | |
| 2589 | static DH * ssl_get_dh_1024(void) |
| 2590 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2591 | static unsigned char dh1024_p[]={ |
| 2592 | 0xFA,0xF9,0x2A,0x22,0x2A,0xA7,0x7F,0xE1,0x67,0x4E,0x53,0xF7, |
| 2593 | 0x56,0x13,0xC3,0xB1,0xE3,0x29,0x6B,0x66,0x31,0x6A,0x7F,0xB3, |
| 2594 | 0xC2,0x68,0x6B,0xCB,0x1D,0x57,0x39,0x1D,0x1F,0xFF,0x1C,0xC9, |
| 2595 | 0xA6,0xA4,0x98,0x82,0x31,0x5D,0x25,0xFF,0x8A,0xE0,0x73,0x96, |
| 2596 | 0x81,0xC8,0x83,0x79,0xC1,0x5A,0x04,0xF8,0x37,0x0D,0xA8,0x3D, |
| 2597 | 0xAE,0x74,0xBC,0xDB,0xB6,0xA4,0x75,0xD9,0x71,0x8A,0xA0,0x17, |
| 2598 | 0x9E,0x2D,0xC8,0xA8,0xDF,0x2C,0x5F,0x82,0x95,0xF8,0x92,0x9B, |
| 2599 | 0xA7,0x33,0x5F,0x89,0x71,0xC8,0x2D,0x6B,0x18,0x86,0xC4,0x94, |
| 2600 | 0x22,0xA5,0x52,0x8D,0xF6,0xF6,0xD2,0x37,0x92,0x0F,0xA5,0xCC, |
| 2601 | 0xDB,0x7B,0x1D,0x3D,0xA1,0x31,0xB7,0x80,0x8F,0x0B,0x67,0x5E, |
| 2602 | 0x36,0xA5,0x60,0x0C,0xF1,0x95,0x33,0x8B, |
| 2603 | }; |
| 2604 | static unsigned char dh1024_g[]={ |
| 2605 | 0x02, |
| 2606 | }; |
| 2607 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2608 | BIGNUM *p; |
| 2609 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2610 | DH *dh = DH_new(); |
| 2611 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2612 | p = BN_bin2bn(dh1024_p, sizeof dh1024_p, NULL); |
| 2613 | g = BN_bin2bn(dh1024_g, sizeof dh1024_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2614 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2615 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2616 | DH_free(dh); |
| 2617 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2618 | } else { |
| 2619 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2620 | } |
| 2621 | } |
| 2622 | return dh; |
| 2623 | } |
| 2624 | |
| 2625 | static DH *ssl_get_dh_2048(void) |
| 2626 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2627 | static unsigned char dh2048_p[]={ |
| 2628 | 0xEC,0x86,0xF8,0x70,0xA0,0x33,0x16,0xEC,0x05,0x1A,0x73,0x59, |
| 2629 | 0xCD,0x1F,0x8B,0xF8,0x29,0xE4,0xD2,0xCF,0x52,0xDD,0xC2,0x24, |
| 2630 | 0x8D,0xB5,0x38,0x9A,0xFB,0x5C,0xA4,0xE4,0xB2,0xDA,0xCE,0x66, |
| 2631 | 0x50,0x74,0xA6,0x85,0x4D,0x4B,0x1D,0x30,0xB8,0x2B,0xF3,0x10, |
| 2632 | 0xE9,0xA7,0x2D,0x05,0x71,0xE7,0x81,0xDF,0x8B,0x59,0x52,0x3B, |
| 2633 | 0x5F,0x43,0x0B,0x68,0xF1,0xDB,0x07,0xBE,0x08,0x6B,0x1B,0x23, |
| 2634 | 0xEE,0x4D,0xCC,0x9E,0x0E,0x43,0xA0,0x1E,0xDF,0x43,0x8C,0xEC, |
| 2635 | 0xBE,0xBE,0x90,0xB4,0x51,0x54,0xB9,0x2F,0x7B,0x64,0x76,0x4E, |
| 2636 | 0x5D,0xD4,0x2E,0xAE,0xC2,0x9E,0xAE,0x51,0x43,0x59,0xC7,0x77, |
| 2637 | 0x9C,0x50,0x3C,0x0E,0xED,0x73,0x04,0x5F,0xF1,0x4C,0x76,0x2A, |
| 2638 | 0xD8,0xF8,0xCF,0xFC,0x34,0x40,0xD1,0xB4,0x42,0x61,0x84,0x66, |
| 2639 | 0x42,0x39,0x04,0xF8,0x68,0xB2,0x62,0xD7,0x55,0xED,0x1B,0x74, |
| 2640 | 0x75,0x91,0xE0,0xC5,0x69,0xC1,0x31,0x5C,0xDB,0x7B,0x44,0x2E, |
| 2641 | 0xCE,0x84,0x58,0x0D,0x1E,0x66,0x0C,0xC8,0x44,0x9E,0xFD,0x40, |
| 2642 | 0x08,0x67,0x5D,0xFB,0xA7,0x76,0x8F,0x00,0x11,0x87,0xE9,0x93, |
| 2643 | 0xF9,0x7D,0xC4,0xBC,0x74,0x55,0x20,0xD4,0x4A,0x41,0x2F,0x43, |
| 2644 | 0x42,0x1A,0xC1,0xF2,0x97,0x17,0x49,0x27,0x37,0x6B,0x2F,0x88, |
| 2645 | 0x7E,0x1C,0xA0,0xA1,0x89,0x92,0x27,0xD9,0x56,0x5A,0x71,0xC1, |
| 2646 | 0x56,0x37,0x7E,0x3A,0x9D,0x05,0xE7,0xEE,0x5D,0x8F,0x82,0x17, |
| 2647 | 0xBC,0xE9,0xC2,0x93,0x30,0x82,0xF9,0xF4,0xC9,0xAE,0x49,0xDB, |
| 2648 | 0xD0,0x54,0xB4,0xD9,0x75,0x4D,0xFA,0x06,0xB8,0xD6,0x38,0x41, |
| 2649 | 0xB7,0x1F,0x77,0xF3, |
| 2650 | }; |
| 2651 | static unsigned char dh2048_g[]={ |
| 2652 | 0x02, |
| 2653 | }; |
| 2654 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2655 | BIGNUM *p; |
| 2656 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2657 | DH *dh = DH_new(); |
| 2658 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2659 | p = BN_bin2bn(dh2048_p, sizeof dh2048_p, NULL); |
| 2660 | g = BN_bin2bn(dh2048_g, sizeof dh2048_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2661 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2662 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2663 | DH_free(dh); |
| 2664 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2665 | } else { |
| 2666 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2667 | } |
| 2668 | } |
| 2669 | return dh; |
| 2670 | } |
| 2671 | |
| 2672 | static DH *ssl_get_dh_4096(void) |
| 2673 | { |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2674 | static unsigned char dh4096_p[]={ |
| 2675 | 0xDE,0x16,0x94,0xCD,0x99,0x58,0x07,0xF1,0xF7,0x32,0x96,0x11, |
| 2676 | 0x04,0x82,0xD4,0x84,0x72,0x80,0x99,0x06,0xCA,0xF0,0xA3,0x68, |
| 2677 | 0x07,0xCE,0x64,0x50,0xE7,0x74,0x45,0x20,0x80,0x5E,0x4D,0xAD, |
| 2678 | 0xA5,0xB6,0xED,0xFA,0x80,0x6C,0x3B,0x35,0xC4,0x9A,0x14,0x6B, |
| 2679 | 0x32,0xBB,0xFD,0x1F,0x17,0x8E,0xB7,0x1F,0xD6,0xFA,0x3F,0x7B, |
| 2680 | 0xEE,0x16,0xA5,0x62,0x33,0x0D,0xED,0xBC,0x4E,0x58,0xE5,0x47, |
| 2681 | 0x4D,0xE9,0xAB,0x8E,0x38,0xD3,0x6E,0x90,0x57,0xE3,0x22,0x15, |
| 2682 | 0x33,0xBD,0xF6,0x43,0x45,0xB5,0x10,0x0A,0xBE,0x2C,0xB4,0x35, |
| 2683 | 0xB8,0x53,0x8D,0xAD,0xFB,0xA7,0x1F,0x85,0x58,0x41,0x7A,0x79, |
| 2684 | 0x20,0x68,0xB3,0xE1,0x3D,0x08,0x76,0xBF,0x86,0x0D,0x49,0xE3, |
| 2685 | 0x82,0x71,0x8C,0xB4,0x8D,0x81,0x84,0xD4,0xE7,0xBE,0x91,0xDC, |
| 2686 | 0x26,0x39,0x48,0x0F,0x35,0xC4,0xCA,0x65,0xE3,0x40,0x93,0x52, |
| 2687 | 0x76,0x58,0x7D,0xDD,0x51,0x75,0xDC,0x69,0x61,0xBF,0x47,0x2C, |
| 2688 | 0x16,0x68,0x2D,0xC9,0x29,0xD3,0xE6,0xC0,0x99,0x48,0xA0,0x9A, |
| 2689 | 0xC8,0x78,0xC0,0x6D,0x81,0x67,0x12,0x61,0x3F,0x71,0xBA,0x41, |
| 2690 | 0x1F,0x6C,0x89,0x44,0x03,0xBA,0x3B,0x39,0x60,0xAA,0x28,0x55, |
| 2691 | 0x59,0xAE,0xB8,0xFA,0xCB,0x6F,0xA5,0x1A,0xF7,0x2B,0xDD,0x52, |
| 2692 | 0x8A,0x8B,0xE2,0x71,0xA6,0x5E,0x7E,0xD8,0x2E,0x18,0xE0,0x66, |
| 2693 | 0xDF,0xDD,0x22,0x21,0x99,0x52,0x73,0xA6,0x33,0x20,0x65,0x0E, |
| 2694 | 0x53,0xE7,0x6B,0x9B,0xC5,0xA3,0x2F,0x97,0x65,0x76,0xD3,0x47, |
| 2695 | 0x23,0x77,0x12,0xB6,0x11,0x7B,0x24,0xED,0xF1,0xEF,0xC0,0xE2, |
| 2696 | 0xA3,0x7E,0x67,0x05,0x3E,0x96,0x4D,0x45,0xC2,0x18,0xD1,0x73, |
| 2697 | 0x9E,0x07,0xF3,0x81,0x6E,0x52,0x63,0xF6,0x20,0x76,0xB9,0x13, |
| 2698 | 0xD2,0x65,0x30,0x18,0x16,0x09,0x16,0x9E,0x8F,0xF1,0xD2,0x10, |
| 2699 | 0x5A,0xD3,0xD4,0xAF,0x16,0x61,0xDA,0x55,0x2E,0x18,0x5E,0x14, |
| 2700 | 0x08,0x54,0x2E,0x2A,0x25,0xA2,0x1A,0x9B,0x8B,0x32,0xA9,0xFD, |
| 2701 | 0xC2,0x48,0x96,0xE1,0x80,0xCA,0xE9,0x22,0x17,0xBB,0xCE,0x3E, |
| 2702 | 0x9E,0xED,0xC7,0xF1,0x1F,0xEC,0x17,0x21,0xDC,0x7B,0x82,0x48, |
| 2703 | 0x8E,0xBB,0x4B,0x9D,0x5B,0x04,0x04,0xDA,0xDB,0x39,0xDF,0x01, |
| 2704 | 0x40,0xC3,0xAA,0x26,0x23,0x89,0x75,0xC6,0x0B,0xD0,0xA2,0x60, |
| 2705 | 0x6A,0xF1,0xCC,0x65,0x18,0x98,0x1B,0x52,0xD2,0x74,0x61,0xCC, |
| 2706 | 0xBD,0x60,0xAE,0xA3,0xA0,0x66,0x6A,0x16,0x34,0x92,0x3F,0x41, |
| 2707 | 0x40,0x31,0x29,0xC0,0x2C,0x63,0xB2,0x07,0x8D,0xEB,0x94,0xB8, |
| 2708 | 0xE8,0x47,0x92,0x52,0x93,0x6A,0x1B,0x7E,0x1A,0x61,0xB3,0x1B, |
| 2709 | 0xF0,0xD6,0x72,0x9B,0xF1,0xB0,0xAF,0xBF,0x3E,0x65,0xEF,0x23, |
| 2710 | 0x1D,0x6F,0xFF,0x70,0xCD,0x8A,0x4C,0x8A,0xA0,0x72,0x9D,0xBE, |
| 2711 | 0xD4,0xBB,0x24,0x47,0x4A,0x68,0xB5,0xF5,0xC6,0xD5,0x7A,0xCD, |
| 2712 | 0xCA,0x06,0x41,0x07,0xAD,0xC2,0x1E,0xE6,0x54,0xA7,0xAD,0x03, |
| 2713 | 0xD9,0x12,0xC1,0x9C,0x13,0xB1,0xC9,0x0A,0x43,0x8E,0x1E,0x08, |
| 2714 | 0xCE,0x50,0x82,0x73,0x5F,0xA7,0x55,0x1D,0xD9,0x59,0xAC,0xB5, |
| 2715 | 0xEA,0x02,0x7F,0x6C,0x5B,0x74,0x96,0x98,0x67,0x24,0xA3,0x0F, |
| 2716 | 0x15,0xFC,0xA9,0x7D,0x3E,0x67,0xD1,0x70,0xF8,0x97,0xF3,0x67, |
| 2717 | 0xC5,0x8C,0x88,0x44,0x08,0x02,0xC7,0x2B, |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2718 | }; |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2719 | static unsigned char dh4096_g[]={ |
| 2720 | 0x02, |
| 2721 | }; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2722 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2723 | BIGNUM *p; |
| 2724 | BIGNUM *g; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2725 | DH *dh = DH_new(); |
| 2726 | if (dh) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2727 | p = BN_bin2bn(dh4096_p, sizeof dh4096_p, NULL); |
| 2728 | g = BN_bin2bn(dh4096_g, sizeof dh4096_g, NULL); |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2729 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2730 | if (!p || !g) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2731 | DH_free(dh); |
| 2732 | dh = NULL; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2733 | } else { |
| 2734 | DH_set0_pqg(dh, p, NULL, g); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2735 | } |
| 2736 | } |
| 2737 | return dh; |
| 2738 | } |
| 2739 | |
| 2740 | /* Returns Diffie-Hellman parameters matching the private key length |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2741 | but not exceeding global_ssl.default_dh_param */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2742 | static DH *ssl_get_tmp_dh(SSL *ssl, int export, int keylen) |
| 2743 | { |
| 2744 | DH *dh = NULL; |
| 2745 | EVP_PKEY *pkey = SSL_get_privatekey(ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 2746 | int type; |
| 2747 | |
| 2748 | type = pkey ? EVP_PKEY_base_id(pkey) : EVP_PKEY_NONE; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2749 | |
| 2750 | /* The keylen supplied by OpenSSL can only be 512 or 1024. |
| 2751 | See ssl3_send_server_key_exchange() in ssl/s3_srvr.c |
| 2752 | */ |
| 2753 | if (type == EVP_PKEY_RSA || type == EVP_PKEY_DSA) { |
| 2754 | keylen = EVP_PKEY_bits(pkey); |
| 2755 | } |
| 2756 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2757 | if (keylen > global_ssl.default_dh_param) { |
| 2758 | keylen = global_ssl.default_dh_param; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2759 | } |
| 2760 | |
Remi Gacogne | d3a341a | 2015-05-29 16:26:17 +0200 | [diff] [blame] | 2761 | if (keylen >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2762 | dh = local_dh_4096; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2763 | } |
| 2764 | else if (keylen >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2765 | dh = local_dh_2048; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2766 | } |
| 2767 | else { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2768 | dh = local_dh_1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2769 | } |
| 2770 | |
| 2771 | return dh; |
| 2772 | } |
| 2773 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2774 | static DH * ssl_sock_get_dh_from_file(const char *filename) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2775 | { |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2776 | DH *dh = NULL; |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2777 | BIO *in = BIO_new(BIO_s_file()); |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2778 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2779 | if (in == NULL) |
| 2780 | goto end; |
| 2781 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2782 | if (BIO_read_filename(in, filename) <= 0) |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2783 | goto end; |
| 2784 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2785 | dh = PEM_read_bio_DHparams(in, NULL, NULL, NULL); |
| 2786 | |
| 2787 | end: |
| 2788 | if (in) |
| 2789 | BIO_free(in); |
| 2790 | |
Emeric Brun | e1b4ed4 | 2018-08-16 15:14:12 +0200 | [diff] [blame] | 2791 | ERR_clear_error(); |
| 2792 | |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2793 | return dh; |
| 2794 | } |
| 2795 | |
| 2796 | int ssl_sock_load_global_dh_param_from_file(const char *filename) |
| 2797 | { |
| 2798 | global_dh = ssl_sock_get_dh_from_file(filename); |
| 2799 | |
| 2800 | if (global_dh) { |
| 2801 | return 0; |
| 2802 | } |
| 2803 | |
| 2804 | return -1; |
| 2805 | } |
| 2806 | |
| 2807 | /* Loads Diffie-Hellman parameter from a file. Returns 1 if loaded, else -1 |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 2808 | if an error occurred, and 0 if parameter not found. */ |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2809 | int ssl_sock_load_dh_params(SSL_CTX *ctx, const char *file) |
| 2810 | { |
| 2811 | int ret = -1; |
| 2812 | DH *dh = ssl_sock_get_dh_from_file(file); |
| 2813 | |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2814 | if (dh) { |
| 2815 | ret = 1; |
| 2816 | SSL_CTX_set_tmp_dh(ctx, dh); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 2817 | |
| 2818 | if (ssl_dh_ptr_index >= 0) { |
| 2819 | /* store a pointer to the DH params to avoid complaining about |
| 2820 | ssl-default-dh-param not being set for this SSL_CTX */ |
| 2821 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, dh); |
| 2822 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2823 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 2824 | else if (global_dh) { |
| 2825 | SSL_CTX_set_tmp_dh(ctx, global_dh); |
| 2826 | ret = 0; /* DH params not found */ |
| 2827 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2828 | else { |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2829 | /* Clear openssl global errors stack */ |
| 2830 | ERR_clear_error(); |
| 2831 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 2832 | if (global_ssl.default_dh_param <= 1024) { |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2833 | /* we are limited to DH parameter of 1024 bits anyway */ |
Remi Gacogne | c7e1263 | 2016-07-02 16:26:10 +0200 | [diff] [blame] | 2834 | if (local_dh_1024 == NULL) |
| 2835 | local_dh_1024 = ssl_get_dh_1024(); |
| 2836 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2837 | if (local_dh_1024 == NULL) |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2838 | goto end; |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2839 | |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 2840 | SSL_CTX_set_tmp_dh(ctx, local_dh_1024); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 2841 | } |
| 2842 | else { |
| 2843 | SSL_CTX_set_tmp_dh_callback(ctx, ssl_get_tmp_dh); |
| 2844 | } |
Willy Tarreau | 6e774b4 | 2014-04-25 21:35:23 +0200 | [diff] [blame] | 2845 | |
Emeric Brun | 41fdb3c | 2013-04-26 11:05:44 +0200 | [diff] [blame] | 2846 | ret = 0; /* DH params not found */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2847 | } |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 2848 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2849 | end: |
| 2850 | if (dh) |
| 2851 | DH_free(dh); |
| 2852 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 2853 | return ret; |
| 2854 | } |
| 2855 | #endif |
| 2856 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 2857 | 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] | 2858 | struct pkey_info kinfo, char *name, int order) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2859 | { |
| 2860 | struct sni_ctx *sc; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2861 | int wild = 0, neg = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2862 | struct ebmb_node *node; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2863 | |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2864 | if (*name == '!') { |
| 2865 | neg = 1; |
| 2866 | name++; |
| 2867 | } |
| 2868 | if (*name == '*') { |
| 2869 | wild = 1; |
| 2870 | name++; |
| 2871 | } |
| 2872 | /* !* filter is a nop */ |
| 2873 | if (neg && wild) |
| 2874 | return order; |
| 2875 | if (*name) { |
| 2876 | int j, len; |
| 2877 | len = strlen(name); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2878 | for (j = 0; j < len && j < trash.size; j++) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2879 | trash.area[j] = tolower(name[j]); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2880 | if (j >= trash.size) |
| 2881 | return order; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2882 | trash.area[j] = 0; |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2883 | |
| 2884 | /* Check for duplicates. */ |
| 2885 | if (wild) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2886 | node = ebst_lookup(&s->sni_w_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2887 | else |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2888 | node = ebst_lookup(&s->sni_ctx, trash.area); |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2889 | for (; node; node = ebmb_next_dup(node)) { |
| 2890 | sc = ebmb_entry(node, struct sni_ctx, name); |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2891 | if (sc->ctx == ctx && sc->conf == conf && sc->neg == neg) |
Thierry FOURNIER / OZON.IO | 07c3d78 | 2016-10-06 10:56:48 +0200 | [diff] [blame] | 2892 | return order; |
| 2893 | } |
| 2894 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2895 | sc = malloc(sizeof(struct sni_ctx) + len + 1); |
Thierry FOURNIER / OZON.IO | 7a3bd3b | 2016-10-06 10:35:29 +0200 | [diff] [blame] | 2896 | if (!sc) |
| 2897 | return order; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2898 | memcpy(sc->name.key, trash.area, len + 1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2899 | sc->ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 2900 | sc->conf = conf; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 2901 | sc->kinfo = kinfo; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 2902 | sc->order = order++; |
| 2903 | sc->neg = neg; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 2904 | if (kinfo.sig != TLSEXT_signature_anonymous) |
| 2905 | SSL_CTX_set_ex_data(ctx, ssl_pkey_info_index, &sc->kinfo); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 2906 | if (wild) |
| 2907 | ebst_insert(&s->sni_w_ctx, &sc->name); |
| 2908 | else |
| 2909 | ebst_insert(&s->sni_ctx, &sc->name); |
| 2910 | } |
| 2911 | return order; |
| 2912 | } |
| 2913 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2914 | |
| 2915 | /* The following code is used for loading multiple crt files into |
| 2916 | * SSL_CTX's based on CN/SAN |
| 2917 | */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 2918 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2919 | /* This is used to preload the certifcate, private key |
| 2920 | * and Cert Chain of a file passed in via the crt |
| 2921 | * argument |
| 2922 | * |
| 2923 | * This way, we do not have to read the file multiple times |
| 2924 | */ |
| 2925 | struct cert_key_and_chain { |
| 2926 | X509 *cert; |
| 2927 | EVP_PKEY *key; |
| 2928 | unsigned int num_chain_certs; |
| 2929 | /* This is an array of X509 pointers */ |
| 2930 | X509 **chain_certs; |
| 2931 | }; |
| 2932 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 2933 | #define SSL_SOCK_POSSIBLE_KT_COMBOS (1<<(SSL_SOCK_NUM_KEYTYPES)) |
| 2934 | |
| 2935 | struct key_combo_ctx { |
| 2936 | SSL_CTX *ctx; |
| 2937 | int order; |
| 2938 | }; |
| 2939 | |
| 2940 | /* Map used for processing multiple keypairs for a single purpose |
| 2941 | * |
| 2942 | * This maps CN/SNI name to certificate type |
| 2943 | */ |
| 2944 | struct sni_keytype { |
| 2945 | int keytypes; /* BITMASK for keytypes */ |
| 2946 | struct ebmb_node name; /* node holding the servername value */ |
| 2947 | }; |
| 2948 | |
| 2949 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 2950 | /* Frees the contents of a cert_key_and_chain |
| 2951 | */ |
| 2952 | static void ssl_sock_free_cert_key_and_chain_contents(struct cert_key_and_chain *ckch) |
| 2953 | { |
| 2954 | int i; |
| 2955 | |
| 2956 | if (!ckch) |
| 2957 | return; |
| 2958 | |
| 2959 | /* Free the certificate and set pointer to NULL */ |
| 2960 | if (ckch->cert) |
| 2961 | X509_free(ckch->cert); |
| 2962 | ckch->cert = NULL; |
| 2963 | |
| 2964 | /* Free the key and set pointer to NULL */ |
| 2965 | if (ckch->key) |
| 2966 | EVP_PKEY_free(ckch->key); |
| 2967 | ckch->key = NULL; |
| 2968 | |
| 2969 | /* Free each certificate in the chain */ |
| 2970 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 2971 | if (ckch->chain_certs[i]) |
| 2972 | X509_free(ckch->chain_certs[i]); |
| 2973 | } |
| 2974 | |
| 2975 | /* Free the chain obj itself and set to NULL */ |
| 2976 | if (ckch->num_chain_certs > 0) { |
| 2977 | free(ckch->chain_certs); |
| 2978 | ckch->num_chain_certs = 0; |
| 2979 | ckch->chain_certs = NULL; |
| 2980 | } |
| 2981 | |
| 2982 | } |
| 2983 | |
| 2984 | /* checks if a key and cert exists in the ckch |
| 2985 | */ |
| 2986 | static int ssl_sock_is_ckch_valid(struct cert_key_and_chain *ckch) |
| 2987 | { |
| 2988 | return (ckch->cert != NULL && ckch->key != NULL); |
| 2989 | } |
| 2990 | |
| 2991 | |
| 2992 | /* Loads the contents of a crt file (path) into a cert_key_and_chain |
| 2993 | * This allows us to carry the contents of the file without having to |
| 2994 | * read the file multiple times. |
| 2995 | * |
| 2996 | * returns: |
| 2997 | * 0 on Success |
| 2998 | * 1 on SSL Failure |
| 2999 | * 2 on file not found |
| 3000 | */ |
| 3001 | static int ssl_sock_load_crt_file_into_ckch(const char *path, struct cert_key_and_chain *ckch, char **err) |
| 3002 | { |
| 3003 | |
| 3004 | BIO *in; |
| 3005 | X509 *ca = NULL; |
| 3006 | int ret = 1; |
| 3007 | |
| 3008 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 3009 | |
| 3010 | in = BIO_new(BIO_s_file()); |
| 3011 | if (in == NULL) |
| 3012 | goto end; |
| 3013 | |
| 3014 | if (BIO_read_filename(in, path) <= 0) |
| 3015 | goto end; |
| 3016 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3017 | /* Read Private Key */ |
| 3018 | ckch->key = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL); |
| 3019 | if (ckch->key == NULL) { |
| 3020 | memprintf(err, "%sunable to load private key from file '%s'.\n", |
| 3021 | err && *err ? *err : "", path); |
| 3022 | goto end; |
| 3023 | } |
| 3024 | |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3025 | /* Seek back to beginning of file */ |
Thierry FOURNIER / OZON.IO | d44ea3f | 2016-10-14 00:49:21 +0200 | [diff] [blame] | 3026 | if (BIO_reset(in) == -1) { |
| 3027 | memprintf(err, "%san error occurred while reading the file '%s'.\n", |
| 3028 | err && *err ? *err : "", path); |
| 3029 | goto end; |
| 3030 | } |
Willy Tarreau | bb137a8 | 2016-04-06 19:02:38 +0200 | [diff] [blame] | 3031 | |
| 3032 | /* Read Certificate */ |
| 3033 | ckch->cert = PEM_read_bio_X509_AUX(in, NULL, NULL, NULL); |
| 3034 | if (ckch->cert == NULL) { |
| 3035 | memprintf(err, "%sunable to load certificate from file '%s'.\n", |
| 3036 | err && *err ? *err : "", path); |
| 3037 | goto end; |
| 3038 | } |
| 3039 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3040 | /* Read Certificate Chain */ |
| 3041 | while ((ca = PEM_read_bio_X509(in, NULL, NULL, NULL))) { |
| 3042 | /* Grow the chain certs */ |
| 3043 | ckch->num_chain_certs++; |
| 3044 | ckch->chain_certs = realloc(ckch->chain_certs, (ckch->num_chain_certs * sizeof(X509 *))); |
| 3045 | |
| 3046 | /* use - 1 here since we just incremented it above */ |
| 3047 | ckch->chain_certs[ckch->num_chain_certs - 1] = ca; |
| 3048 | } |
| 3049 | ret = ERR_get_error(); |
| 3050 | if (ret && (ERR_GET_LIB(ret) != ERR_LIB_PEM && ERR_GET_REASON(ret) != PEM_R_NO_START_LINE)) { |
| 3051 | memprintf(err, "%sunable to load certificate chain from file '%s'.\n", |
| 3052 | err && *err ? *err : "", path); |
| 3053 | ret = 1; |
| 3054 | goto end; |
| 3055 | } |
| 3056 | |
| 3057 | ret = 0; |
| 3058 | |
| 3059 | end: |
| 3060 | |
| 3061 | ERR_clear_error(); |
| 3062 | if (in) |
| 3063 | BIO_free(in); |
| 3064 | |
| 3065 | /* Something went wrong in one of the reads */ |
| 3066 | if (ret != 0) |
| 3067 | ssl_sock_free_cert_key_and_chain_contents(ckch); |
| 3068 | |
| 3069 | return ret; |
| 3070 | } |
| 3071 | |
| 3072 | /* Loads the info in ckch into ctx |
| 3073 | * Currently, this does not process any information about ocsp, dhparams or |
| 3074 | * sctl |
| 3075 | * Returns |
| 3076 | * 0 on success |
| 3077 | * 1 on failure |
| 3078 | */ |
| 3079 | static int ssl_sock_put_ckch_into_ctx(const char *path, const struct cert_key_and_chain *ckch, SSL_CTX *ctx, char **err) |
| 3080 | { |
| 3081 | int i = 0; |
| 3082 | |
| 3083 | if (SSL_CTX_use_PrivateKey(ctx, ckch->key) <= 0) { |
| 3084 | memprintf(err, "%sunable to load SSL private key into SSL Context '%s'.\n", |
| 3085 | err && *err ? *err : "", path); |
| 3086 | return 1; |
| 3087 | } |
| 3088 | |
| 3089 | if (!SSL_CTX_use_certificate(ctx, ckch->cert)) { |
| 3090 | memprintf(err, "%sunable to load SSL certificate into SSL Context '%s'.\n", |
| 3091 | err && *err ? *err : "", path); |
| 3092 | return 1; |
| 3093 | } |
| 3094 | |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3095 | /* Load all certs in the ckch into the ctx_chain for the ssl_ctx */ |
| 3096 | for (i = 0; i < ckch->num_chain_certs; i++) { |
| 3097 | if (!SSL_CTX_add1_chain_cert(ctx, ckch->chain_certs[i])) { |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3098 | memprintf(err, "%sunable to load chain certificate #%d into SSL Context '%s'. Make sure you are linking against Openssl >= 1.0.2.\n", |
| 3099 | err && *err ? *err : "", (i+1), path); |
yanbzhu | 488a4d2 | 2015-12-01 15:16:07 -0500 | [diff] [blame] | 3100 | return 1; |
| 3101 | } |
| 3102 | } |
| 3103 | |
| 3104 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3105 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3106 | err && *err ? *err : "", path); |
| 3107 | return 1; |
| 3108 | } |
| 3109 | |
| 3110 | return 0; |
| 3111 | } |
| 3112 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3113 | |
| 3114 | static void ssl_sock_populate_sni_keytypes_hplr(const char *str, struct eb_root *sni_keytypes, int key_index) |
| 3115 | { |
| 3116 | struct sni_keytype *s_kt = NULL; |
| 3117 | struct ebmb_node *node; |
| 3118 | int i; |
| 3119 | |
| 3120 | for (i = 0; i < trash.size; i++) { |
| 3121 | if (!str[i]) |
| 3122 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3123 | trash.area[i] = tolower(str[i]); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3124 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3125 | trash.area[i] = 0; |
| 3126 | node = ebst_lookup(sni_keytypes, trash.area); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3127 | if (!node) { |
| 3128 | /* CN not found in tree */ |
| 3129 | s_kt = malloc(sizeof(struct sni_keytype) + i + 1); |
| 3130 | /* Using memcpy here instead of strncpy. |
| 3131 | * strncpy will cause sig_abrt errors under certain versions of gcc with -O2 |
| 3132 | * See: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=60792 |
| 3133 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3134 | memcpy(s_kt->name.key, trash.area, i+1); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3135 | s_kt->keytypes = 0; |
| 3136 | ebst_insert(sni_keytypes, &s_kt->name); |
| 3137 | } else { |
| 3138 | /* CN found in tree */ |
| 3139 | s_kt = container_of(node, struct sni_keytype, name); |
| 3140 | } |
| 3141 | |
| 3142 | /* Mark that this CN has the keytype of key_index via keytypes mask */ |
| 3143 | s_kt->keytypes |= 1<<key_index; |
| 3144 | |
| 3145 | } |
| 3146 | |
| 3147 | |
| 3148 | /* Given a path that does not exist, try to check for path.rsa, path.dsa and path.ecdsa files. |
| 3149 | * If any are found, group these files into a set of SSL_CTX* |
| 3150 | * based on shared and unique CN and SAN entries. Add these SSL_CTX* to the SNI tree. |
| 3151 | * |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3152 | * 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] | 3153 | * |
| 3154 | * Returns |
| 3155 | * 0 on success |
| 3156 | * 1 on failure |
| 3157 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3158 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3159 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3160 | { |
| 3161 | char fp[MAXPATHLEN+1] = {0}; |
| 3162 | int n = 0; |
| 3163 | int i = 0; |
| 3164 | struct cert_key_and_chain certs_and_keys[SSL_SOCK_NUM_KEYTYPES] = { {0} }; |
| 3165 | struct eb_root sni_keytypes_map = { {0} }; |
| 3166 | struct ebmb_node *node; |
| 3167 | struct ebmb_node *next; |
| 3168 | /* Array of SSL_CTX pointers corresponding to each possible combo |
| 3169 | * of keytypes |
| 3170 | */ |
| 3171 | struct key_combo_ctx key_combos[SSL_SOCK_POSSIBLE_KT_COMBOS] = { {0} }; |
| 3172 | int rv = 0; |
| 3173 | X509_NAME *xname = NULL; |
| 3174 | char *str = NULL; |
| 3175 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3176 | STACK_OF(GENERAL_NAME) *names = NULL; |
| 3177 | #endif |
| 3178 | |
| 3179 | /* Load all possible certs and keys */ |
| 3180 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3181 | struct stat buf; |
| 3182 | |
| 3183 | snprintf(fp, sizeof(fp), "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3184 | if (stat(fp, &buf) == 0) { |
| 3185 | if (ssl_sock_load_crt_file_into_ckch(fp, &certs_and_keys[n], err) == 1) { |
| 3186 | rv = 1; |
| 3187 | goto end; |
| 3188 | } |
| 3189 | } |
| 3190 | } |
| 3191 | |
| 3192 | /* Process each ckch and update keytypes for each CN/SAN |
| 3193 | * for example, if CN/SAN www.a.com is associated with |
| 3194 | * certs with keytype 0 and 2, then at the end of the loop, |
| 3195 | * www.a.com will have: |
| 3196 | * keyindex = 0 | 1 | 4 = 5 |
| 3197 | */ |
| 3198 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3199 | |
| 3200 | if (!ssl_sock_is_ckch_valid(&certs_and_keys[n])) |
| 3201 | continue; |
| 3202 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3203 | if (fcount) { |
Willy Tarreau | 24b892f | 2016-06-20 23:01:57 +0200 | [diff] [blame] | 3204 | for (i = 0; i < fcount; i++) |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3205 | ssl_sock_populate_sni_keytypes_hplr(sni_filter[i], &sni_keytypes_map, n); |
| 3206 | } else { |
| 3207 | /* A lot of the following code is OpenSSL boilerplate for processing CN's and SAN's, |
| 3208 | * so the line that contains logic is marked via comments |
| 3209 | */ |
| 3210 | xname = X509_get_subject_name(certs_and_keys[n].cert); |
| 3211 | i = -1; |
| 3212 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3213 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3214 | ASN1_STRING *value; |
| 3215 | value = X509_NAME_ENTRY_get_data(entry); |
| 3216 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3217 | /* Important line is here */ |
| 3218 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3219 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3220 | OPENSSL_free(str); |
| 3221 | str = NULL; |
| 3222 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3223 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3224 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3225 | /* Do the above logic for each SAN */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3226 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3227 | names = X509_get_ext_d2i(certs_and_keys[n].cert, NID_subject_alt_name, NULL, NULL); |
| 3228 | if (names) { |
| 3229 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3230 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3231 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3232 | if (name->type == GEN_DNS) { |
| 3233 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
| 3234 | /* Important line is here */ |
| 3235 | ssl_sock_populate_sni_keytypes_hplr(str, &sni_keytypes_map, n); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3236 | |
Emmanuel Hocdet | d294aea | 2016-05-13 11:14:06 +0200 | [diff] [blame] | 3237 | OPENSSL_free(str); |
| 3238 | str = NULL; |
| 3239 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3240 | } |
| 3241 | } |
| 3242 | } |
| 3243 | } |
| 3244 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
| 3245 | } |
| 3246 | |
| 3247 | /* If no files found, return error */ |
| 3248 | if (eb_is_empty(&sni_keytypes_map)) { |
| 3249 | memprintf(err, "%sunable to load SSL certificate file '%s' file does not exist.\n", |
| 3250 | err && *err ? *err : "", path); |
| 3251 | rv = 1; |
| 3252 | goto end; |
| 3253 | } |
| 3254 | |
| 3255 | /* We now have a map of CN/SAN to keytypes that are loaded in |
| 3256 | * Iterate through the map to create the SSL_CTX's (if needed) |
| 3257 | * and add each CTX to the SNI tree |
| 3258 | * |
| 3259 | * Some math here: |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 3260 | * There are 2^n - 1 possible combinations, each unique |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3261 | * combination is denoted by the key in the map. Each key |
| 3262 | * has a value between 1 and 2^n - 1. Conveniently, the array |
| 3263 | * of SSL_CTX* is sized 2^n. So, we can simply use the i'th |
| 3264 | * entry in the array to correspond to the unique combo (key) |
| 3265 | * associated with i. This unique key combo (i) will be associated |
| 3266 | * with combos[i-1] |
| 3267 | */ |
| 3268 | |
| 3269 | node = ebmb_first(&sni_keytypes_map); |
| 3270 | while (node) { |
| 3271 | SSL_CTX *cur_ctx; |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3272 | char cur_file[MAXPATHLEN+1]; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3273 | const struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3274 | |
| 3275 | str = (char *)container_of(node, struct sni_keytype, name)->name.key; |
| 3276 | i = container_of(node, struct sni_keytype, name)->keytypes; |
| 3277 | cur_ctx = key_combos[i-1].ctx; |
| 3278 | |
| 3279 | if (cur_ctx == NULL) { |
| 3280 | /* need to create SSL_CTX */ |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3281 | cur_ctx = SSL_CTX_new(SSLv23_server_method()); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3282 | if (cur_ctx == NULL) { |
| 3283 | memprintf(err, "%sunable to allocate SSL context.\n", |
| 3284 | err && *err ? *err : ""); |
| 3285 | rv = 1; |
| 3286 | goto end; |
| 3287 | } |
| 3288 | |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3289 | /* Load all required certs/keys/chains/OCSPs info into SSL_CTX */ |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3290 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) { |
| 3291 | if (i & (1<<n)) { |
| 3292 | /* Key combo contains ckch[n] */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3293 | snprintf(cur_file, MAXPATHLEN+1, "%s.%s", path, SSL_SOCK_KEYTYPE_NAMES[n]); |
| 3294 | 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] | 3295 | SSL_CTX_free(cur_ctx); |
| 3296 | rv = 1; |
| 3297 | goto end; |
| 3298 | } |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3299 | |
| 3300 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 3301 | /* Load OCSP Info into context */ |
Bertrand Jacquin | 3342309 | 2016-11-13 16:37:13 +0000 | [diff] [blame] | 3302 | if (ssl_sock_load_ocsp(cur_ctx, cur_file) < 0) { |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3303 | if (err) |
| 3304 | 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] | 3305 | *err ? *err : "", cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3306 | SSL_CTX_free(cur_ctx); |
| 3307 | rv = 1; |
| 3308 | goto end; |
| 3309 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3310 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3311 | ssl_sock_set_ocsp_response_from_file(cur_ctx, cur_file); |
yanbzhu | be2774d | 2015-12-10 15:07:30 -0500 | [diff] [blame] | 3312 | #endif |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3313 | } |
| 3314 | } |
| 3315 | |
| 3316 | /* Load DH params into the ctx to support DHE keys */ |
| 3317 | #ifndef OPENSSL_NO_DH |
| 3318 | if (ssl_dh_ptr_index >= 0) |
| 3319 | SSL_CTX_set_ex_data(cur_ctx, ssl_dh_ptr_index, NULL); |
| 3320 | |
| 3321 | rv = ssl_sock_load_dh_params(cur_ctx, NULL); |
| 3322 | if (rv < 0) { |
| 3323 | if (err) |
| 3324 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3325 | *err ? *err : "", path); |
| 3326 | rv = 1; |
| 3327 | goto end; |
| 3328 | } |
| 3329 | #endif |
| 3330 | |
| 3331 | /* Update key_combos */ |
| 3332 | key_combos[i-1].ctx = cur_ctx; |
| 3333 | } |
| 3334 | |
| 3335 | /* Update SNI Tree */ |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3336 | 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] | 3337 | kinfo, str, key_combos[i-1].order); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3338 | node = ebmb_next(node); |
| 3339 | } |
| 3340 | |
| 3341 | |
| 3342 | /* Mark a default context if none exists, using the ctx that has the most shared keys */ |
| 3343 | if (!bind_conf->default_ctx) { |
| 3344 | for (i = SSL_SOCK_POSSIBLE_KT_COMBOS - 1; i >= 0; i--) { |
| 3345 | if (key_combos[i].ctx) { |
| 3346 | bind_conf->default_ctx = key_combos[i].ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3347 | bind_conf->default_ssl_conf = ssl_conf; |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3348 | break; |
| 3349 | } |
| 3350 | } |
| 3351 | } |
| 3352 | |
| 3353 | end: |
| 3354 | |
| 3355 | if (names) |
| 3356 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
| 3357 | |
| 3358 | for (n = 0; n < SSL_SOCK_NUM_KEYTYPES; n++) |
| 3359 | ssl_sock_free_cert_key_and_chain_contents(&certs_and_keys[n]); |
| 3360 | |
| 3361 | node = ebmb_first(&sni_keytypes_map); |
| 3362 | while (node) { |
| 3363 | next = ebmb_next(node); |
| 3364 | ebmb_delete(node); |
| 3365 | node = next; |
| 3366 | } |
| 3367 | |
| 3368 | return rv; |
| 3369 | } |
| 3370 | #else |
| 3371 | /* This is a dummy, that just logs an error and returns error */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3372 | static int ssl_sock_load_multi_cert(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3373 | char **sni_filter, int fcount, char **err) |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3374 | { |
| 3375 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3376 | err && *err ? *err : "", path, strerror(errno)); |
| 3377 | return 1; |
| 3378 | } |
| 3379 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3380 | #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] | 3381 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3382 | /* Loads a certificate key and CA chain from a file. Returns 0 on error, -1 if |
| 3383 | * an early error happens and the caller must call SSL_CTX_free() by itelf. |
| 3384 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3385 | static int ssl_sock_load_cert_chain_file(SSL_CTX *ctx, const char *file, struct bind_conf *s, |
| 3386 | struct ssl_bind_conf *ssl_conf, char **sni_filter, int fcount) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3387 | { |
| 3388 | BIO *in; |
| 3389 | X509 *x = NULL, *ca; |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3390 | int i, err; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3391 | int ret = -1; |
| 3392 | int order = 0; |
| 3393 | X509_NAME *xname; |
| 3394 | char *str; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3395 | pem_password_cb *passwd_cb; |
| 3396 | void *passwd_cb_userdata; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3397 | EVP_PKEY *pkey; |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3398 | struct pkey_info kinfo = { .sig = TLSEXT_signature_anonymous, .bits = 0 }; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3399 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3400 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 3401 | STACK_OF(GENERAL_NAME) *names; |
| 3402 | #endif |
| 3403 | |
| 3404 | in = BIO_new(BIO_s_file()); |
| 3405 | if (in == NULL) |
| 3406 | goto end; |
| 3407 | |
| 3408 | if (BIO_read_filename(in, file) <= 0) |
| 3409 | goto end; |
| 3410 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3411 | |
| 3412 | passwd_cb = SSL_CTX_get_default_passwd_cb(ctx); |
| 3413 | passwd_cb_userdata = SSL_CTX_get_default_passwd_cb_userdata(ctx); |
| 3414 | |
| 3415 | x = PEM_read_bio_X509_AUX(in, NULL, passwd_cb, passwd_cb_userdata); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3416 | if (x == NULL) |
| 3417 | goto end; |
| 3418 | |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3419 | pkey = X509_get_pubkey(x); |
| 3420 | if (pkey) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3421 | kinfo.bits = EVP_PKEY_bits(pkey); |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3422 | switch(EVP_PKEY_base_id(pkey)) { |
| 3423 | case EVP_PKEY_RSA: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3424 | kinfo.sig = TLSEXT_signature_rsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3425 | break; |
| 3426 | case EVP_PKEY_EC: |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3427 | kinfo.sig = TLSEXT_signature_ecdsa; |
| 3428 | break; |
| 3429 | case EVP_PKEY_DSA: |
| 3430 | kinfo.sig = TLSEXT_signature_dsa; |
Emmanuel Hocdet | 0594211 | 2017-02-20 16:11:50 +0100 | [diff] [blame] | 3431 | break; |
| 3432 | } |
| 3433 | EVP_PKEY_free(pkey); |
| 3434 | } |
| 3435 | |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3436 | if (fcount) { |
| 3437 | while (fcount--) |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3438 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, sni_filter[fcount], order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3439 | } |
| 3440 | else { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3441 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3442 | names = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 3443 | if (names) { |
| 3444 | for (i = 0; i < sk_GENERAL_NAME_num(names); i++) { |
| 3445 | GENERAL_NAME *name = sk_GENERAL_NAME_value(names, i); |
| 3446 | if (name->type == GEN_DNS) { |
| 3447 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3448 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3449 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3450 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3451 | } |
| 3452 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3453 | sk_GENERAL_NAME_pop_free(names, GENERAL_NAME_free); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3454 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3455 | #endif /* SSL_CTRL_SET_TLSEXT_HOSTNAME */ |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3456 | xname = X509_get_subject_name(x); |
| 3457 | i = -1; |
| 3458 | while ((i = X509_NAME_get_index_by_NID(xname, NID_commonName, i)) != -1) { |
| 3459 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(xname, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3460 | ASN1_STRING *value; |
| 3461 | |
| 3462 | value = X509_NAME_ENTRY_get_data(entry); |
| 3463 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Emmanuel Hocdet | ddc090b | 2017-10-27 18:43:29 +0200 | [diff] [blame] | 3464 | order = ssl_sock_add_cert_sni(ctx, s, ssl_conf, kinfo, str, order); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3465 | OPENSSL_free(str); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3466 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3467 | } |
| 3468 | } |
| 3469 | |
| 3470 | ret = 0; /* the caller must not free the SSL_CTX argument anymore */ |
| 3471 | if (!SSL_CTX_use_certificate(ctx, x)) |
| 3472 | goto end; |
| 3473 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3474 | #ifdef SSL_CTX_clear_extra_chain_certs |
| 3475 | SSL_CTX_clear_extra_chain_certs(ctx); |
| 3476 | #else |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3477 | if (ctx->extra_certs != NULL) { |
| 3478 | sk_X509_pop_free(ctx->extra_certs, X509_free); |
| 3479 | ctx->extra_certs = NULL; |
| 3480 | } |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3481 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3482 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 3483 | while ((ca = PEM_read_bio_X509(in, NULL, passwd_cb, passwd_cb_userdata))) { |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3484 | if (!SSL_CTX_add_extra_chain_cert(ctx, ca)) { |
| 3485 | X509_free(ca); |
| 3486 | goto end; |
| 3487 | } |
| 3488 | } |
| 3489 | |
| 3490 | err = ERR_get_error(); |
| 3491 | if (!err || (ERR_GET_LIB(err) == ERR_LIB_PEM && ERR_GET_REASON(err) == PEM_R_NO_START_LINE)) { |
| 3492 | /* we successfully reached the last cert in the file */ |
| 3493 | ret = 1; |
| 3494 | } |
| 3495 | ERR_clear_error(); |
| 3496 | |
| 3497 | end: |
| 3498 | if (x) |
| 3499 | X509_free(x); |
| 3500 | |
| 3501 | if (in) |
| 3502 | BIO_free(in); |
| 3503 | |
| 3504 | return ret; |
| 3505 | } |
| 3506 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3507 | static int ssl_sock_load_cert_file(const char *path, struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, |
| 3508 | char **sni_filter, int fcount, char **err) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3509 | { |
| 3510 | int ret; |
| 3511 | SSL_CTX *ctx; |
| 3512 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3513 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3514 | if (!ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3515 | memprintf(err, "%sunable to allocate SSL context for cert '%s'.\n", |
| 3516 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3517 | return 1; |
| 3518 | } |
| 3519 | |
| 3520 | if (SSL_CTX_use_PrivateKey_file(ctx, path, SSL_FILETYPE_PEM) <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3521 | memprintf(err, "%sunable to load SSL private key from PEM file '%s'.\n", |
| 3522 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3523 | SSL_CTX_free(ctx); |
| 3524 | return 1; |
| 3525 | } |
| 3526 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3527 | ret = ssl_sock_load_cert_chain_file(ctx, path, bind_conf, ssl_conf, sni_filter, fcount); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3528 | if (ret <= 0) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3529 | memprintf(err, "%sunable to load SSL certificate from PEM file '%s'.\n", |
| 3530 | err && *err ? *err : "", path); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3531 | if (ret < 0) /* serious error, must do that ourselves */ |
| 3532 | SSL_CTX_free(ctx); |
| 3533 | return 1; |
| 3534 | } |
Emeric Brun | 61694ab | 2012-10-26 13:35:33 +0200 | [diff] [blame] | 3535 | |
| 3536 | if (SSL_CTX_check_private_key(ctx) <= 0) { |
| 3537 | memprintf(err, "%sinconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 3538 | err && *err ? *err : "", path); |
| 3539 | return 1; |
| 3540 | } |
| 3541 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3542 | /* we must not free the SSL_CTX anymore below, since it's already in |
| 3543 | * the tree, so it will be discovered and cleaned in time. |
| 3544 | */ |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3545 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 3546 | /* store a NULL pointer to indicate we have not yet loaded |
| 3547 | a custom DH param file */ |
| 3548 | if (ssl_dh_ptr_index >= 0) { |
| 3549 | SSL_CTX_set_ex_data(ctx, ssl_dh_ptr_index, NULL); |
| 3550 | } |
| 3551 | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3552 | ret = ssl_sock_load_dh_params(ctx, path); |
| 3553 | if (ret < 0) { |
| 3554 | if (err) |
| 3555 | memprintf(err, "%sunable to load DH parameters from file '%s'.\n", |
| 3556 | *err ? *err : "", path); |
| 3557 | return 1; |
| 3558 | } |
| 3559 | #endif |
| 3560 | |
Lukas Tribus | e4e30f7 | 2014-12-09 16:32:51 +0100 | [diff] [blame] | 3561 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3562 | ret = ssl_sock_load_ocsp(ctx, path); |
| 3563 | if (ret < 0) { |
| 3564 | if (err) |
| 3565 | 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", |
| 3566 | *err ? *err : "", path); |
| 3567 | return 1; |
| 3568 | } |
Emmanuel Hocdet | 2c32d8f | 2017-05-22 14:58:00 +0200 | [diff] [blame] | 3569 | #elif (defined OPENSSL_IS_BORINGSSL) |
| 3570 | ssl_sock_set_ocsp_response_from_file(ctx, path); |
Emeric Brun | 4147b2e | 2014-06-16 18:36:30 +0200 | [diff] [blame] | 3571 | #endif |
| 3572 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3573 | #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] | 3574 | if (sctl_ex_index >= 0) { |
| 3575 | ret = ssl_sock_load_sctl(ctx, path); |
| 3576 | if (ret < 0) { |
| 3577 | if (err) |
| 3578 | memprintf(err, "%s '%s.sctl' is present but cannot be read or parsed'.\n", |
| 3579 | *err ? *err : "", path); |
| 3580 | return 1; |
| 3581 | } |
| 3582 | } |
| 3583 | #endif |
| 3584 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3585 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3586 | if (bind_conf->default_ctx) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 3587 | memprintf(err, "%sthis version of openssl cannot load multiple SSL certificates.\n", |
| 3588 | err && *err ? *err : ""); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3589 | return 1; |
| 3590 | } |
| 3591 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3592 | if (!bind_conf->default_ctx) { |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 3593 | bind_conf->default_ctx = ctx; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3594 | bind_conf->default_ssl_conf = ssl_conf; |
| 3595 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3596 | |
| 3597 | return 0; |
| 3598 | } |
| 3599 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 3600 | 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] | 3601 | { |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3602 | struct dirent **de_list; |
| 3603 | int i, n; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3604 | DIR *dir; |
| 3605 | struct stat buf; |
Willy Tarreau | ee2663b | 2012-12-06 11:36:59 +0100 | [diff] [blame] | 3606 | char *end; |
| 3607 | char fp[MAXPATHLEN+1]; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3608 | int cfgerr = 0; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3609 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3610 | int is_bundle; |
| 3611 | int j; |
| 3612 | #endif |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3613 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3614 | if (stat(path, &buf) == 0) { |
| 3615 | dir = opendir(path); |
| 3616 | if (!dir) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3617 | return ssl_sock_load_cert_file(path, bind_conf, NULL, NULL, 0, err); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3618 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3619 | /* strip trailing slashes, including first one */ |
| 3620 | for (end = path + strlen(path) - 1; end >= path && *end == '/'; end--) |
| 3621 | *end = 0; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3622 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3623 | n = scandir(path, &de_list, 0, alphasort); |
| 3624 | if (n < 0) { |
| 3625 | memprintf(err, "%sunable to scan directory '%s' : %s.\n", |
| 3626 | err && *err ? *err : "", path, strerror(errno)); |
| 3627 | cfgerr++; |
| 3628 | } |
| 3629 | else { |
| 3630 | for (i = 0; i < n; i++) { |
| 3631 | struct dirent *de = de_list[i]; |
Emeric Brun | 2aab722 | 2014-06-18 18:15:09 +0200 | [diff] [blame] | 3632 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3633 | end = strrchr(de->d_name, '.'); |
| 3634 | if (end && (!strcmp(end, ".issuer") || !strcmp(end, ".ocsp") || !strcmp(end, ".sctl"))) |
| 3635 | goto ignore_entry; |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3636 | |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3637 | snprintf(fp, sizeof(fp), "%s/%s", path, de->d_name); |
| 3638 | if (stat(fp, &buf) != 0) { |
| 3639 | memprintf(err, "%sunable to stat SSL certificate from file '%s' : %s.\n", |
| 3640 | err && *err ? *err : "", fp, strerror(errno)); |
| 3641 | cfgerr++; |
| 3642 | goto ignore_entry; |
| 3643 | } |
| 3644 | if (!S_ISREG(buf.st_mode)) |
| 3645 | goto ignore_entry; |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3646 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3647 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3648 | is_bundle = 0; |
| 3649 | /* Check if current entry in directory is part of a multi-cert bundle */ |
| 3650 | |
| 3651 | if (end) { |
| 3652 | for (j = 0; j < SSL_SOCK_NUM_KEYTYPES; j++) { |
| 3653 | if (!strcmp(end + 1, SSL_SOCK_KEYTYPE_NAMES[j])) { |
| 3654 | is_bundle = 1; |
| 3655 | break; |
| 3656 | } |
| 3657 | } |
| 3658 | |
| 3659 | if (is_bundle) { |
| 3660 | char dp[MAXPATHLEN+1] = {0}; /* this will be the filename w/o the keytype */ |
| 3661 | int dp_len; |
| 3662 | |
| 3663 | dp_len = end - de->d_name; |
| 3664 | snprintf(dp, dp_len + 1, "%s", de->d_name); |
| 3665 | |
| 3666 | /* increment i and free de until we get to a non-bundle cert |
| 3667 | * Note here that we look at de_list[i + 1] before freeing de |
| 3668 | * this is important since ignore_entry will free de |
| 3669 | */ |
| 3670 | while (i + 1 < n && !strncmp(de_list[i + 1]->d_name, dp, dp_len)) { |
| 3671 | free(de); |
| 3672 | i++; |
| 3673 | de = de_list[i]; |
| 3674 | } |
| 3675 | |
| 3676 | snprintf(fp, sizeof(fp), "%s/%s", path, dp); |
Emeric Brun | eb155b6 | 2018-08-16 15:11:12 +0200 | [diff] [blame] | 3677 | cfgerr += ssl_sock_load_multi_cert(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 63ea846 | 2015-12-09 13:35:14 -0500 | [diff] [blame] | 3678 | |
| 3679 | /* Successfully processed the bundle */ |
| 3680 | goto ignore_entry; |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3685 | cfgerr += ssl_sock_load_cert_file(fp, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3686 | ignore_entry: |
| 3687 | free(de); |
Cyril Bonté | 3180f7b | 2015-01-25 00:16:08 +0100 | [diff] [blame] | 3688 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3689 | free(de_list); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3690 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3691 | closedir(dir); |
| 3692 | return cfgerr; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3693 | } |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3694 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3695 | cfgerr = ssl_sock_load_multi_cert(path, bind_conf, NULL, NULL, 0, err); |
yanbzhu | 08ce6ab | 2015-12-02 13:01:29 -0500 | [diff] [blame] | 3696 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3697 | return cfgerr; |
| 3698 | } |
| 3699 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 3700 | /* Make sure openssl opens /dev/urandom before the chroot. The work is only |
| 3701 | * done once. Zero is returned if the operation fails. No error is returned |
| 3702 | * if the random is said as not implemented, because we expect that openssl |
| 3703 | * will use another method once needed. |
| 3704 | */ |
| 3705 | static int ssl_initialize_random() |
| 3706 | { |
| 3707 | unsigned char random; |
| 3708 | static int random_initialized = 0; |
| 3709 | |
| 3710 | if (!random_initialized && RAND_bytes(&random, 1) != 0) |
| 3711 | random_initialized = 1; |
| 3712 | |
| 3713 | return random_initialized; |
| 3714 | } |
| 3715 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3716 | /* release ssl bind conf */ |
| 3717 | void ssl_sock_free_ssl_conf(struct ssl_bind_conf *conf) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3718 | { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3719 | if (conf) { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 3720 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3721 | free(conf->npn_str); |
| 3722 | conf->npn_str = NULL; |
| 3723 | #endif |
| 3724 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 3725 | free(conf->alpn_str); |
| 3726 | conf->alpn_str = NULL; |
| 3727 | #endif |
| 3728 | free(conf->ca_file); |
| 3729 | conf->ca_file = NULL; |
| 3730 | free(conf->crl_file); |
| 3731 | conf->crl_file = NULL; |
| 3732 | free(conf->ciphers); |
| 3733 | conf->ciphers = NULL; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3734 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 3735 | free(conf->ciphersuites); |
| 3736 | conf->ciphersuites = NULL; |
| 3737 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 3738 | free(conf->curves); |
| 3739 | conf->curves = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3740 | free(conf->ecdhe); |
| 3741 | conf->ecdhe = NULL; |
| 3742 | } |
| 3743 | } |
| 3744 | |
| 3745 | int ssl_sock_load_cert_list_file(char *file, struct bind_conf *bind_conf, struct proxy *curproxy, char **err) |
| 3746 | { |
| 3747 | char thisline[CRT_LINESIZE]; |
| 3748 | char path[MAXPATHLEN+1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3749 | FILE *f; |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3750 | struct stat buf; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3751 | int linenum = 0; |
| 3752 | int cfgerr = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3753 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3754 | if ((f = fopen(file, "r")) == NULL) { |
| 3755 | memprintf(err, "cannot open file '%s' : %s", file, strerror(errno)); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3756 | return 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3757 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3758 | |
| 3759 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3760 | int arg, newarg, cur_arg, i, ssl_b = 0, ssl_e = 0; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3761 | char *end; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3762 | char *args[MAX_CRT_ARGS + 1]; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3763 | char *line = thisline; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3764 | char *crt_path; |
| 3765 | struct ssl_bind_conf *ssl_conf = NULL; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3766 | |
| 3767 | linenum++; |
| 3768 | end = line + strlen(line); |
| 3769 | if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') { |
| 3770 | /* Check if we reached the limit and the last char is not \n. |
| 3771 | * Watch out for the last line without the terminating '\n'! |
| 3772 | */ |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3773 | memprintf(err, "line %d too long in file '%s', limit is %d characters", |
| 3774 | linenum, file, (int)sizeof(thisline)-1); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3775 | cfgerr = 1; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3776 | break; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3777 | } |
| 3778 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3779 | arg = 0; |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3780 | newarg = 1; |
| 3781 | while (*line) { |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3782 | if (*line == '#' || *line == '\n' || *line == '\r') { |
| 3783 | /* end of string, end of loop */ |
| 3784 | *line = 0; |
| 3785 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3786 | } else if (isspace(*line)) { |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3787 | newarg = 1; |
| 3788 | *line = 0; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3789 | } else if (*line == '[') { |
| 3790 | if (ssl_b) { |
| 3791 | memprintf(err, "too many '[' on line %d in file '%s'.", linenum, file); |
| 3792 | cfgerr = 1; |
| 3793 | break; |
| 3794 | } |
| 3795 | if (!arg) { |
| 3796 | memprintf(err, "file must start with a cert on line %d in file '%s'", linenum, file); |
| 3797 | cfgerr = 1; |
| 3798 | break; |
| 3799 | } |
| 3800 | ssl_b = arg; |
| 3801 | newarg = 1; |
| 3802 | *line = 0; |
| 3803 | } else if (*line == ']') { |
| 3804 | if (ssl_e) { |
| 3805 | memprintf(err, "too many ']' on line %d in file '%s'.", linenum, file); |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3806 | cfgerr = 1; |
| 3807 | break; |
| 3808 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3809 | if (!ssl_b) { |
| 3810 | memprintf(err, "missing '[' in line %d in file '%s'.", linenum, file); |
| 3811 | cfgerr = 1; |
| 3812 | break; |
| 3813 | } |
| 3814 | ssl_e = arg; |
| 3815 | newarg = 1; |
| 3816 | *line = 0; |
| 3817 | } else if (newarg) { |
| 3818 | if (arg == MAX_CRT_ARGS) { |
| 3819 | memprintf(err, "too many args on line %d in file '%s'.", linenum, file); |
| 3820 | cfgerr = 1; |
| 3821 | break; |
| 3822 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3823 | newarg = 0; |
| 3824 | args[arg++] = line; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3825 | } |
Emeric Brun | 50bcecc | 2013-04-22 13:05:23 +0200 | [diff] [blame] | 3826 | line++; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3827 | } |
Emmanuel Hocdet | 7c41a1b | 2013-05-07 20:20:06 +0200 | [diff] [blame] | 3828 | if (cfgerr) |
| 3829 | break; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3830 | args[arg++] = line; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3831 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3832 | /* empty line */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3833 | if (!*args[0]) |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3834 | continue; |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3835 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3836 | crt_path = args[0]; |
| 3837 | if (*crt_path != '/' && global_ssl.crt_base) { |
| 3838 | if ((strlen(global_ssl.crt_base) + 1 + strlen(crt_path)) > MAXPATHLEN) { |
| 3839 | memprintf(err, "'%s' : path too long on line %d in file '%s'", |
| 3840 | crt_path, linenum, file); |
| 3841 | cfgerr = 1; |
| 3842 | break; |
| 3843 | } |
| 3844 | snprintf(path, sizeof(path), "%s/%s", global_ssl.crt_base, crt_path); |
| 3845 | crt_path = path; |
| 3846 | } |
| 3847 | |
| 3848 | ssl_conf = calloc(1, sizeof *ssl_conf); |
| 3849 | cur_arg = ssl_b ? ssl_b : 1; |
| 3850 | while (cur_arg < ssl_e) { |
| 3851 | newarg = 0; |
| 3852 | for (i = 0; ssl_bind_kws[i].kw != NULL; i++) { |
| 3853 | if (strcmp(ssl_bind_kws[i].kw, args[cur_arg]) == 0) { |
| 3854 | newarg = 1; |
| 3855 | cfgerr = ssl_bind_kws[i].parse(args, cur_arg, curproxy, ssl_conf, err); |
| 3856 | if (cur_arg + 1 + ssl_bind_kws[i].skip > ssl_e) { |
| 3857 | memprintf(err, "ssl args out of '[]' for %s on line %d in file '%s'", |
| 3858 | args[cur_arg], linenum, file); |
| 3859 | cfgerr = 1; |
| 3860 | } |
| 3861 | cur_arg += 1 + ssl_bind_kws[i].skip; |
| 3862 | break; |
| 3863 | } |
| 3864 | } |
| 3865 | if (!cfgerr && !newarg) { |
| 3866 | memprintf(err, "unknown ssl keyword %s on line %d in file '%s'.", |
| 3867 | args[cur_arg], linenum, file); |
| 3868 | cfgerr = 1; |
| 3869 | break; |
| 3870 | } |
| 3871 | } |
| 3872 | if (cfgerr) { |
| 3873 | ssl_sock_free_ssl_conf(ssl_conf); |
| 3874 | free(ssl_conf); |
| 3875 | ssl_conf = NULL; |
| 3876 | break; |
| 3877 | } |
| 3878 | |
| 3879 | if (stat(crt_path, &buf) == 0) { |
| 3880 | cfgerr = ssl_sock_load_cert_file(crt_path, bind_conf, ssl_conf, |
| 3881 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3882 | } else { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 3883 | cfgerr = ssl_sock_load_multi_cert(crt_path, bind_conf, ssl_conf, |
| 3884 | &args[cur_arg], arg - cur_arg - 1, err); |
yanbzhu | 1b04e5b | 2015-12-02 13:54:14 -0500 | [diff] [blame] | 3885 | } |
| 3886 | |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3887 | if (cfgerr) { |
| 3888 | 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] | 3889 | break; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 3890 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3891 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 3892 | fclose(f); |
| 3893 | return cfgerr; |
| 3894 | } |
| 3895 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3896 | /* Create an initial CTX used to start the SSL connection before switchctx */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3897 | static int |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 3898 | ssl_sock_initial_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3899 | { |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3900 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3901 | long options = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3902 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 3903 | SSL_OP_NO_SSLv2 | |
| 3904 | SSL_OP_NO_COMPRESSION | |
Emeric Brun | a4bcd9a | 2012-09-20 16:19:02 +0200 | [diff] [blame] | 3905 | SSL_OP_SINGLE_DH_USE | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 3906 | SSL_OP_SINGLE_ECDH_USE | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3907 | SSL_OP_NO_SESSION_RESUMPTION_ON_RENEGOTIATION | |
Lukas Tribus | 926594f | 2018-05-18 17:55:57 +0200 | [diff] [blame] | 3908 | SSL_OP_PRIORITIZE_CHACHA | |
Emeric Brun | 3c4bc6e | 2012-10-04 18:44:19 +0200 | [diff] [blame] | 3909 | SSL_OP_CIPHER_SERVER_PREFERENCE; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3910 | long mode = |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 3911 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 3912 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 3913 | SSL_MODE_RELEASE_BUFFERS | |
| 3914 | SSL_MODE_SMALL_BUFFERS; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 3915 | 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] | 3916 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3917 | int flags = MC_SSL_O_ALL; |
| 3918 | int cfgerr = 0; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 3919 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3920 | ctx = SSL_CTX_new(SSLv23_server_method()); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3921 | bind_conf->initial_ctx = ctx; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3922 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3923 | 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] | 3924 | ha_warning("Proxy '%s': no-sslv3/no-tlsv1x are ignored for bind '%s' at [%s:%d]. " |
| 3925 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3926 | 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] | 3927 | else |
| 3928 | flags = conf_ssl_methods->flags; |
| 3929 | |
Emmanuel Hocdet | bd695fe | 2017-05-15 15:53:41 +0200 | [diff] [blame] | 3930 | min = conf_ssl_methods->min; |
| 3931 | max = conf_ssl_methods->max; |
| 3932 | /* start with TLSv10 to remove SSLv3 per default */ |
| 3933 | if (!min && (!max || max >= CONF_TLSV10)) |
| 3934 | min = CONF_TLSV10; |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3935 | /* 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] | 3936 | if (min) |
| 3937 | flags |= (methodVersions[min].flag - 1); |
| 3938 | if (max) |
| 3939 | flags |= ~((methodVersions[max].flag << 1) - 1); |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3940 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3941 | min = max = CONF_TLSV_NONE; |
| 3942 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3943 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3944 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3945 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3946 | if (min) { |
| 3947 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3948 | ha_warning("Proxy '%s': SSL/TLS versions range not contiguous for bind '%s' at [%s:%d]. " |
| 3949 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 3950 | bind_conf->frontend->id, bind_conf->arg, bind_conf->file, bind_conf->line, |
| 3951 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3952 | hole = 0; |
| 3953 | } |
| 3954 | max = i; |
| 3955 | } |
| 3956 | else { |
| 3957 | min = max = i; |
| 3958 | } |
| 3959 | } |
| 3960 | else { |
| 3961 | if (min) |
| 3962 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3963 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3964 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 3965 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 3966 | 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] | 3967 | cfgerr += 1; |
| 3968 | } |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 3969 | /* save real min/max in bind_conf */ |
| 3970 | conf_ssl_methods->min = min; |
| 3971 | conf_ssl_methods->max = max; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3972 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 3973 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3974 | /* 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] | 3975 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3976 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 3977 | methodVersions[min].ctx_set_version(ctx, SET_SERVER); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 3978 | else |
| 3979 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 3980 | if (flags & methodVersions[i].flag) |
| 3981 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3982 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 3983 | /* 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] | 3984 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 3985 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emeric Brun | fa5c5c8 | 2017-04-28 16:19:51 +0200 | [diff] [blame] | 3986 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3987 | |
| 3988 | if (bind_conf->ssl_options & BC_SSL_O_NO_TLS_TICKETS) |
| 3989 | options |= SSL_OP_NO_TICKET; |
| 3990 | if (bind_conf->ssl_options & BC_SSL_O_PREF_CLIE_CIPH) |
| 3991 | options &= ~SSL_OP_CIPHER_SERVER_PREFERENCE; |
Dirkjan Bussink | 526894f | 2019-01-21 09:35:03 -0800 | [diff] [blame] | 3992 | |
| 3993 | #ifdef SSL_OP_NO_RENEGOTIATION |
| 3994 | options |= SSL_OP_NO_RENEGOTIATION; |
| 3995 | #endif |
| 3996 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 3997 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 3998 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 3999 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4000 | if (global_ssl.async) |
| 4001 | mode |= SSL_MODE_ASYNC; |
| 4002 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4003 | SSL_CTX_set_mode(ctx, mode); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4004 | if (global_ssl.life_time) |
| 4005 | SSL_CTX_set_timeout(ctx, global_ssl.life_time); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4006 | |
| 4007 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 4008 | #ifdef OPENSSL_IS_BORINGSSL |
| 4009 | SSL_CTX_set_select_certificate_cb(ctx, ssl_sock_switchctx_cbk); |
| 4010 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4011 | #elif (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 51088ce | 2019-01-02 18:46:41 +0100 | [diff] [blame] | 4012 | if (bind_conf->ssl_conf.early_data) { |
| 4013 | SSL_CTX_set_options(ctx, SSL_OP_NO_ANTI_REPLAY); |
| 4014 | SSL_CTX_set_max_early_data(ctx, global.tune.bufsize - global.tune.maxrewrite); |
| 4015 | } |
Emmanuel Hocdet | 84e417d | 2017-08-16 11:33:17 +0200 | [diff] [blame] | 4016 | SSL_CTX_set_client_hello_cb(ctx, ssl_sock_switchctx_cbk, NULL); |
| 4017 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_err_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4018 | #else |
| 4019 | SSL_CTX_set_tlsext_servername_callback(ctx, ssl_sock_switchctx_cbk); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4020 | #endif |
Emmanuel Hocdet | 253c62b | 2017-08-14 11:01:25 +0200 | [diff] [blame] | 4021 | SSL_CTX_set_tlsext_servername_arg(ctx, bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4022 | #endif |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4023 | return cfgerr; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4024 | } |
| 4025 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4026 | |
| 4027 | static inline void sh_ssl_sess_free_blocks(struct shared_block *first, struct shared_block *block) |
| 4028 | { |
| 4029 | if (first == block) { |
| 4030 | struct sh_ssl_sess_hdr *sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4031 | if (first->len > 0) |
| 4032 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
| 4033 | } |
| 4034 | } |
| 4035 | |
| 4036 | /* return first block from sh_ssl_sess */ |
| 4037 | static inline struct shared_block *sh_ssl_sess_first_block(struct sh_ssl_sess_hdr *sh_ssl_sess) |
| 4038 | { |
| 4039 | return (struct shared_block *)((unsigned char *)sh_ssl_sess - ((struct shared_block *)NULL)->data); |
| 4040 | |
| 4041 | } |
| 4042 | |
| 4043 | /* store a session into the cache |
| 4044 | * s_id : session id padded with zero to SSL_MAX_SSL_SESSION_ID_LENGTH |
| 4045 | * data: asn1 encoded session |
| 4046 | * data_len: asn1 encoded session length |
| 4047 | * Returns 1 id session was stored (else 0) |
| 4048 | */ |
| 4049 | static int sh_ssl_sess_store(unsigned char *s_id, unsigned char *data, int data_len) |
| 4050 | { |
| 4051 | struct shared_block *first; |
| 4052 | struct sh_ssl_sess_hdr *sh_ssl_sess, *oldsh_ssl_sess; |
| 4053 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4054 | 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] | 4055 | if (!first) { |
| 4056 | /* Could not retrieve enough free blocks to store that session */ |
| 4057 | return 0; |
| 4058 | } |
| 4059 | |
| 4060 | /* STORE the key in the first elem */ |
| 4061 | sh_ssl_sess = (struct sh_ssl_sess_hdr *)first->data; |
| 4062 | memcpy(sh_ssl_sess->key_data, s_id, SSL_MAX_SSL_SESSION_ID_LENGTH); |
| 4063 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4064 | |
| 4065 | /* it returns the already existing node |
| 4066 | or current node if none, never returns null */ |
| 4067 | oldsh_ssl_sess = sh_ssl_sess_tree_insert(sh_ssl_sess); |
| 4068 | if (oldsh_ssl_sess != sh_ssl_sess) { |
| 4069 | /* NOTE: Row couldn't be in use because we lock read & write function */ |
| 4070 | /* release the reserved row */ |
| 4071 | shctx_row_dec_hot(ssl_shctx, first); |
| 4072 | /* replace the previous session already in the tree */ |
| 4073 | sh_ssl_sess = oldsh_ssl_sess; |
| 4074 | /* ignore the previous session data, only use the header */ |
| 4075 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
| 4076 | shctx_row_inc_hot(ssl_shctx, first); |
| 4077 | first->len = sizeof(struct sh_ssl_sess_hdr); |
| 4078 | } |
| 4079 | |
Frédéric Lécaille | 0bec807 | 2018-10-22 17:55:57 +0200 | [diff] [blame] | 4080 | 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] | 4081 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4082 | return 0; |
William Lallemand | 99b90af | 2018-01-03 19:15:51 +0100 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | shctx_row_dec_hot(ssl_shctx, first); |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4086 | |
| 4087 | return 1; |
| 4088 | } |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4089 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4090 | /* SSL callback used when a new session is created while connecting to a server */ |
| 4091 | static int ssl_sess_new_srv_cb(SSL *ssl, SSL_SESSION *sess) |
| 4092 | { |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 4093 | struct connection *conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4094 | struct server *s; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4095 | |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4096 | s = __objt_server(conn->target); |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4097 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4098 | if (!(s->ssl_ctx.options & SRV_SSL_O_NO_REUSE)) { |
| 4099 | int len; |
| 4100 | unsigned char *ptr; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4101 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4102 | len = i2d_SSL_SESSION(sess, NULL); |
| 4103 | if (s->ssl_ctx.reused_sess[tid].ptr && s->ssl_ctx.reused_sess[tid].allocated_size >= len) { |
| 4104 | ptr = s->ssl_ctx.reused_sess[tid].ptr; |
| 4105 | } else { |
| 4106 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4107 | ptr = s->ssl_ctx.reused_sess[tid].ptr = malloc(len); |
| 4108 | s->ssl_ctx.reused_sess[tid].allocated_size = len; |
| 4109 | } |
| 4110 | if (s->ssl_ctx.reused_sess[tid].ptr) { |
| 4111 | s->ssl_ctx.reused_sess[tid].size = i2d_SSL_SESSION(sess, |
| 4112 | &ptr); |
| 4113 | } |
| 4114 | } else { |
| 4115 | free(s->ssl_ctx.reused_sess[tid].ptr); |
| 4116 | s->ssl_ctx.reused_sess[tid].ptr = NULL; |
| 4117 | } |
| 4118 | |
| 4119 | return 0; |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4120 | } |
| 4121 | |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4122 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4123 | /* SSL callback used on new session creation */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4124 | int sh_ssl_sess_new_cb(SSL *ssl, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4125 | { |
| 4126 | unsigned char encsess[SHSESS_MAX_DATA_LEN]; /* encoded session */ |
| 4127 | unsigned char encid[SSL_MAX_SSL_SESSION_ID_LENGTH]; /* encoded id */ |
| 4128 | unsigned char *p; |
| 4129 | int data_len; |
| 4130 | unsigned int sid_length, sid_ctx_length; |
| 4131 | const unsigned char *sid_data; |
| 4132 | const unsigned char *sid_ctx_data; |
| 4133 | |
| 4134 | /* Session id is already stored in to key and session id is known |
| 4135 | * so we dont store it to keep size. |
| 4136 | */ |
| 4137 | |
| 4138 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4139 | sid_ctx_data = SSL_SESSION_get0_id_context(sess, &sid_ctx_length); |
| 4140 | SSL_SESSION_set1_id(sess, sid_data, 0); |
| 4141 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, 0); |
| 4142 | |
| 4143 | /* check if buffer is large enough for the ASN1 encoded session */ |
| 4144 | data_len = i2d_SSL_SESSION(sess, NULL); |
| 4145 | if (data_len > SHSESS_MAX_DATA_LEN) |
| 4146 | goto err; |
| 4147 | |
| 4148 | p = encsess; |
| 4149 | |
| 4150 | /* process ASN1 session encoding before the lock */ |
| 4151 | i2d_SSL_SESSION(sess, &p); |
| 4152 | |
| 4153 | memcpy(encid, sid_data, sid_length); |
| 4154 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) |
| 4155 | memset(encid + sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH-sid_length); |
| 4156 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4157 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4158 | /* store to cache */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4159 | sh_ssl_sess_store(encid, encsess, data_len); |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4160 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4161 | err: |
| 4162 | /* reset original length values */ |
| 4163 | SSL_SESSION_set1_id(sess, sid_data, sid_length); |
| 4164 | SSL_SESSION_set1_id_context(sess, sid_ctx_data, sid_ctx_length); |
| 4165 | |
| 4166 | return 0; /* do not increment session reference count */ |
| 4167 | } |
| 4168 | |
| 4169 | /* 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] | 4170 | 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] | 4171 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4172 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4173 | unsigned char data[SHSESS_MAX_DATA_LEN], *p; |
| 4174 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4175 | SSL_SESSION *sess; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4176 | struct shared_block *first; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4177 | |
| 4178 | global.shctx_lookups++; |
| 4179 | |
| 4180 | /* allow the session to be freed automatically by openssl */ |
| 4181 | *do_copy = 0; |
| 4182 | |
| 4183 | /* tree key is zeros padded sessionid */ |
| 4184 | if (key_len < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4185 | memcpy(tmpkey, key, key_len); |
| 4186 | memset(tmpkey + key_len, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - key_len); |
| 4187 | key = tmpkey; |
| 4188 | } |
| 4189 | |
| 4190 | /* lock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4191 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4192 | |
| 4193 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4194 | sh_ssl_sess = sh_ssl_sess_tree_lookup(key); |
| 4195 | if (!sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4196 | /* no session found: unlock cache and exit */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4197 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4198 | global.shctx_misses++; |
| 4199 | return NULL; |
| 4200 | } |
| 4201 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4202 | /* sh_ssl_sess (shared_block->data) is at the end of shared_block */ |
| 4203 | first = sh_ssl_sess_first_block(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4204 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4205 | 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] | 4206 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4207 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4208 | |
| 4209 | /* decode ASN1 session */ |
| 4210 | p = data; |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4211 | 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] | 4212 | /* Reset session id and session id contenxt */ |
| 4213 | if (sess) { |
| 4214 | SSL_SESSION_set1_id(sess, key, key_len); |
| 4215 | SSL_SESSION_set1_id_context(sess, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4216 | } |
| 4217 | |
| 4218 | return sess; |
| 4219 | } |
| 4220 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4221 | |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4222 | /* 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] | 4223 | void sh_ssl_sess_remove_cb(SSL_CTX *ctx, SSL_SESSION *sess) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4224 | { |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4225 | struct sh_ssl_sess_hdr *sh_ssl_sess; |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4226 | unsigned char tmpkey[SSL_MAX_SSL_SESSION_ID_LENGTH]; |
| 4227 | unsigned int sid_length; |
| 4228 | const unsigned char *sid_data; |
| 4229 | (void)ctx; |
| 4230 | |
| 4231 | sid_data = SSL_SESSION_get_id(sess, &sid_length); |
| 4232 | /* tree key is zeros padded sessionid */ |
| 4233 | if (sid_length < SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 4234 | memcpy(tmpkey, sid_data, sid_length); |
| 4235 | memset(tmpkey+sid_length, 0, SSL_MAX_SSL_SESSION_ID_LENGTH - sid_length); |
| 4236 | sid_data = tmpkey; |
| 4237 | } |
| 4238 | |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4239 | shctx_lock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4240 | |
| 4241 | /* lookup for session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4242 | sh_ssl_sess = sh_ssl_sess_tree_lookup(sid_data); |
| 4243 | if (sh_ssl_sess) { |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4244 | /* free session */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4245 | sh_ssl_sess_tree_delete(sh_ssl_sess); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | /* unlock cache */ |
William Lallemand | a3c77cf | 2017-10-30 23:44:40 +0100 | [diff] [blame] | 4249 | shctx_unlock(ssl_shctx); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | /* Set session cache mode to server and disable openssl internal cache. |
| 4253 | * Set shared cache callbacks on an ssl context. |
| 4254 | * Shared context MUST be firstly initialized */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4255 | void ssl_set_shctx(SSL_CTX *ctx) |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4256 | { |
| 4257 | SSL_CTX_set_session_id_context(ctx, (const unsigned char *)SHCTX_APPNAME, strlen(SHCTX_APPNAME)); |
| 4258 | |
| 4259 | if (!ssl_shctx) { |
| 4260 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); |
| 4261 | return; |
| 4262 | } |
| 4263 | |
| 4264 | SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_SERVER | |
| 4265 | SSL_SESS_CACHE_NO_INTERNAL | |
| 4266 | SSL_SESS_CACHE_NO_AUTO_CLEAR); |
| 4267 | |
| 4268 | /* Set callbacks */ |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4269 | SSL_CTX_sess_set_new_cb(ctx, sh_ssl_sess_new_cb); |
| 4270 | SSL_CTX_sess_set_get_cb(ctx, sh_ssl_sess_get_cb); |
| 4271 | SSL_CTX_sess_set_remove_cb(ctx, sh_ssl_sess_remove_cb); |
William Lallemand | ed0b5ad | 2017-10-30 19:36:36 +0100 | [diff] [blame] | 4272 | } |
| 4273 | |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4274 | int ssl_sock_prepare_ctx(struct bind_conf *bind_conf, struct ssl_bind_conf *ssl_conf, SSL_CTX *ctx) |
| 4275 | { |
| 4276 | struct proxy *curproxy = bind_conf->frontend; |
| 4277 | int cfgerr = 0; |
| 4278 | int verify = SSL_VERIFY_NONE; |
Willy Tarreau | 5d4cafb | 2018-01-04 18:55:19 +0100 | [diff] [blame] | 4279 | struct ssl_bind_conf __maybe_unused *ssl_conf_cur; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4280 | const char *conf_ciphers; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4281 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4282 | const char *conf_ciphersuites; |
| 4283 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4284 | const char *conf_curves = NULL; |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4285 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 4286 | if (ssl_conf) { |
| 4287 | struct tls_version_filter *conf_ssl_methods = &ssl_conf->ssl_methods; |
| 4288 | int i, min, max; |
| 4289 | int flags = MC_SSL_O_ALL; |
| 4290 | |
| 4291 | /* 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] | 4292 | min = conf_ssl_methods->min ? conf_ssl_methods->min : bind_conf->ssl_conf.ssl_methods.min; |
| 4293 | 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] | 4294 | if (min) |
| 4295 | flags |= (methodVersions[min].flag - 1); |
| 4296 | if (max) |
| 4297 | flags |= ~((methodVersions[max].flag << 1) - 1); |
| 4298 | min = max = CONF_TLSV_NONE; |
| 4299 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4300 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
| 4301 | if (min) |
| 4302 | max = i; |
| 4303 | else |
| 4304 | min = max = i; |
| 4305 | } |
| 4306 | /* save real min/max */ |
| 4307 | conf_ssl_methods->min = min; |
| 4308 | conf_ssl_methods->max = max; |
| 4309 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4310 | ha_alert("Proxy '%s': all SSL/TLS versions are disabled for bind '%s' at [%s:%d].\n", |
| 4311 | 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] | 4312 | cfgerr += 1; |
| 4313 | } |
| 4314 | } |
| 4315 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4316 | 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] | 4317 | case SSL_SOCK_VERIFY_NONE: |
| 4318 | verify = SSL_VERIFY_NONE; |
| 4319 | break; |
| 4320 | case SSL_SOCK_VERIFY_OPTIONAL: |
| 4321 | verify = SSL_VERIFY_PEER; |
| 4322 | break; |
| 4323 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4324 | verify = SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT; |
| 4325 | break; |
| 4326 | } |
| 4327 | SSL_CTX_set_verify(ctx, verify, ssl_sock_bind_verifycbk); |
| 4328 | if (verify & SSL_VERIFY_PEER) { |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4329 | char *ca_file = (ssl_conf && ssl_conf->ca_file) ? ssl_conf->ca_file : bind_conf->ssl_conf.ca_file; |
| 4330 | char *crl_file = (ssl_conf && ssl_conf->crl_file) ? ssl_conf->crl_file : bind_conf->ssl_conf.crl_file; |
| 4331 | if (ca_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4332 | /* load CAfile to verify */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4333 | if (!SSL_CTX_load_verify_locations(ctx, ca_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4334 | ha_alert("Proxy '%s': unable to load CA file '%s' for bind '%s' at [%s:%d].\n", |
| 4335 | 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] | 4336 | cfgerr++; |
| 4337 | } |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 4338 | if (!((ssl_conf && ssl_conf->no_ca_names) || bind_conf->ssl_conf.no_ca_names)) { |
| 4339 | /* set CA names for client cert request, function returns void */ |
| 4340 | SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(ca_file)); |
| 4341 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4342 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4343 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4344 | ha_alert("Proxy '%s': verify is enabled but no CA file specified for bind '%s' at [%s:%d].\n", |
| 4345 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4346 | cfgerr++; |
| 4347 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4348 | #ifdef X509_V_FLAG_CRL_CHECK |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4349 | if (crl_file) { |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4350 | X509_STORE *store = SSL_CTX_get_cert_store(ctx); |
| 4351 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4352 | if (!store || !X509_STORE_load_locations(store, crl_file, NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4353 | ha_alert("Proxy '%s': unable to configure CRL file '%s' for bind '%s' at [%s:%d].\n", |
| 4354 | 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] | 4355 | cfgerr++; |
| 4356 | } |
Emeric Brun | 561e574 | 2012-10-02 15:20:55 +0200 | [diff] [blame] | 4357 | else { |
| 4358 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4359 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4360 | } |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 4361 | #endif |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 4362 | ERR_clear_error(); |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 4363 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4364 | #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] | 4365 | if(bind_conf->keys_ref) { |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4366 | 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] | 4367 | ha_alert("Proxy '%s': unable to set callback for TLS ticket validation for bind '%s' at [%s:%d].\n", |
| 4368 | curproxy->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 4369 | cfgerr++; |
| 4370 | } |
| 4371 | } |
| 4372 | #endif |
| 4373 | |
William Lallemand | 4f45bb9 | 2017-10-30 20:08:51 +0100 | [diff] [blame] | 4374 | ssl_set_shctx(ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4375 | conf_ciphers = (ssl_conf && ssl_conf->ciphers) ? ssl_conf->ciphers : bind_conf->ssl_conf.ciphers; |
| 4376 | if (conf_ciphers && |
| 4377 | !SSL_CTX_set_cipher_list(ctx, conf_ciphers)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4378 | ha_alert("Proxy '%s': unable to set SSL cipher list to '%s' for bind '%s' at [%s:%d].\n", |
| 4379 | 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] | 4380 | cfgerr++; |
| 4381 | } |
| 4382 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4383 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4384 | conf_ciphersuites = (ssl_conf && ssl_conf->ciphersuites) ? ssl_conf->ciphersuites : bind_conf->ssl_conf.ciphersuites; |
| 4385 | if (conf_ciphersuites && |
| 4386 | !SSL_CTX_set_ciphersuites(ctx, conf_ciphersuites)) { |
| 4387 | ha_alert("Proxy '%s': unable to set TLS 1.3 cipher suites to '%s' for bind '%s' at [%s:%d].\n", |
| 4388 | curproxy->id, conf_ciphersuites, bind_conf->arg, bind_conf->file, bind_conf->line); |
| 4389 | cfgerr++; |
| 4390 | } |
| 4391 | #endif |
| 4392 | |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4393 | #ifndef OPENSSL_NO_DH |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4394 | /* If tune.ssl.default-dh-param has not been set, |
| 4395 | neither has ssl-default-dh-file and no static DH |
| 4396 | params were in the certificate file. */ |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4397 | if (global_ssl.default_dh_param == 0 && |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 4398 | global_dh == NULL && |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 4399 | (ssl_dh_ptr_index == -1 || |
| 4400 | SSL_CTX_get_ex_data(ctx, ssl_dh_ptr_index) == NULL)) { |
Emmanuel Hocdet | cc6c2a2 | 2017-03-03 17:04:14 +0100 | [diff] [blame] | 4401 | STACK_OF(SSL_CIPHER) * ciphers = NULL; |
| 4402 | const SSL_CIPHER * cipher = NULL; |
| 4403 | char cipher_description[128]; |
| 4404 | /* The description of ciphers using an Ephemeral Diffie Hellman key exchange |
| 4405 | contains " Kx=DH " or " Kx=DH(". Beware of " Kx=DH/", |
| 4406 | which is not ephemeral DH. */ |
| 4407 | const char dhe_description[] = " Kx=DH "; |
| 4408 | const char dhe_export_description[] = " Kx=DH("; |
| 4409 | int idx = 0; |
| 4410 | int dhe_found = 0; |
| 4411 | SSL *ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4412 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4413 | ssl = SSL_new(ctx); |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4414 | |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4415 | if (ssl) { |
| 4416 | ciphers = SSL_get_ciphers(ssl); |
| 4417 | |
| 4418 | if (ciphers) { |
| 4419 | for (idx = 0; idx < sk_SSL_CIPHER_num(ciphers); idx++) { |
| 4420 | cipher = sk_SSL_CIPHER_value(ciphers, idx); |
| 4421 | if (SSL_CIPHER_description(cipher, cipher_description, sizeof (cipher_description)) == cipher_description) { |
| 4422 | if (strstr(cipher_description, dhe_description) != NULL || |
| 4423 | strstr(cipher_description, dhe_export_description) != NULL) { |
| 4424 | dhe_found = 1; |
| 4425 | break; |
| 4426 | } |
Remi Gacogne | c1eab8c | 2014-06-12 18:20:11 +0200 | [diff] [blame] | 4427 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4428 | } |
| 4429 | } |
Remi Gacogne | 23d5d37 | 2014-10-10 17:04:26 +0200 | [diff] [blame] | 4430 | SSL_free(ssl); |
| 4431 | ssl = NULL; |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4432 | } |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4433 | |
Lukas Tribus | 9013272 | 2014-08-18 00:56:33 +0200 | [diff] [blame] | 4434 | if (dhe_found) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4435 | 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] | 4436 | } |
| 4437 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4438 | global_ssl.default_dh_param = 1024; |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4439 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4440 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4441 | if (global_ssl.default_dh_param >= 1024) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4442 | if (local_dh_1024 == NULL) { |
| 4443 | local_dh_1024 = ssl_get_dh_1024(); |
| 4444 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4445 | if (global_ssl.default_dh_param >= 2048) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4446 | if (local_dh_2048 == NULL) { |
| 4447 | local_dh_2048 = ssl_get_dh_2048(); |
| 4448 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 4449 | if (global_ssl.default_dh_param >= 4096) { |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4450 | if (local_dh_4096 == NULL) { |
| 4451 | local_dh_4096 = ssl_get_dh_4096(); |
| 4452 | } |
Remi Gacogne | 8de5415 | 2014-07-15 11:36:40 +0200 | [diff] [blame] | 4453 | } |
| 4454 | } |
| 4455 | } |
| 4456 | #endif /* OPENSSL_NO_DH */ |
Remi Gacogne | f46cd6e | 2014-06-12 14:58:40 +0200 | [diff] [blame] | 4457 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4458 | SSL_CTX_set_info_callback(ctx, ssl_sock_infocbk); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4459 | #if HA_OPENSSL_VERSION_NUMBER >= 0x00907000L |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4460 | SSL_CTX_set_msg_callback(ctx, ssl_sock_msgcbk); |
Willy Tarreau | 5cbe4ef | 2014-05-08 22:45:11 +0200 | [diff] [blame] | 4461 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 4462 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 4463 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4464 | ssl_conf_cur = NULL; |
| 4465 | if (ssl_conf && ssl_conf->npn_str) |
| 4466 | ssl_conf_cur = ssl_conf; |
| 4467 | else if (bind_conf->ssl_conf.npn_str) |
| 4468 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4469 | if (ssl_conf_cur) |
| 4470 | 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] | 4471 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 4472 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4473 | ssl_conf_cur = NULL; |
| 4474 | if (ssl_conf && ssl_conf->alpn_str) |
| 4475 | ssl_conf_cur = ssl_conf; |
| 4476 | else if (bind_conf->ssl_conf.alpn_str) |
| 4477 | ssl_conf_cur = &bind_conf->ssl_conf; |
| 4478 | if (ssl_conf_cur) |
| 4479 | 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] | 4480 | #endif |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4481 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4482 | conf_curves = (ssl_conf && ssl_conf->curves) ? ssl_conf->curves : bind_conf->ssl_conf.curves; |
| 4483 | if (conf_curves) { |
| 4484 | if (!SSL_CTX_set1_curves_list(ctx, conf_curves)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4485 | ha_alert("Proxy '%s': unable to set SSL curves list to '%s' for bind '%s' at [%s:%d].\n", |
| 4486 | 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] | 4487 | cfgerr++; |
| 4488 | } |
Emmanuel Hocdet | a52bb15 | 2017-03-20 11:11:49 +0100 | [diff] [blame] | 4489 | #if defined(SSL_CTX_set_ecdh_auto) |
| 4490 | (void)SSL_CTX_set_ecdh_auto(ctx, 1); |
| 4491 | #endif |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4492 | } |
| 4493 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4494 | #if defined(SSL_CTX_set_tmp_ecdh) && !defined(OPENSSL_NO_ECDH) |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 4495 | if (!conf_curves) { |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4496 | int i; |
| 4497 | EC_KEY *ecdh; |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4498 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4499 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 4500 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4501 | NULL); |
| 4502 | |
| 4503 | if (ecdhe == NULL) { |
| 4504 | SSL_CTX_set_dh_auto(ctx, 1); |
| 4505 | return cfgerr; |
| 4506 | } |
| 4507 | #else |
| 4508 | const char *ecdhe = (ssl_conf && ssl_conf->ecdhe) ? ssl_conf->ecdhe : |
| 4509 | (bind_conf->ssl_conf.ecdhe ? bind_conf->ssl_conf.ecdhe : |
| 4510 | ECDHE_DEFAULT_CURVE); |
| 4511 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4512 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4513 | i = OBJ_sn2nid(ecdhe); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4514 | if (!i || ((ecdh = EC_KEY_new_by_curve_name(i)) == NULL)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4515 | ha_alert("Proxy '%s': unable to set elliptic named curve to '%s' for bind '%s' at [%s:%d].\n", |
| 4516 | curproxy->id, ecdhe, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 4517 | cfgerr++; |
| 4518 | } |
| 4519 | else { |
| 4520 | SSL_CTX_set_tmp_ecdh(ctx, ecdh); |
| 4521 | EC_KEY_free(ecdh); |
| 4522 | } |
| 4523 | } |
| 4524 | #endif |
| 4525 | |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4526 | return cfgerr; |
| 4527 | } |
| 4528 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4529 | static int ssl_sock_srv_hostcheck(const char *pattern, const char *hostname) |
| 4530 | { |
| 4531 | const char *pattern_wildcard, *pattern_left_label_end, *hostname_left_label_end; |
| 4532 | size_t prefixlen, suffixlen; |
| 4533 | |
| 4534 | /* Trivial case */ |
| 4535 | if (strcmp(pattern, hostname) == 0) |
| 4536 | return 1; |
| 4537 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4538 | /* The rest of this logic is based on RFC 6125, section 6.4.3 |
| 4539 | * (http://tools.ietf.org/html/rfc6125#section-6.4.3) */ |
| 4540 | |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4541 | pattern_wildcard = NULL; |
| 4542 | pattern_left_label_end = pattern; |
| 4543 | while (*pattern_left_label_end != '.') { |
| 4544 | switch (*pattern_left_label_end) { |
| 4545 | case 0: |
| 4546 | /* End of label not found */ |
| 4547 | return 0; |
| 4548 | case '*': |
| 4549 | /* If there is more than one wildcards */ |
| 4550 | if (pattern_wildcard) |
| 4551 | return 0; |
| 4552 | pattern_wildcard = pattern_left_label_end; |
| 4553 | break; |
| 4554 | } |
| 4555 | pattern_left_label_end++; |
| 4556 | } |
| 4557 | |
| 4558 | /* If it's not trivial and there is no wildcard, it can't |
| 4559 | * match */ |
| 4560 | if (!pattern_wildcard) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4561 | return 0; |
| 4562 | |
| 4563 | /* Make sure all labels match except the leftmost */ |
| 4564 | hostname_left_label_end = strchr(hostname, '.'); |
| 4565 | if (!hostname_left_label_end |
| 4566 | || strcmp(pattern_left_label_end, hostname_left_label_end) != 0) |
| 4567 | return 0; |
| 4568 | |
| 4569 | /* Make sure the leftmost label of the hostname is long enough |
| 4570 | * that the wildcard can match */ |
Emeric Brun | 369da85 | 2013-10-08 11:39:35 +0200 | [diff] [blame] | 4571 | if (hostname_left_label_end - hostname < (pattern_left_label_end - pattern) - 1) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4572 | return 0; |
| 4573 | |
| 4574 | /* Finally compare the string on either side of the |
| 4575 | * wildcard */ |
| 4576 | prefixlen = pattern_wildcard - pattern; |
| 4577 | suffixlen = pattern_left_label_end - (pattern_wildcard + 1); |
Emeric Brun | a848dae | 2013-10-08 11:27:28 +0200 | [diff] [blame] | 4578 | if ((prefixlen && (memcmp(pattern, hostname, prefixlen) != 0)) |
| 4579 | || (suffixlen && (memcmp(pattern_wildcard + 1, hostname_left_label_end - suffixlen, suffixlen) != 0))) |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4580 | return 0; |
| 4581 | |
| 4582 | return 1; |
| 4583 | } |
| 4584 | |
| 4585 | static int ssl_sock_srv_verifycbk(int ok, X509_STORE_CTX *ctx) |
| 4586 | { |
| 4587 | SSL *ssl; |
| 4588 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4589 | struct ssl_sock_ctx *ssl_ctx; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4590 | const char *servername; |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4591 | const char *sni; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4592 | |
| 4593 | int depth; |
| 4594 | X509 *cert; |
| 4595 | STACK_OF(GENERAL_NAME) *alt_names; |
| 4596 | int i; |
| 4597 | X509_NAME *cert_subject; |
| 4598 | char *str; |
| 4599 | |
| 4600 | if (ok == 0) |
| 4601 | return ok; |
| 4602 | |
| 4603 | 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] | 4604 | conn = SSL_get_ex_data(ssl, ssl_app_data_index); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4605 | ssl_ctx = conn->xprt_ctx; |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4606 | |
Willy Tarreau | ad92a9a | 2017-07-28 11:38:41 +0200 | [diff] [blame] | 4607 | /* We're checking if the provided hostnames match the desired one. The |
| 4608 | * desired hostname comes from the SNI we presented if any, or if not |
| 4609 | * provided then it may have been explicitly stated using a "verifyhost" |
| 4610 | * directive. If neither is set, we don't care about the name so the |
| 4611 | * verification is OK. |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4612 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 4613 | servername = SSL_get_servername(ssl_ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4614 | sni = servername; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4615 | if (!servername) { |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 4616 | servername = __objt_server(conn->target)->ssl_ctx.verify_host; |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4617 | if (!servername) |
| 4618 | return ok; |
| 4619 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4620 | |
| 4621 | /* We only need to verify the CN on the actual server cert, |
| 4622 | * not the indirect CAs */ |
| 4623 | depth = X509_STORE_CTX_get_error_depth(ctx); |
| 4624 | if (depth != 0) |
| 4625 | return ok; |
| 4626 | |
| 4627 | /* At this point, the cert is *not* OK unless we can find a |
| 4628 | * hostname match */ |
| 4629 | ok = 0; |
| 4630 | |
| 4631 | cert = X509_STORE_CTX_get_current_cert(ctx); |
| 4632 | /* It seems like this might happen if verify peer isn't set */ |
| 4633 | if (!cert) |
| 4634 | return ok; |
| 4635 | |
| 4636 | alt_names = X509_get_ext_d2i(cert, NID_subject_alt_name, NULL, NULL); |
| 4637 | if (alt_names) { |
| 4638 | for (i = 0; !ok && i < sk_GENERAL_NAME_num(alt_names); i++) { |
| 4639 | GENERAL_NAME *name = sk_GENERAL_NAME_value(alt_names, i); |
| 4640 | if (name->type == GEN_DNS) { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4641 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4642 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.ia5) >= 0) { |
| 4643 | #else |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4644 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, name->d.dNSName) >= 0) { |
Emeric Brun | a33410c | 2013-09-17 15:47:48 +0200 | [diff] [blame] | 4645 | #endif |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4646 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4647 | OPENSSL_free(str); |
| 4648 | } |
| 4649 | } |
| 4650 | } |
Emeric Brun | 4ad50a4 | 2013-09-17 15:19:54 +0200 | [diff] [blame] | 4651 | sk_GENERAL_NAME_pop_free(alt_names, GENERAL_NAME_free); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4652 | } |
| 4653 | |
| 4654 | cert_subject = X509_get_subject_name(cert); |
| 4655 | i = -1; |
| 4656 | while (!ok && (i = X509_NAME_get_index_by_NID(cert_subject, NID_commonName, i)) != -1) { |
| 4657 | X509_NAME_ENTRY *entry = X509_NAME_get_entry(cert_subject, i); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 4658 | ASN1_STRING *value; |
| 4659 | value = X509_NAME_ENTRY_get_data(entry); |
| 4660 | if (ASN1_STRING_to_UTF8((unsigned char **)&str, value) >= 0) { |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4661 | ok = ssl_sock_srv_hostcheck(str, servername); |
| 4662 | OPENSSL_free(str); |
| 4663 | } |
| 4664 | } |
| 4665 | |
Willy Tarreau | 71d058c | 2017-07-26 20:09:56 +0200 | [diff] [blame] | 4666 | /* report the mismatch and indicate if SNI was used or not */ |
| 4667 | if (!ok && !conn->err_code) |
| 4668 | 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] | 4669 | return ok; |
| 4670 | } |
| 4671 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4672 | /* prepare ssl context from servers options. Returns an error count */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4673 | int ssl_sock_prepare_srv_ctx(struct server *srv) |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4674 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4675 | struct proxy *curproxy = srv->proxy; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4676 | int cfgerr = 0; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4677 | long options = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4678 | SSL_OP_ALL | /* all known workarounds for bugs */ |
| 4679 | SSL_OP_NO_SSLv2 | |
| 4680 | SSL_OP_NO_COMPRESSION; |
Remi Gacogne | af5c3da | 2014-05-19 10:29:58 +0200 | [diff] [blame] | 4681 | long mode = |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4682 | SSL_MODE_ENABLE_PARTIAL_WRITE | |
| 4683 | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | |
Willy Tarreau | 396a186 | 2014-11-13 14:06:52 +0100 | [diff] [blame] | 4684 | SSL_MODE_RELEASE_BUFFERS | |
| 4685 | SSL_MODE_SMALL_BUFFERS; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4686 | int verify = SSL_VERIFY_NONE; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4687 | SSL_CTX *ctx = NULL; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4688 | struct tls_version_filter *conf_ssl_methods = &srv->ssl_ctx.methods; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4689 | int i, min, max, hole; |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4690 | int flags = MC_SSL_O_ALL; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4691 | |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4692 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4693 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4694 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Thierry Fournier | 383085f | 2013-01-24 14:15:43 +0100 | [diff] [blame] | 4695 | cfgerr++; |
| 4696 | } |
| 4697 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4698 | /* Automatic memory computations need to know we use SSL there */ |
| 4699 | global.ssl_used_backend = 1; |
| 4700 | |
| 4701 | /* Initiate SSL context for current server */ |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4702 | if (!srv->ssl_ctx.reused_sess) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 4703 | 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] | 4704 | ha_alert("Proxy '%s', server '%s' [%s:%d] out of memory.\n", |
| 4705 | curproxy->id, srv->id, |
| 4706 | srv->conf.file, srv->conf.line); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 4707 | cfgerr++; |
| 4708 | return cfgerr; |
| 4709 | } |
| 4710 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4711 | if (srv->use_ssl) |
| 4712 | srv->xprt = &ssl_sock; |
| 4713 | if (srv->check.use_ssl) |
Cyril Bonté | 9ce1311 | 2014-11-15 22:41:27 +0100 | [diff] [blame] | 4714 | srv->check.xprt = &ssl_sock; |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4715 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4716 | ctx = SSL_CTX_new(SSLv23_client_method()); |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4717 | if (!ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4718 | ha_alert("config : %s '%s', server '%s': unable to allocate ssl context.\n", |
| 4719 | proxy_type_str(curproxy), curproxy->id, |
| 4720 | srv->id); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4721 | cfgerr++; |
| 4722 | return cfgerr; |
| 4723 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4724 | |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4725 | 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] | 4726 | ha_warning("config : %s '%s': no-sslv3/no-tlsv1x are ignored for server '%s'. " |
| 4727 | "Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4728 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4729 | else |
| 4730 | flags = conf_ssl_methods->flags; |
| 4731 | |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4732 | /* Real min and max should be determinate with configuration and openssl's capabilities */ |
| 4733 | if (conf_ssl_methods->min) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4734 | flags |= (methodVersions[conf_ssl_methods->min].flag - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4735 | if (conf_ssl_methods->max) |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4736 | flags |= ~((methodVersions[conf_ssl_methods->max].flag << 1) - 1); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4737 | |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4738 | /* find min, max and holes */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4739 | min = max = CONF_TLSV_NONE; |
| 4740 | hole = 0; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4741 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4742 | /* version is in openssl && version not disable in configuration */ |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4743 | if (methodVersions[i].option && !(flags & methodVersions[i].flag)) { |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4744 | if (min) { |
| 4745 | if (hole) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4746 | ha_warning("config : %s '%s': SSL/TLS versions range not contiguous for server '%s'. " |
| 4747 | "Hole find for %s. Use only 'ssl-min-ver' and 'ssl-max-ver' to fix.\n", |
| 4748 | proxy_type_str(curproxy), curproxy->id, srv->id, |
| 4749 | methodVersions[hole].name); |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4750 | hole = 0; |
| 4751 | } |
| 4752 | max = i; |
| 4753 | } |
| 4754 | else { |
| 4755 | min = max = i; |
| 4756 | } |
| 4757 | } |
| 4758 | else { |
| 4759 | if (min) |
| 4760 | hole = i; |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4761 | } |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4762 | if (!min) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4763 | ha_alert("config : %s '%s': all SSL/TLS versions are disabled for server '%s'.\n", |
| 4764 | proxy_type_str(curproxy), curproxy->id, srv->id); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4765 | cfgerr += 1; |
| 4766 | } |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4767 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 4768 | #if (HA_OPENSSL_VERSION_NUMBER < 0x1010000fL) |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4769 | /* 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] | 4770 | precautionary measure to avoid any surprise with older openssl version. */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4771 | if (min == max) |
Emmanuel Hocdet | 4aa615f | 2017-05-18 12:33:19 +0200 | [diff] [blame] | 4772 | methodVersions[min].ctx_set_version(ctx, SET_CLIENT); |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4773 | else |
| 4774 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 4775 | if (flags & methodVersions[i].flag) |
| 4776 | options |= methodVersions[i].option; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4777 | #else /* openssl >= 1.1.0 */ |
Emmanuel Hocdet | b4e9ba4 | 2017-03-30 19:25:07 +0200 | [diff] [blame] | 4778 | /* 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] | 4779 | methodVersions[min].ctx_set_version(ctx, SET_MIN); |
| 4780 | methodVersions[max].ctx_set_version(ctx, SET_MAX); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 4781 | #endif |
| 4782 | |
| 4783 | if (srv->ssl_ctx.options & SRV_SSL_O_NO_TLS_TICKETS) |
| 4784 | options |= SSL_OP_NO_TICKET; |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4785 | SSL_CTX_set_options(ctx, options); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4786 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4787 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 4788 | if (global_ssl.async) |
| 4789 | mode |= SSL_MODE_ASYNC; |
| 4790 | #endif |
Emmanuel Hocdet | 4de1ff1 | 2017-03-03 12:21:32 +0100 | [diff] [blame] | 4791 | SSL_CTX_set_mode(ctx, mode); |
| 4792 | srv->ssl_ctx.ctx = ctx; |
| 4793 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4794 | if (srv->ssl_ctx.client_crt) { |
| 4795 | 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] | 4796 | ha_alert("config : %s '%s', server '%s': unable to load SSL private key from PEM file '%s'.\n", |
| 4797 | proxy_type_str(curproxy), curproxy->id, |
| 4798 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4799 | cfgerr++; |
| 4800 | } |
| 4801 | 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] | 4802 | ha_alert("config : %s '%s', server '%s': unable to load ssl certificate from PEM file '%s'.\n", |
| 4803 | proxy_type_str(curproxy), curproxy->id, |
| 4804 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4805 | cfgerr++; |
| 4806 | } |
| 4807 | else if (SSL_CTX_check_private_key(srv->ssl_ctx.ctx) <= 0) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4808 | ha_alert("config : %s '%s', server '%s': inconsistencies between private key and certificate loaded from PEM file '%s'.\n", |
| 4809 | proxy_type_str(curproxy), curproxy->id, |
| 4810 | srv->id, srv->ssl_ctx.client_crt); |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 4811 | cfgerr++; |
| 4812 | } |
| 4813 | } |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4814 | |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4815 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
| 4816 | verify = SSL_VERIFY_PEER; |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4817 | switch (srv->ssl_ctx.verify) { |
| 4818 | case SSL_SOCK_VERIFY_NONE: |
| 4819 | verify = SSL_VERIFY_NONE; |
| 4820 | break; |
| 4821 | case SSL_SOCK_VERIFY_REQUIRED: |
| 4822 | verify = SSL_VERIFY_PEER; |
| 4823 | break; |
| 4824 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 4825 | SSL_CTX_set_verify(srv->ssl_ctx.ctx, |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4826 | verify, |
Willy Tarreau | 2ab8867 | 2017-07-05 18:23:03 +0200 | [diff] [blame] | 4827 | (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] | 4828 | if (verify & SSL_VERIFY_PEER) { |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4829 | if (srv->ssl_ctx.ca_file) { |
| 4830 | /* load CAfile to verify */ |
| 4831 | 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] | 4832 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to load CA file '%s'.\n", |
| 4833 | curproxy->id, srv->id, |
| 4834 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ca_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4835 | cfgerr++; |
| 4836 | } |
| 4837 | } |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4838 | else { |
| 4839 | if (global.ssl_server_verify == SSL_SERVER_VERIFY_REQUIRED) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4840 | 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", |
| 4841 | curproxy->id, srv->id, |
| 4842 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4843 | else |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4844 | ha_alert("Proxy '%s', server '%s' [%s:%d] verify is enabled but no CA file specified.\n", |
| 4845 | curproxy->id, srv->id, |
| 4846 | srv->conf.file, srv->conf.line); |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 4847 | cfgerr++; |
| 4848 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4849 | #ifdef X509_V_FLAG_CRL_CHECK |
| 4850 | if (srv->ssl_ctx.crl_file) { |
| 4851 | X509_STORE *store = SSL_CTX_get_cert_store(srv->ssl_ctx.ctx); |
| 4852 | |
| 4853 | 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] | 4854 | ha_alert("Proxy '%s', server '%s' [%s:%d] unable to configure CRL file '%s'.\n", |
| 4855 | curproxy->id, srv->id, |
| 4856 | srv->conf.file, srv->conf.line, srv->ssl_ctx.crl_file); |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 4857 | cfgerr++; |
| 4858 | } |
| 4859 | else { |
| 4860 | X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK|X509_V_FLAG_CRL_CHECK_ALL); |
| 4861 | } |
| 4862 | } |
| 4863 | #endif |
| 4864 | } |
| 4865 | |
Olivier Houchard | bd84ac8 | 2017-11-03 13:43:35 +0100 | [diff] [blame] | 4866 | SSL_CTX_set_session_cache_mode(srv->ssl_ctx.ctx, SSL_SESS_CACHE_CLIENT | |
| 4867 | SSL_SESS_CACHE_NO_INTERNAL_STORE); |
| 4868 | 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] | 4869 | if (srv->ssl_ctx.ciphers && |
| 4870 | !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] | 4871 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set SSL cipher list to '%s'.\n", |
| 4872 | curproxy->id, srv->id, |
| 4873 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphers); |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4874 | cfgerr++; |
| 4875 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4876 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 4877 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4878 | if (srv->ssl_ctx.ciphersuites && |
Pierre Cheynier | bc34cd1 | 2019-03-21 16:15:47 +0000 | [diff] [blame] | 4879 | !SSL_CTX_set_ciphersuites(srv->ssl_ctx.ctx, srv->ssl_ctx.ciphersuites)) { |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 4880 | ha_alert("Proxy '%s', server '%s' [%s:%d] : unable to set TLS 1.3 cipher suites to '%s'.\n", |
| 4881 | curproxy->id, srv->id, |
| 4882 | srv->conf.file, srv->conf.line, srv->ssl_ctx.ciphersuites); |
| 4883 | cfgerr++; |
| 4884 | } |
| 4885 | #endif |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 4886 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 4887 | if (srv->ssl_ctx.npn_str) |
| 4888 | SSL_CTX_set_next_proto_select_cb(ctx, ssl_sock_srv_select_protos, srv); |
| 4889 | #endif |
| 4890 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 4891 | if (srv->ssl_ctx.alpn_str) |
| 4892 | SSL_CTX_set_alpn_protos(ctx, (unsigned char *)srv->ssl_ctx.alpn_str, srv->ssl_ctx.alpn_len); |
| 4893 | #endif |
| 4894 | |
Emeric Brun | 94324a4 | 2012-10-11 14:00:19 +0200 | [diff] [blame] | 4895 | |
| 4896 | return cfgerr; |
| 4897 | } |
| 4898 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4899 | /* 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] | 4900 | * be NULL, in which case nothing is done. Returns the number of errors |
| 4901 | * encountered. |
| 4902 | */ |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 4903 | int ssl_sock_prepare_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4904 | { |
| 4905 | struct ebmb_node *node; |
| 4906 | struct sni_ctx *sni; |
| 4907 | int err = 0; |
| 4908 | |
Willy Tarreau | fce0311 | 2015-01-15 21:32:40 +0100 | [diff] [blame] | 4909 | /* Automatic memory computations need to know we use SSL there */ |
| 4910 | global.ssl_used_frontend = 1; |
| 4911 | |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4912 | /* Make sure openssl opens /dev/urandom before the chroot */ |
| 4913 | if (!ssl_initialize_random()) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4914 | ha_alert("OpenSSL random data generator initialization failed.\n"); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4915 | err++; |
| 4916 | } |
| 4917 | /* Create initial_ctx used to start the ssl connection before do switchctx */ |
| 4918 | if (!bind_conf->initial_ctx) { |
Emmanuel Hocdet | abd3233 | 2017-05-05 18:06:12 +0200 | [diff] [blame] | 4919 | err += ssl_sock_initial_ctx(bind_conf); |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 4920 | /* It should not be necessary to call this function, but it's |
| 4921 | necessary first to check and move all initialisation related |
| 4922 | to initial_ctx in ssl_sock_initial_ctx. */ |
| 4923 | err += ssl_sock_prepare_ctx(bind_conf, NULL, bind_conf->initial_ctx); |
| 4924 | } |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4925 | if (bind_conf->default_ctx) |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4926 | 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] | 4927 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4928 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4929 | while (node) { |
| 4930 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4931 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4932 | /* only initialize the CTX on its first occurrence and |
| 4933 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4934 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4935 | node = ebmb_next(node); |
| 4936 | } |
| 4937 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 4938 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4939 | while (node) { |
| 4940 | sni = ebmb_entry(node, struct sni_ctx, name); |
Emeric Brun | 0bed994 | 2014-10-30 19:25:24 +0100 | [diff] [blame] | 4941 | if (!sni->order && sni->ctx != bind_conf->default_ctx) |
| 4942 | /* only initialize the CTX on its first occurrence and |
| 4943 | if it is not the default_ctx */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 4944 | err += ssl_sock_prepare_ctx(bind_conf, sni->conf, sni->ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 4945 | node = ebmb_next(node); |
| 4946 | } |
| 4947 | return err; |
| 4948 | } |
| 4949 | |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4950 | /* Prepares all the contexts for a bind_conf and allocates the shared SSL |
| 4951 | * context if needed. Returns < 0 on error, 0 on success. The warnings and |
| 4952 | * alerts are directly emitted since the rest of the stack does it below. |
| 4953 | */ |
| 4954 | int ssl_sock_prepare_bind_conf(struct bind_conf *bind_conf) |
| 4955 | { |
| 4956 | struct proxy *px = bind_conf->frontend; |
| 4957 | int alloc_ctx; |
| 4958 | int err; |
| 4959 | |
| 4960 | if (!bind_conf->is_ssl) { |
| 4961 | if (bind_conf->default_ctx) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4962 | ha_warning("Proxy '%s': A certificate was specified but SSL was not enabled on bind '%s' at [%s:%d] (use 'ssl').\n", |
| 4963 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4964 | } |
| 4965 | return 0; |
| 4966 | } |
| 4967 | if (!bind_conf->default_ctx) { |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4968 | if (bind_conf->strict_sni && !bind_conf->generate_certs) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4969 | ha_warning("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d], ssl connections will fail (use 'crt').\n", |
| 4970 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4971 | } |
| 4972 | else { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 4973 | ha_alert("Proxy '%s': no SSL certificate specified for bind '%s' at [%s:%d] (use 'crt').\n", |
| 4974 | px->id, bind_conf->arg, bind_conf->file, bind_conf->line); |
Emmanuel Hocdet | aa0d637 | 2017-08-09 11:24:25 +0200 | [diff] [blame] | 4975 | return -1; |
| 4976 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4977 | } |
William Lallemand | c61c0b3 | 2017-12-04 18:46:39 +0100 | [diff] [blame] | 4978 | if (!ssl_shctx && global.tune.sslcachesize) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4979 | alloc_ctx = shctx_init(&ssl_shctx, global.tune.sslcachesize, |
Frédéric Lécaille | b7838af | 2018-10-22 16:21:39 +0200 | [diff] [blame] | 4980 | sizeof(struct sh_ssl_sess_hdr) + SHSESS_BLOCK_MIN_SIZE, -1, |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4981 | sizeof(*sh_ssl_sess_tree), |
| 4982 | ((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] | 4983 | if (alloc_ctx <= 0) { |
William Lallemand | c3cd35f | 2017-11-28 11:04:43 +0100 | [diff] [blame] | 4984 | if (alloc_ctx == SHCTX_E_INIT_LOCK) |
| 4985 | 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"); |
| 4986 | else |
| 4987 | ha_alert("Unable to allocate SSL session cache.\n"); |
| 4988 | return -1; |
| 4989 | } |
| 4990 | /* free block callback */ |
| 4991 | ssl_shctx->free_block = sh_ssl_sess_free_blocks; |
| 4992 | /* init the root tree within the extra space */ |
| 4993 | sh_ssl_sess_tree = (void *)ssl_shctx + sizeof(struct shared_context); |
| 4994 | *sh_ssl_sess_tree = EB_ROOT_UNIQUE; |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4995 | } |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 4996 | err = 0; |
| 4997 | /* initialize all certificate contexts */ |
| 4998 | err += ssl_sock_prepare_all_ctx(bind_conf); |
| 4999 | |
| 5000 | /* initialize CA variables if the certificates generation is enabled */ |
| 5001 | err += ssl_sock_load_ca(bind_conf); |
| 5002 | |
| 5003 | return -err; |
| 5004 | } |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5005 | |
| 5006 | /* release ssl context allocated for servers. */ |
| 5007 | void ssl_sock_free_srv_ctx(struct server *srv) |
| 5008 | { |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5009 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 5010 | if (srv->ssl_ctx.alpn_str) |
| 5011 | free(srv->ssl_ctx.alpn_str); |
| 5012 | #endif |
Lukas Tribus | da95fd9 | 2018-11-25 13:21:27 +0100 | [diff] [blame] | 5013 | #ifdef OPENSSL_NPN_NEGOTIATED |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 5014 | if (srv->ssl_ctx.npn_str) |
| 5015 | free(srv->ssl_ctx.npn_str); |
Lukas Tribus | 7706b85 | 2018-11-26 22:57:17 +0100 | [diff] [blame] | 5016 | #endif |
Christopher Faulet | 77fe80c | 2015-07-29 13:02:40 +0200 | [diff] [blame] | 5017 | if (srv->ssl_ctx.ctx) |
| 5018 | SSL_CTX_free(srv->ssl_ctx.ctx); |
| 5019 | } |
| 5020 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5021 | /* 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] | 5022 | * be NULL, in which case nothing is done. The default_ctx is nullified too. |
| 5023 | */ |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5024 | void ssl_sock_free_all_ctx(struct bind_conf *bind_conf) |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5025 | { |
| 5026 | struct ebmb_node *node, *back; |
| 5027 | struct sni_ctx *sni; |
| 5028 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5029 | node = ebmb_first(&bind_conf->sni_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5030 | while (node) { |
| 5031 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5032 | back = ebmb_next(node); |
| 5033 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5034 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5035 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5036 | ssl_sock_free_ssl_conf(sni->conf); |
| 5037 | free(sni->conf); |
| 5038 | sni->conf = NULL; |
| 5039 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5040 | free(sni); |
| 5041 | node = back; |
| 5042 | } |
| 5043 | |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5044 | node = ebmb_first(&bind_conf->sni_w_ctx); |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5045 | while (node) { |
| 5046 | sni = ebmb_entry(node, struct sni_ctx, name); |
| 5047 | back = ebmb_next(node); |
| 5048 | ebmb_delete(node); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5049 | if (!sni->order) { /* only free the CTX on its first occurrence */ |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5050 | SSL_CTX_free(sni->ctx); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5051 | ssl_sock_free_ssl_conf(sni->conf); |
| 5052 | free(sni->conf); |
| 5053 | sni->conf = NULL; |
| 5054 | } |
Emeric Brun | fc0421f | 2012-09-07 17:30:07 +0200 | [diff] [blame] | 5055 | free(sni); |
| 5056 | node = back; |
| 5057 | } |
Emmanuel Hocdet | f6b37c6 | 2017-03-06 15:34:44 +0100 | [diff] [blame] | 5058 | SSL_CTX_free(bind_conf->initial_ctx); |
| 5059 | bind_conf->initial_ctx = NULL; |
Willy Tarreau | 2a65ff0 | 2012-09-13 17:54:29 +0200 | [diff] [blame] | 5060 | bind_conf->default_ctx = NULL; |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5061 | bind_conf->default_ssl_conf = NULL; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5062 | } |
| 5063 | |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5064 | /* Destroys all the contexts for a bind_conf. This is used during deinit(). */ |
| 5065 | void ssl_sock_destroy_bind_conf(struct bind_conf *bind_conf) |
| 5066 | { |
| 5067 | ssl_sock_free_ca(bind_conf); |
| 5068 | ssl_sock_free_all_ctx(bind_conf); |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 5069 | ssl_sock_free_ssl_conf(&bind_conf->ssl_conf); |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5070 | free(bind_conf->ca_sign_file); |
| 5071 | free(bind_conf->ca_sign_pass); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 5072 | if (bind_conf->keys_ref && !--bind_conf->keys_ref->refcount) { |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5073 | free(bind_conf->keys_ref->filename); |
| 5074 | free(bind_conf->keys_ref->tlskeys); |
| 5075 | LIST_DEL(&bind_conf->keys_ref->list); |
| 5076 | free(bind_conf->keys_ref); |
| 5077 | } |
| 5078 | bind_conf->keys_ref = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5079 | bind_conf->ca_sign_pass = NULL; |
| 5080 | bind_conf->ca_sign_file = NULL; |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 5081 | } |
| 5082 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5083 | /* Load CA cert file and private key used to generate certificates */ |
| 5084 | int |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5085 | ssl_sock_load_ca(struct bind_conf *bind_conf) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5086 | { |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 5087 | struct proxy *px = bind_conf->frontend; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5088 | FILE *fp; |
| 5089 | X509 *cacert = NULL; |
| 5090 | EVP_PKEY *capkey = NULL; |
| 5091 | int err = 0; |
| 5092 | |
Christopher Faulet | f8bb0ce | 2017-09-15 09:52:49 +0200 | [diff] [blame] | 5093 | if (!bind_conf->generate_certs) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5094 | return err; |
| 5095 | |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5096 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5097 | if (global_ssl.ctx_cache) { |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5098 | ssl_ctx_lru_tree = lru64_new(global_ssl.ctx_cache); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5099 | } |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5100 | ssl_ctx_lru_seed = (unsigned int)time(NULL); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 5101 | ssl_ctx_serial = now_ms; |
Willy Tarreau | a84c267 | 2015-10-09 12:10:13 +0200 | [diff] [blame] | 5102 | #endif |
Christopher Faulet | d2cab92 | 2015-07-28 16:03:47 +0200 | [diff] [blame] | 5103 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5104 | if (!bind_conf->ca_sign_file) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5105 | ha_alert("Proxy '%s': cannot enable certificate generation, " |
| 5106 | "no CA certificate File configured at [%s:%d].\n", |
| 5107 | px->id, bind_conf->file, bind_conf->line); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5108 | goto load_error; |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5109 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5110 | |
| 5111 | /* read in the CA certificate */ |
| 5112 | if (!(fp = fopen(bind_conf->ca_sign_file, "r"))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5113 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5114 | 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] | 5115 | goto load_error; |
| 5116 | } |
| 5117 | if (!(cacert = PEM_read_X509(fp, NULL, NULL, NULL))) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 5118 | ha_alert("Proxy '%s': Failed to read CA certificate file '%s' at [%s:%d].\n", |
| 5119 | 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] | 5120 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5121 | } |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5122 | rewind(fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5123 | 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] | 5124 | ha_alert("Proxy '%s': Failed to read CA private key file '%s' at [%s:%d].\n", |
| 5125 | 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] | 5126 | goto read_error; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5127 | } |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5128 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5129 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5130 | bind_conf->ca_sign_cert = cacert; |
| 5131 | bind_conf->ca_sign_pkey = capkey; |
| 5132 | return err; |
| 5133 | |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5134 | read_error: |
| 5135 | fclose (fp); |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5136 | if (capkey) EVP_PKEY_free(capkey); |
| 5137 | if (cacert) X509_free(cacert); |
Christopher Faulet | c6f02fb | 2015-10-09 10:53:31 +0200 | [diff] [blame] | 5138 | load_error: |
| 5139 | bind_conf->generate_certs = 0; |
| 5140 | err++; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5141 | return err; |
| 5142 | } |
| 5143 | |
| 5144 | /* Release CA cert and private key used to generate certificated */ |
| 5145 | void |
| 5146 | ssl_sock_free_ca(struct bind_conf *bind_conf) |
| 5147 | { |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5148 | if (bind_conf->ca_sign_pkey) |
| 5149 | EVP_PKEY_free(bind_conf->ca_sign_pkey); |
| 5150 | if (bind_conf->ca_sign_cert) |
| 5151 | X509_free(bind_conf->ca_sign_cert); |
Willy Tarreau | 94ff03a | 2016-12-22 17:57:46 +0100 | [diff] [blame] | 5152 | bind_conf->ca_sign_pkey = NULL; |
| 5153 | bind_conf->ca_sign_cert = NULL; |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 5154 | } |
| 5155 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5156 | /* |
| 5157 | * This function is called if SSL * context is not yet allocated. The function |
| 5158 | * is designed to be called before any other data-layer operation and sets the |
| 5159 | * handshake flag on the connection. It is safe to call it multiple times. |
| 5160 | * It returns 0 on success and -1 in error case. |
| 5161 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5162 | static int ssl_sock_init(struct connection *conn, void **xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5163 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5164 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5165 | /* already initialized */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5166 | if (*xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5167 | return 0; |
| 5168 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5169 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5170 | return 0; |
| 5171 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5172 | ctx = pool_alloc(ssl_sock_ctx_pool); |
| 5173 | if (!ctx) { |
| 5174 | conn->err_code = CO_ER_SSL_NO_MEM; |
| 5175 | return -1; |
| 5176 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5177 | ctx->sent_early_data = 0; |
| 5178 | ctx->tmp_early_data = -1; |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5179 | ctx->conn = conn; |
| 5180 | |
| 5181 | /* Only work with sockets for now, this should be adapted when we'll |
| 5182 | * add QUIC support. |
| 5183 | */ |
| 5184 | ctx->xprt = xprt_get(XPRT_RAW); |
| 5185 | if (ctx->xprt->init) |
| 5186 | ctx->xprt->init(conn, &ctx->xprt_ctx); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5187 | |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5188 | if (global.maxsslconn && sslconns >= global.maxsslconn) { |
| 5189 | conn->err_code = CO_ER_SSL_TOO_MANY; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5190 | return -1; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5191 | } |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5192 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5193 | /* If it is in client mode initiate SSL session |
| 5194 | in connect state otherwise accept state */ |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5195 | if (objt_server(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5196 | int may_retry = 1; |
| 5197 | |
| 5198 | retry_connect: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5199 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5200 | ctx->ssl = SSL_new(__objt_server(conn->target)->ssl_ctx.ctx); |
| 5201 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5202 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5203 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5204 | goto retry_connect; |
| 5205 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5206 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5207 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5208 | } |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5209 | ctx->bio = BIO_new(ha_meth); |
| 5210 | if (!ctx->bio) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5211 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5212 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5213 | goto retry_connect; |
| 5214 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5215 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5216 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5217 | } |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5218 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000 |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5219 | ctx->bio->ptr = ctx; |
| 5220 | #else |
| 5221 | BIO_set_data(ctx->bio, ctx); |
| 5222 | #endif |
| 5223 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5224 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5225 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5226 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5227 | SSL_free(ctx->ssl); |
| 5228 | ctx->ssl = NULL; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5229 | conn->xprt_ctx = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5230 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5231 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5232 | goto retry_connect; |
| 5233 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5234 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5235 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5236 | } |
| 5237 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5238 | SSL_set_connect_state(ctx->ssl); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5239 | if (__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5240 | const unsigned char *ptr = __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr; |
| 5241 | 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] | 5242 | if (sess && !SSL_set_session(ctx->ssl, sess)) { |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5243 | SSL_SESSION_free(sess); |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5244 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5245 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Olivier Houchard | e6060c5 | 2017-11-16 17:42:52 +0100 | [diff] [blame] | 5246 | } else if (sess) { |
| 5247 | SSL_SESSION_free(sess); |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5248 | } |
| 5249 | } |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 5250 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5251 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5252 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5253 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5254 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5255 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5256 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5257 | return 0; |
| 5258 | } |
Willy Tarreau | 3fdb366 | 2012-11-12 00:42:33 +0100 | [diff] [blame] | 5259 | else if (objt_listener(conn->target)) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5260 | int may_retry = 1; |
| 5261 | |
| 5262 | retry_accept: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5263 | /* Alloc a new SSL session ctx */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5264 | ctx->ssl = SSL_new(__objt_listener(conn->target)->bind_conf->initial_ctx); |
| 5265 | if (!ctx->ssl) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5266 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5267 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5268 | goto retry_accept; |
| 5269 | } |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5270 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5271 | goto err; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5272 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5273 | |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5274 | ctx->bio = BIO_new(ha_meth); |
| 5275 | if (!ctx->bio) { |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5276 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5277 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5278 | goto retry_accept; |
| 5279 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5280 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5281 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5282 | } |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 5283 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000 |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 5284 | ctx->bio->ptr = ctx; |
| 5285 | #else |
| 5286 | BIO_set_data(ctx->bio, ctx); |
| 5287 | #endif |
| 5288 | SSL_set_bio(ctx->ssl, ctx->bio, ctx->bio); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5289 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5290 | /* set connection pointer */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5291 | if (!SSL_set_ex_data(ctx->ssl, ssl_app_data_index, conn)) { |
| 5292 | SSL_free(ctx->ssl); |
| 5293 | ctx->ssl = NULL; |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5294 | if (may_retry--) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5295 | pool_gc(NULL); |
Willy Tarreau | fba03cd | 2014-11-13 13:48:58 +0100 | [diff] [blame] | 5296 | goto retry_accept; |
| 5297 | } |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5298 | conn->err_code = CO_ER_SSL_NO_MEM; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5299 | goto err; |
Emeric Brun | 5547615 | 2014-11-12 17:35:37 +0100 | [diff] [blame] | 5300 | } |
| 5301 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5302 | SSL_set_accept_state(ctx->ssl); |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5303 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5304 | /* leave init state and start handshake */ |
Willy Tarreau | 0573747 | 2012-09-04 08:03:39 +0200 | [diff] [blame] | 5305 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5306 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) || defined(OPENSSL_IS_BORINGSSL) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5307 | conn->flags |= CO_FL_EARLY_SSL_HS; |
| 5308 | #endif |
Willy Tarreau | 403edff | 2012-09-06 11:58:37 +0200 | [diff] [blame] | 5309 | |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5310 | _HA_ATOMIC_ADD(&sslconns, 1); |
| 5311 | _HA_ATOMIC_ADD(&totalsslconns, 1); |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5312 | *xprt_ctx = ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5313 | return 0; |
| 5314 | } |
| 5315 | /* don't know how to handle such a target */ |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5316 | conn->err_code = CO_ER_SSL_NO_TARGET; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5317 | err: |
| 5318 | pool_free(ssl_sock_ctx_pool, ctx); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5319 | return -1; |
| 5320 | } |
| 5321 | |
| 5322 | |
| 5323 | /* This is the callback which is used when an SSL handshake is pending. It |
| 5324 | * updates the FD status if it wants some polling before being called again. |
| 5325 | * It returns 0 if it fails in a fatal way or needs to poll to go further, |
| 5326 | * otherwise it returns non-zero and removes itself from the connection's |
| 5327 | * flags (the bit is provided in <flag> by the caller). |
| 5328 | */ |
| 5329 | int ssl_sock_handshake(struct connection *conn, unsigned int flag) |
| 5330 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5331 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5332 | int ret; |
| 5333 | |
Willy Tarreau | 3c72872 | 2014-01-23 13:50:42 +0100 | [diff] [blame] | 5334 | if (!conn_ctrl_ready(conn)) |
Willy Tarreau | f79c817 | 2013-10-21 16:30:56 +0200 | [diff] [blame] | 5335 | return 0; |
| 5336 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 5337 | if (!conn->xprt_ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5338 | goto out_error; |
| 5339 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5340 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10101000L |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5341 | /* |
| 5342 | * Check if we have early data. If we do, we have to read them |
| 5343 | * before SSL_do_handshake() is called, And there's no way to |
| 5344 | * detect early data, except to try to read them |
| 5345 | */ |
| 5346 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5347 | size_t read_data; |
| 5348 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5349 | ret = SSL_read_early_data(ctx->ssl, &ctx->tmp_early_data, |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5350 | 1, &read_data); |
| 5351 | if (ret == SSL_READ_EARLY_DATA_ERROR) |
| 5352 | goto check_error; |
| 5353 | if (ret == SSL_READ_EARLY_DATA_SUCCESS) { |
| 5354 | conn->flags &= ~(CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN); |
| 5355 | return 1; |
| 5356 | } else |
| 5357 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5358 | } |
| 5359 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5360 | /* If we use SSL_do_handshake to process a reneg initiated by |
| 5361 | * the remote peer, it sometimes returns SSL_ERROR_SSL. |
| 5362 | * Usually SSL_write and SSL_read are used and process implicitly |
| 5363 | * the reneg handshake. |
| 5364 | * Here we use SSL_peek as a workaround for reneg. |
| 5365 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5366 | if ((conn->flags & CO_FL_CONNECTED) && SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5367 | char c; |
| 5368 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5369 | ret = SSL_peek(ctx->ssl, &c, 1); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5370 | if (ret <= 0) { |
| 5371 | /* handshake may have not been completed, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5372 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5373 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5374 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5375 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5376 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5377 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5378 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5379 | return 0; |
| 5380 | } |
| 5381 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5382 | /* handshake may have been completed but we have |
| 5383 | * no more data to read. |
| 5384 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5385 | if (!SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5386 | ret = 1; |
| 5387 | goto reneg_ok; |
| 5388 | } |
| 5389 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5390 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5391 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5392 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5393 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5394 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5395 | return 0; |
| 5396 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5397 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5398 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5399 | ssl_async_process_fds(conn, ctx->ssl); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5400 | return 0; |
| 5401 | } |
| 5402 | #endif |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5403 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5404 | /* if errno is null, then connection was successfully established */ |
| 5405 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5406 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5407 | if (!conn->err_code) { |
Emeric Brun | 77e8919 | 2018-08-16 11:36:40 +0200 | [diff] [blame] | 5408 | #ifdef OPENSSL_IS_BORINGSSL /* BoringSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5409 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5410 | #else |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5411 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5412 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5413 | OSSL_HANDSHAKE_STATE state = SSL_get_state((SSL *)ctx->ssl); |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5414 | empty_handshake = state == TLS_ST_BEFORE; |
| 5415 | #else |
Ilya Shipitsin | 54832b9 | 2019-05-05 23:27:54 +0500 | [diff] [blame] | 5416 | empty_handshake = SSL_state((SSL *)ctx->ssl) == SSL_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5417 | #endif |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5418 | if (empty_handshake) { |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5419 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5420 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5421 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5422 | else |
| 5423 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5424 | } |
| 5425 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5426 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5427 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5428 | else |
| 5429 | conn->err_code = CO_ER_SSL_ABORT; |
| 5430 | } |
| 5431 | } |
| 5432 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5433 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5434 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5435 | else |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5436 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5437 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5438 | #endif |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5439 | } |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5440 | goto out_error; |
| 5441 | } |
| 5442 | else { |
| 5443 | /* Fail on all other handshake errors */ |
| 5444 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5445 | * buffer, causing an RST to be emitted upon close() on |
| 5446 | * TCP sockets. We first try to drain possibly pending |
| 5447 | * data to avoid this as much as possible. |
| 5448 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5449 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5450 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5451 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5452 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5453 | goto out_error; |
| 5454 | } |
| 5455 | } |
| 5456 | /* read some data: consider handshake completed */ |
| 5457 | goto reneg_ok; |
| 5458 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5459 | ret = SSL_do_handshake(ctx->ssl); |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5460 | check_error: |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5461 | if (ret != 1) { |
| 5462 | /* handshake did not complete, let's find why */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5463 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5464 | |
| 5465 | if (ret == SSL_ERROR_WANT_WRITE) { |
| 5466 | /* SSL handshake needs to write, L4 connection may not be ready */ |
| 5467 | __conn_sock_stop_recv(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5468 | __conn_sock_want_send(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5469 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5470 | return 0; |
| 5471 | } |
| 5472 | else if (ret == SSL_ERROR_WANT_READ) { |
| 5473 | /* SSL handshake needs to read, L4 connection is ready */ |
| 5474 | if (conn->flags & CO_FL_WAIT_L4_CONN) |
| 5475 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
| 5476 | __conn_sock_stop_send(conn); |
Willy Tarreau | e1f50c4 | 2014-01-22 20:02:06 +0100 | [diff] [blame] | 5477 | __conn_sock_want_recv(conn); |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5478 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5479 | return 0; |
| 5480 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5481 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5482 | else if (ret == SSL_ERROR_WANT_ASYNC) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5483 | ssl_async_process_fds(conn, ctx->ssl); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5484 | return 0; |
| 5485 | } |
| 5486 | #endif |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5487 | else if (ret == SSL_ERROR_SYSCALL) { |
| 5488 | /* if errno is null, then connection was successfully established */ |
| 5489 | if (!errno && conn->flags & CO_FL_WAIT_L4_CONN) |
| 5490 | conn->flags &= ~CO_FL_WAIT_L4_CONN; |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5491 | if (!conn->err_code) { |
Emeric Brun | 77e8919 | 2018-08-16 11:36:40 +0200 | [diff] [blame] | 5492 | #ifdef OPENSSL_IS_BORINGSSL /* BoringSSL */ |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5493 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
| 5494 | #else |
| 5495 | int empty_handshake; |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5496 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5497 | OSSL_HANDSHAKE_STATE state = SSL_get_state(ctx->ssl); |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5498 | empty_handshake = state == TLS_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5499 | #else |
Ilya Shipitsin | 54832b9 | 2019-05-05 23:27:54 +0500 | [diff] [blame] | 5500 | empty_handshake = SSL_state((SSL *)ctx->ssl) == SSL_ST_BEFORE; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 5501 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5502 | if (empty_handshake) { |
| 5503 | if (!errno) { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5504 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5505 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5506 | else |
| 5507 | conn->err_code = CO_ER_SSL_EMPTY; |
| 5508 | } |
| 5509 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5510 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5511 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5512 | else |
| 5513 | conn->err_code = CO_ER_SSL_ABORT; |
| 5514 | } |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5515 | } |
| 5516 | else { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5517 | if (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5518 | conn->err_code = CO_ER_SSL_HANDSHAKE_HB; |
| 5519 | else |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5520 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5521 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 5522 | #endif |
Emeric Brun | 29f037d | 2014-04-25 19:05:36 +0200 | [diff] [blame] | 5523 | } |
Willy Tarreau | 8923019 | 2012-09-28 20:22:13 +0200 | [diff] [blame] | 5524 | goto out_error; |
| 5525 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5526 | else { |
| 5527 | /* Fail on all other handshake errors */ |
Willy Tarreau | 566dc55 | 2012-10-19 20:52:18 +0200 | [diff] [blame] | 5528 | /* Note: OpenSSL may leave unread bytes in the socket's |
| 5529 | * buffer, causing an RST to be emitted upon close() on |
| 5530 | * TCP sockets. We first try to drain possibly pending |
| 5531 | * data to avoid this as much as possible. |
| 5532 | */ |
Willy Tarreau | d85c485 | 2015-03-13 00:40:28 +0100 | [diff] [blame] | 5533 | conn_sock_drain(conn); |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5534 | if (!conn->err_code) |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5535 | conn->err_code = (ctx->xprt_st & SSL_SOCK_RECV_HEARTBEAT) ? |
Willy Tarreau | f51c698 | 2014-04-25 20:02:39 +0200 | [diff] [blame] | 5536 | CO_ER_SSL_KILLED_HB : CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5537 | goto out_error; |
| 5538 | } |
| 5539 | } |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5540 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5541 | else { |
| 5542 | /* |
| 5543 | * If the server refused the early data, we have to send a |
| 5544 | * 425 to the client, as we no longer have the data to sent |
| 5545 | * them again. |
| 5546 | */ |
| 5547 | if ((conn->flags & CO_FL_EARLY_DATA) && (objt_server(conn->target))) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5548 | if (SSL_get_early_data_status(ctx->ssl) == SSL_EARLY_DATA_REJECTED) { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5549 | conn->err_code = CO_ER_SSL_EARLY_FAILED; |
| 5550 | goto out_error; |
| 5551 | } |
| 5552 | } |
| 5553 | } |
| 5554 | #endif |
| 5555 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5556 | |
Emeric Brun | 674b743 | 2012-11-08 19:21:55 +0100 | [diff] [blame] | 5557 | reneg_ok: |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5558 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5559 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5560 | /* ASYNC engine API doesn't support moving read/write |
| 5561 | * buffers. So we disable ASYNC mode right after |
| 5562 | * the handshake to avoid buffer oveflows. |
| 5563 | */ |
| 5564 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5565 | SSL_clear_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5566 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5567 | /* Handshake succeeded */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5568 | if (!SSL_session_reused(ctx->ssl)) { |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5569 | if (objt_server(conn->target)) { |
| 5570 | update_freq_ctr(&global.ssl_be_keys_per_sec, 1); |
| 5571 | if (global.ssl_be_keys_per_sec.curr_ctr > global.ssl_be_keys_max) |
| 5572 | 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] | 5573 | } |
Willy Tarreau | 0c9c272 | 2014-05-28 12:28:58 +0200 | [diff] [blame] | 5574 | else { |
| 5575 | update_freq_ctr(&global.ssl_fe_keys_per_sec, 1); |
| 5576 | if (global.ssl_fe_keys_per_sec.curr_ctr > global.ssl_fe_keys_max) |
| 5577 | global.ssl_fe_keys_max = global.ssl_fe_keys_per_sec.curr_ctr; |
| 5578 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5579 | } |
| 5580 | |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5581 | #ifdef OPENSSL_IS_BORINGSSL |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5582 | if ((conn->flags & CO_FL_EARLY_SSL_HS) && !SSL_in_early_data(ctx->ssl)) |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5583 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5584 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5585 | /* The connection is now established at both layers, it's time to leave */ |
| 5586 | conn->flags &= ~(flag | CO_FL_WAIT_L4_CONN | CO_FL_WAIT_L6_CONN); |
| 5587 | return 1; |
| 5588 | |
| 5589 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5590 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5591 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5592 | ERR_clear_error(); |
| 5593 | |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5594 | /* free resumed session if exists */ |
Willy Tarreau | 07d94e4 | 2018-09-20 10:57:52 +0200 | [diff] [blame] | 5595 | if (objt_server(conn->target) && __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr) { |
| 5596 | free(__objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr); |
| 5597 | __objt_server(conn->target)->ssl_ctx.reused_sess[tid].ptr = NULL; |
Emeric Brun | 9fa8973 | 2012-10-04 17:09:56 +0200 | [diff] [blame] | 5598 | } |
| 5599 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5600 | /* Fail on all other handshake errors */ |
| 5601 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 20879a0 | 2012-12-03 16:32:10 +0100 | [diff] [blame] | 5602 | if (!conn->err_code) |
| 5603 | conn->err_code = CO_ER_SSL_HANDSHAKE; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5604 | return 0; |
| 5605 | } |
| 5606 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5607 | 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] | 5608 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5609 | |
| 5610 | return conn_subscribe(conn, NULL, event_type, param); |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5611 | } |
| 5612 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5613 | 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] | 5614 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5615 | |
| 5616 | return conn_unsubscribe(conn, NULL, event_type, param); |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 5617 | } |
| 5618 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5619 | /* 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] | 5620 | * into buffer <buf>. Only one call to recv() is performed, unless the |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5621 | * buffer wraps, in which case a second call may be performed. The connection's |
| 5622 | * flags are updated with whatever special event is detected (error, read0, |
| 5623 | * empty). The caller is responsible for taking care of those events and |
| 5624 | * avoiding the call if inappropriate. The function does not call the |
| 5625 | * connection's polling update function, so the caller is responsible for this. |
| 5626 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5627 | 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] | 5628 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5629 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | bfc4d77 | 2018-07-18 11:22:03 +0200 | [diff] [blame] | 5630 | ssize_t ret; |
| 5631 | size_t try, done = 0; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5632 | |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5633 | conn_refresh_polling_flags(conn); |
| 5634 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5635 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5636 | goto out_error; |
| 5637 | |
| 5638 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5639 | /* a handshake was requested */ |
| 5640 | return 0; |
| 5641 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5642 | /* read the largest possible block. For this, we perform only one call |
| 5643 | * to recv() unless the buffer wraps and we exactly fill the first hunk, |
| 5644 | * in which case we accept to do it once again. A new attempt is made on |
| 5645 | * EINTR too. |
| 5646 | */ |
Willy Tarreau | 00b0fb9 | 2014-01-17 11:09:40 +0100 | [diff] [blame] | 5647 | while (count > 0) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5648 | int need_out = 0; |
| 5649 | |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5650 | try = b_contig_space(buf); |
| 5651 | if (!try) |
| 5652 | break; |
| 5653 | |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5654 | if (try > count) |
| 5655 | try = count; |
Willy Tarreau | 591d445 | 2018-06-15 17:21:00 +0200 | [diff] [blame] | 5656 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5657 | 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] | 5658 | ctx->tmp_early_data != -1) { |
| 5659 | *b_tail(buf) = ctx->tmp_early_data; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5660 | done++; |
| 5661 | try--; |
| 5662 | count--; |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5663 | b_add(buf, 1); |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5664 | ctx->tmp_early_data = -1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5665 | continue; |
| 5666 | } |
Willy Tarreau | abf08d9 | 2014-01-14 11:31:27 +0100 | [diff] [blame] | 5667 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5668 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5669 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
| 5670 | size_t read_length; |
| 5671 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5672 | ret = SSL_read_early_data(ctx->ssl, |
Willy Tarreau | 8f9c72d | 2018-06-07 18:46:28 +0200 | [diff] [blame] | 5673 | b_tail(buf), try, &read_length); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5674 | if (ret == SSL_READ_EARLY_DATA_SUCCESS && |
| 5675 | read_length > 0) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5676 | conn->flags |= CO_FL_EARLY_DATA; |
| 5677 | if (ret == SSL_READ_EARLY_DATA_SUCCESS || |
| 5678 | ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5679 | if (ret == SSL_READ_EARLY_DATA_FINISH) { |
| 5680 | /* |
| 5681 | * We're done reading the early data, |
| 5682 | * let's make the handshake |
| 5683 | */ |
| 5684 | conn->flags &= ~CO_FL_EARLY_SSL_HS; |
| 5685 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5686 | need_out = 1; |
| 5687 | if (read_length == 0) |
| 5688 | break; |
| 5689 | } |
| 5690 | ret = read_length; |
| 5691 | } |
| 5692 | } else |
| 5693 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5694 | ret = SSL_read(ctx->ssl, b_tail(buf), try); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5695 | #ifdef OPENSSL_IS_BORINGSSL |
| 5696 | if (conn->flags & CO_FL_EARLY_SSL_HS) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5697 | if (SSL_in_early_data(ctx->ssl)) { |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5698 | if (ret > 0) |
| 5699 | conn->flags |= CO_FL_EARLY_DATA; |
| 5700 | } else { |
Emmanuel Hocdet | cebd796 | 2017-11-27 16:14:40 +0100 | [diff] [blame] | 5701 | conn->flags &= ~(CO_FL_EARLY_SSL_HS); |
Emmanuel Hocdet | ca6a957 | 2017-11-23 12:40:07 +0100 | [diff] [blame] | 5702 | } |
| 5703 | } |
| 5704 | #endif |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5705 | if (conn->flags & CO_FL_ERROR) { |
| 5706 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5707 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5708 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5709 | if (ret > 0) { |
Olivier Houchard | acd1403 | 2018-06-28 18:17:23 +0200 | [diff] [blame] | 5710 | b_add(buf, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5711 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5712 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5713 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5714 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5715 | ret = SSL_get_error(ctx->ssl, ret); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5716 | if (ret == SSL_ERROR_WANT_WRITE) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5717 | /* handshake is running, and it needs to enable write */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5718 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5719 | __conn_sock_want_send(conn); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5720 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5721 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5722 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5723 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5724 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5725 | break; |
| 5726 | } |
| 5727 | else if (ret == SSL_ERROR_WANT_READ) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5728 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5729 | /* handshake is running, and it may need to re-enable read */ |
| 5730 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5731 | __conn_sock_want_recv(conn); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5732 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5733 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5734 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5735 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5736 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5737 | break; |
| 5738 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5739 | /* we need to poll for retry a read later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5740 | fd_cant_recv(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5741 | break; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5742 | } else if (ret == SSL_ERROR_ZERO_RETURN) |
| 5743 | goto read0; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5744 | /* For SSL_ERROR_SYSCALL, make sure to clear the error |
| 5745 | * stack before shutting down the connection for |
| 5746 | * reading. */ |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5747 | if (ret == SSL_ERROR_SYSCALL && (!errno || errno == EAGAIN)) |
| 5748 | goto clear_ssl_error; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5749 | /* otherwise it's a real error */ |
| 5750 | goto out_error; |
| 5751 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5752 | if (need_out) |
| 5753 | break; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5754 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5755 | leave: |
| 5756 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5757 | return done; |
| 5758 | |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5759 | clear_ssl_error: |
| 5760 | /* Clear openssl global errors stack */ |
| 5761 | ssl_sock_dump_errors(conn); |
| 5762 | ERR_clear_error(); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5763 | read0: |
| 5764 | conn_sock_read0(conn); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5765 | goto leave; |
Christopher Faulet | 4ac77a9 | 2018-02-19 14:25:15 +0100 | [diff] [blame] | 5766 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5767 | out_error: |
Olivier Houchard | 7e2e505 | 2018-02-13 15:17:23 +0100 | [diff] [blame] | 5768 | conn->flags |= CO_FL_ERROR; |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5769 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5770 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5771 | ERR_clear_error(); |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5772 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5773 | } |
| 5774 | |
| 5775 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5776 | /* Send up to <count> pending bytes from buffer <buf> to connection <conn>'s |
| 5777 | * socket. <flags> may contain some CO_SFL_* flags to hint the system about |
| 5778 | * 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] | 5779 | * Only one call to send() is performed, unless the buffer wraps, in which case |
| 5780 | * a second call may be performed. The connection's flags are updated with |
| 5781 | * whatever special event is detected (error, empty). The caller is responsible |
| 5782 | * for taking care of those events and avoiding the call if inappropriate. The |
| 5783 | * 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] | 5784 | * is responsible for this. The buffer's output is not adjusted, it's up to the |
| 5785 | * caller to take care of this. It's up to the caller to update the buffer's |
| 5786 | * contents based on the return value. |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5787 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5788 | 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] | 5789 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5790 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5791 | ssize_t ret; |
| 5792 | size_t try, done; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5793 | |
| 5794 | done = 0; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5795 | conn_refresh_polling_flags(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5796 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5797 | if (!ctx) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5798 | goto out_error; |
| 5799 | |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5800 | if (conn->flags & (CO_FL_HANDSHAKE | CO_FL_EARLY_SSL_HS)) |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5801 | /* a handshake was requested */ |
| 5802 | return 0; |
| 5803 | |
| 5804 | /* send the largest possible block. For this we perform only one call |
| 5805 | * to send() unless the buffer wraps and we exactly fill the first hunk, |
| 5806 | * in which case we accept to do it once again. |
| 5807 | */ |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5808 | while (count) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5809 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5810 | size_t written_data; |
| 5811 | #endif |
| 5812 | |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5813 | try = b_contig_data(buf, done); |
| 5814 | if (try > count) |
| 5815 | try = count; |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5816 | |
Willy Tarreau | 7bed945 | 2014-02-02 02:00:24 +0100 | [diff] [blame] | 5817 | if (!(flags & CO_SFL_STREAMER) && |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5818 | !(ctx->xprt_st & SSL_SOCK_SEND_UNLIMITED) && |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 5819 | global_ssl.max_record && try > global_ssl.max_record) { |
| 5820 | try = global_ssl.max_record; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5821 | } |
| 5822 | else { |
| 5823 | /* we need to keep the information about the fact that |
| 5824 | * we're not limiting the upcoming send(), because if it |
| 5825 | * fails, we'll have to retry with at least as many data. |
| 5826 | */ |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5827 | ctx->xprt_st |= SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5828 | } |
Willy Tarreau | bfd5946 | 2013-02-21 07:46:09 +0100 | [diff] [blame] | 5829 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5830 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L) |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5831 | if (!SSL_is_init_finished(ctx->ssl) && conn_is_back(conn)) { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5832 | unsigned int max_early; |
| 5833 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5834 | if (objt_listener(conn->target)) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5835 | max_early = SSL_get_max_early_data(ctx->ssl); |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5836 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5837 | if (SSL_get0_session(ctx->ssl)) |
| 5838 | 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] | 5839 | else |
| 5840 | max_early = 0; |
| 5841 | } |
| 5842 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5843 | if (try + ctx->sent_early_data > max_early) { |
| 5844 | try -= (try + ctx->sent_early_data) - max_early; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5845 | if (try <= 0) { |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5846 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5847 | break; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5848 | } |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5849 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5850 | 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] | 5851 | if (ret == 1) { |
| 5852 | ret = written_data; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5853 | ctx->sent_early_data += ret; |
Olivier Houchard | 010941f | 2019-05-03 20:56:19 +0200 | [diff] [blame] | 5854 | if (objt_server(conn->target)) |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5855 | conn->flags |= CO_FL_SSL_WAIT_HS | CO_FL_WAIT_L6_CONN | CO_FL_EARLY_DATA; |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 5856 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 5857 | } |
| 5858 | |
| 5859 | } else |
| 5860 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5861 | ret = SSL_write(ctx->ssl, b_peek(buf, done), try); |
Willy Tarreau | 518cedd | 2014-02-17 15:43:01 +0100 | [diff] [blame] | 5862 | |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5863 | if (conn->flags & CO_FL_ERROR) { |
| 5864 | /* CO_FL_ERROR may be set by ssl_sock_infocbk */ |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5865 | goto out_error; |
Emeric Brun | e1f38db | 2012-09-03 20:36:47 +0200 | [diff] [blame] | 5866 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5867 | if (ret > 0) { |
Olivier Houchard | f24502b | 2019-01-17 19:09:11 +0100 | [diff] [blame] | 5868 | /* A send succeeded, so we can consier ourself connected */ |
| 5869 | conn->flags |= CO_FL_CONNECTED; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 5870 | ctx->xprt_st &= ~SSL_SOCK_SEND_UNLIMITED; |
Willy Tarreau | 787db9a | 2018-06-14 18:31:46 +0200 | [diff] [blame] | 5871 | count -= ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5872 | done += ret; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5873 | } |
| 5874 | else { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5875 | ret = SSL_get_error(ctx->ssl, ret); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5876 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5877 | if (ret == SSL_ERROR_WANT_WRITE) { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5878 | if (SSL_renegotiate_pending(ctx->ssl)) { |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5879 | /* handshake is running, and it may need to re-enable write */ |
| 5880 | conn->flags |= CO_FL_SSL_WAIT_HS; |
| 5881 | __conn_sock_want_send(conn); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5882 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5883 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5884 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5885 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5886 | #endif |
Emeric Brun | 282a76a | 2012-11-08 18:02:56 +0100 | [diff] [blame] | 5887 | break; |
| 5888 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5889 | /* we need to poll to retry a write later */ |
Willy Tarreau | 585744b | 2017-08-24 14:31:19 +0200 | [diff] [blame] | 5890 | fd_cant_send(conn->handle.fd); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5891 | break; |
| 5892 | } |
| 5893 | else if (ret == SSL_ERROR_WANT_READ) { |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5894 | /* handshake is running, and it needs to enable read */ |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5895 | conn->flags |= CO_FL_SSL_WAIT_HS; |
Emeric Brun | 8af8dd1 | 2012-11-08 17:56:20 +0100 | [diff] [blame] | 5896 | __conn_sock_want_recv(conn); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5897 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5898 | /* Async mode can be re-enabled, because we're leaving data state.*/ |
| 5899 | if (global_ssl.async) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5900 | SSL_set_mode(ctx->ssl, SSL_MODE_ASYNC); |
Emeric Brun | b5e42a8 | 2017-06-06 12:35:14 +0000 | [diff] [blame] | 5901 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5902 | break; |
| 5903 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5904 | goto out_error; |
| 5905 | } |
| 5906 | } |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5907 | leave: |
| 5908 | conn_cond_update_sock_polling(conn); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5909 | return done; |
| 5910 | |
| 5911 | out_error: |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5912 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5913 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5914 | ERR_clear_error(); |
| 5915 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5916 | conn->flags |= CO_FL_ERROR; |
Willy Tarreau | 31d4dbe | 2017-10-25 09:32:15 +0200 | [diff] [blame] | 5917 | goto leave; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5918 | } |
| 5919 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5920 | static void ssl_sock_close(struct connection *conn, void *xprt_ctx) { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5921 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5922 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5923 | |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5924 | if (ctx) { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 5925 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5926 | if (global_ssl.async) { |
| 5927 | OSSL_ASYNC_FD all_fd[32], afd; |
| 5928 | size_t num_all_fds = 0; |
| 5929 | int i; |
| 5930 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5931 | SSL_get_all_async_fds(ctx->ssl, NULL, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5932 | if (num_all_fds > 32) { |
| 5933 | send_log(NULL, LOG_EMERG, "haproxy: openssl returns too many async fds. It seems a bug. Process may crash\n"); |
| 5934 | return; |
| 5935 | } |
| 5936 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5937 | SSL_get_all_async_fds(ctx->ssl, all_fd, &num_all_fds); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5938 | |
| 5939 | /* If an async job is pending, we must try to |
| 5940 | to catch the end using polling before calling |
| 5941 | SSL_free */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5942 | if (num_all_fds && SSL_waiting_for_async(ctx->ssl)) { |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5943 | for (i=0 ; i < num_all_fds ; i++) { |
| 5944 | /* switch on an handler designed to |
| 5945 | * handle the SSL_free |
| 5946 | */ |
| 5947 | afd = all_fd[i]; |
| 5948 | fdtab[afd].iocb = ssl_async_fd_free; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5949 | fdtab[afd].owner = ctx->ssl; |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5950 | fd_want_recv(afd); |
Emeric Brun | ce9e01c | 2017-05-31 10:02:53 +0000 | [diff] [blame] | 5951 | /* To ensure that the fd cache won't be used |
| 5952 | * and we'll catch a real RD event. |
| 5953 | */ |
| 5954 | fd_cant_recv(afd); |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5955 | } |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5956 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5957 | _HA_ATOMIC_ADD(&jobs, 1); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5958 | return; |
| 5959 | } |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 5960 | /* Else we can remove the fds from the fdtab |
| 5961 | * and call SSL_free. |
| 5962 | * note: we do a fd_remove and not a delete |
| 5963 | * because the fd is owned by the engine. |
| 5964 | * the engine is responsible to close |
| 5965 | */ |
| 5966 | for (i=0 ; i < num_all_fds ; i++) |
| 5967 | fd_remove(all_fd[i]); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 5968 | } |
| 5969 | #endif |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5970 | SSL_free(ctx->ssl); |
| 5971 | pool_free(ssl_sock_ctx_pool, ctx); |
Olivier Houchard | 2be5a4c | 2019-03-08 18:54:43 +0100 | [diff] [blame] | 5972 | _HA_ATOMIC_SUB(&sslconns, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5973 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5974 | } |
| 5975 | |
| 5976 | /* This function tries to perform a clean shutdown on an SSL connection, and in |
| 5977 | * any case, flags the connection as reusable if no handshake was in progress. |
| 5978 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5979 | 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] | 5980 | { |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 5981 | struct ssl_sock_ctx *ctx = xprt_ctx; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5982 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5983 | if (conn->flags & CO_FL_HANDSHAKE) |
| 5984 | return; |
Emmanuel Hocdet | 405ff31 | 2017-01-08 14:07:39 +0100 | [diff] [blame] | 5985 | if (!clean) |
| 5986 | /* don't sent notify on SSL_shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5987 | SSL_set_quiet_shutdown(ctx->ssl, 1); |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5988 | /* no handshake was in progress, try a clean ssl shutdown */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5989 | if (SSL_shutdown(ctx->ssl) <= 0) { |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5990 | /* Clear openssl global errors stack */ |
Thierry FOURNIER / OZON.IO | 8b068c2 | 2016-10-10 11:59:50 +0200 | [diff] [blame] | 5991 | ssl_sock_dump_errors(conn); |
Emeric Brun | 644cde0 | 2012-12-14 11:21:13 +0100 | [diff] [blame] | 5992 | ERR_clear_error(); |
| 5993 | } |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 5994 | } |
| 5995 | |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5996 | /* used for ppv2 pkey alog (can be used for logging) */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5997 | int ssl_sock_get_pkey_algo(struct connection *conn, struct buffer *out) |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 5998 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 5999 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6000 | struct pkey_info *pkinfo; |
| 6001 | int bits = 0; |
| 6002 | int sig = TLSEXT_signature_anonymous; |
| 6003 | int len = -1; |
| 6004 | |
| 6005 | if (!ssl_sock_is_ssl(conn)) |
| 6006 | return 0; |
| 6007 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6008 | 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] | 6009 | if (pkinfo) { |
| 6010 | sig = pkinfo->sig; |
| 6011 | bits = pkinfo->bits; |
| 6012 | } else { |
| 6013 | /* multicert and generated cert have no pkey info */ |
| 6014 | X509 *crt; |
| 6015 | EVP_PKEY *pkey; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6016 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 96b7834 | 2017-10-31 15:46:07 +0100 | [diff] [blame] | 6017 | if (!crt) |
| 6018 | return 0; |
| 6019 | pkey = X509_get_pubkey(crt); |
| 6020 | if (pkey) { |
| 6021 | bits = EVP_PKEY_bits(pkey); |
| 6022 | switch(EVP_PKEY_base_id(pkey)) { |
| 6023 | case EVP_PKEY_RSA: |
| 6024 | sig = TLSEXT_signature_rsa; |
| 6025 | break; |
| 6026 | case EVP_PKEY_EC: |
| 6027 | sig = TLSEXT_signature_ecdsa; |
| 6028 | break; |
| 6029 | case EVP_PKEY_DSA: |
| 6030 | sig = TLSEXT_signature_dsa; |
| 6031 | break; |
| 6032 | } |
| 6033 | EVP_PKEY_free(pkey); |
| 6034 | } |
| 6035 | } |
| 6036 | |
| 6037 | switch(sig) { |
| 6038 | case TLSEXT_signature_rsa: |
| 6039 | len = chunk_printf(out, "RSA%d", bits); |
| 6040 | break; |
| 6041 | case TLSEXT_signature_ecdsa: |
| 6042 | len = chunk_printf(out, "EC%d", bits); |
| 6043 | break; |
| 6044 | case TLSEXT_signature_dsa: |
| 6045 | len = chunk_printf(out, "DSA%d", bits); |
| 6046 | break; |
| 6047 | default: |
| 6048 | return 0; |
| 6049 | } |
| 6050 | if (len < 0) |
| 6051 | return 0; |
| 6052 | return 1; |
| 6053 | } |
| 6054 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6055 | /* used for ppv2 cert signature (can be used for logging) */ |
| 6056 | const char *ssl_sock_get_cert_sig(struct connection *conn) |
| 6057 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6058 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6059 | |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6060 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
| 6061 | X509 *crt; |
| 6062 | |
| 6063 | if (!ssl_sock_is_ssl(conn)) |
| 6064 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6065 | crt = SSL_get_certificate(ctx->ssl); |
Emmanuel Hocdet | 283e004 | 2017-11-02 14:05:23 +0100 | [diff] [blame] | 6066 | if (!crt) |
| 6067 | return NULL; |
| 6068 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6069 | return OBJ_nid2sn(OBJ_obj2nid(algorithm)); |
| 6070 | } |
| 6071 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6072 | /* used for ppv2 authority */ |
| 6073 | const char *ssl_sock_get_sni(struct connection *conn) |
| 6074 | { |
| 6075 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6076 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6077 | |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6078 | if (!ssl_sock_is_ssl(conn)) |
| 6079 | return NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6080 | return SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6081 | #else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6082 | return NULL; |
Emmanuel Hocdet | 253c3b7 | 2018-02-01 18:29:59 +0100 | [diff] [blame] | 6083 | #endif |
| 6084 | } |
| 6085 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6086 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6087 | const char *ssl_sock_get_cipher_name(struct connection *conn) |
| 6088 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6089 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6090 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6091 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6092 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6093 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6094 | return SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6095 | } |
| 6096 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6097 | /* used for logging/ppv2, may be changed for a sample fetch later */ |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6098 | const char *ssl_sock_get_proto_version(struct connection *conn) |
| 6099 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6100 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6101 | |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6102 | if (!ssl_sock_is_ssl(conn)) |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6103 | return NULL; |
Emmanuel Hocdet | 01da571 | 2017-10-13 16:59:49 +0200 | [diff] [blame] | 6104 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6105 | return SSL_get_version(ctx->ssl); |
Willy Tarreau | ffc3fcd | 2012-10-12 20:17:54 +0200 | [diff] [blame] | 6106 | } |
| 6107 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6108 | /* Extract a serial from a cert, and copy it to a chunk. |
| 6109 | * Returns 1 if serial is found and copied, 0 if no serial found and |
| 6110 | * -1 if output is not large enough. |
| 6111 | */ |
| 6112 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6113 | ssl_sock_get_serial(X509 *crt, struct buffer *out) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6114 | { |
| 6115 | ASN1_INTEGER *serial; |
| 6116 | |
| 6117 | serial = X509_get_serialNumber(crt); |
| 6118 | if (!serial) |
| 6119 | return 0; |
| 6120 | |
| 6121 | if (out->size < serial->length) |
| 6122 | return -1; |
| 6123 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6124 | memcpy(out->area, serial->data, serial->length); |
| 6125 | out->data = serial->length; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6126 | return 1; |
| 6127 | } |
| 6128 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6129 | /* Extract a cert to der, and copy it to a chunk. |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 6130 | * Returns 1 if the cert is found and copied, 0 on der conversion failure |
| 6131 | * and -1 if the output is not large enough. |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6132 | */ |
| 6133 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6134 | ssl_sock_crt2der(X509 *crt, struct buffer *out) |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6135 | { |
| 6136 | int len; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6137 | unsigned char *p = (unsigned char *) out->area;; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6138 | |
| 6139 | len =i2d_X509(crt, NULL); |
| 6140 | if (len <= 0) |
| 6141 | return 1; |
| 6142 | |
| 6143 | if (out->size < len) |
| 6144 | return -1; |
| 6145 | |
| 6146 | i2d_X509(crt,&p); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6147 | out->data = len; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6148 | return 1; |
| 6149 | } |
| 6150 | |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6151 | |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6152 | /* Copy Date in ASN1_UTCTIME format in struct buffer out. |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6153 | * Returns 1 if serial is found and copied, 0 if no valid time found |
| 6154 | * and -1 if output is not large enough. |
| 6155 | */ |
| 6156 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6157 | ssl_sock_get_time(ASN1_TIME *tm, struct buffer *out) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6158 | { |
| 6159 | if (tm->type == V_ASN1_GENERALIZEDTIME) { |
| 6160 | ASN1_GENERALIZEDTIME *gentm = (ASN1_GENERALIZEDTIME *)tm; |
| 6161 | |
| 6162 | if (gentm->length < 12) |
| 6163 | return 0; |
| 6164 | if (gentm->data[0] != 0x32 || gentm->data[1] != 0x30) |
| 6165 | return 0; |
| 6166 | if (out->size < gentm->length-2) |
| 6167 | return -1; |
| 6168 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6169 | memcpy(out->area, gentm->data+2, gentm->length-2); |
| 6170 | out->data = gentm->length-2; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6171 | return 1; |
| 6172 | } |
| 6173 | else if (tm->type == V_ASN1_UTCTIME) { |
| 6174 | ASN1_UTCTIME *utctm = (ASN1_UTCTIME *)tm; |
| 6175 | |
| 6176 | if (utctm->length < 10) |
| 6177 | return 0; |
| 6178 | if (utctm->data[0] >= 0x35) |
| 6179 | return 0; |
| 6180 | if (out->size < utctm->length) |
| 6181 | return -1; |
| 6182 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6183 | memcpy(out->area, utctm->data, utctm->length); |
| 6184 | out->data = utctm->length; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6185 | return 1; |
| 6186 | } |
| 6187 | |
| 6188 | return 0; |
| 6189 | } |
| 6190 | |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6191 | /* Extract an entry from a X509_NAME and copy its value to an output chunk. |
| 6192 | * Returns 1 if entry found, 0 if entry not found, or -1 if output not large enough. |
| 6193 | */ |
| 6194 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6195 | ssl_sock_get_dn_entry(X509_NAME *a, const struct buffer *entry, int pos, |
| 6196 | struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6197 | { |
| 6198 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6199 | ASN1_OBJECT *obj; |
| 6200 | ASN1_STRING *data; |
| 6201 | const unsigned char *data_ptr; |
| 6202 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6203 | int i, j, n; |
| 6204 | int cur = 0; |
| 6205 | const char *s; |
| 6206 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6207 | int name_count; |
| 6208 | |
| 6209 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6210 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6211 | out->data = 0; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6212 | for (i = 0; i < name_count; i++) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6213 | if (pos < 0) |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6214 | j = (name_count-1) - i; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6215 | else |
| 6216 | j = i; |
| 6217 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6218 | ne = X509_NAME_get_entry(a, j); |
| 6219 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6220 | data = X509_NAME_ENTRY_get_data(ne); |
| 6221 | data_ptr = ASN1_STRING_get0_data(data); |
| 6222 | data_len = ASN1_STRING_length(data); |
| 6223 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6224 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6225 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6226 | s = tmp; |
| 6227 | } |
| 6228 | |
| 6229 | if (chunk_strcasecmp(entry, s) != 0) |
| 6230 | continue; |
| 6231 | |
| 6232 | if (pos < 0) |
| 6233 | cur--; |
| 6234 | else |
| 6235 | cur++; |
| 6236 | |
| 6237 | if (cur != pos) |
| 6238 | continue; |
| 6239 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6240 | if (data_len > out->size) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6241 | return -1; |
| 6242 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6243 | memcpy(out->area, data_ptr, data_len); |
| 6244 | out->data = data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6245 | return 1; |
| 6246 | } |
| 6247 | |
| 6248 | return 0; |
| 6249 | |
| 6250 | } |
| 6251 | |
| 6252 | /* Extract and format full DN from a X509_NAME and copy result into a chunk |
| 6253 | * Returns 1 if dn entries exits, 0 if no dn entry found or -1 if output is not large enough. |
| 6254 | */ |
| 6255 | static int |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6256 | ssl_sock_get_dn_oneline(X509_NAME *a, struct buffer *out) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6257 | { |
| 6258 | X509_NAME_ENTRY *ne; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6259 | ASN1_OBJECT *obj; |
| 6260 | ASN1_STRING *data; |
| 6261 | const unsigned char *data_ptr; |
| 6262 | int data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6263 | int i, n, ln; |
| 6264 | int l = 0; |
| 6265 | const char *s; |
| 6266 | char *p; |
| 6267 | char tmp[128]; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6268 | int name_count; |
| 6269 | |
| 6270 | |
| 6271 | name_count = X509_NAME_entry_count(a); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6272 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6273 | out->data = 0; |
| 6274 | p = out->area; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6275 | for (i = 0; i < name_count; i++) { |
| 6276 | ne = X509_NAME_get_entry(a, i); |
| 6277 | obj = X509_NAME_ENTRY_get_object(ne); |
| 6278 | data = X509_NAME_ENTRY_get_data(ne); |
| 6279 | data_ptr = ASN1_STRING_get0_data(data); |
| 6280 | data_len = ASN1_STRING_length(data); |
| 6281 | n = OBJ_obj2nid(obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6282 | if ((n == NID_undef) || ((s = OBJ_nid2sn(n)) == NULL)) { |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6283 | i2t_ASN1_OBJECT(tmp, sizeof(tmp), obj); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6284 | s = tmp; |
| 6285 | } |
| 6286 | ln = strlen(s); |
| 6287 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6288 | l += 1 + ln + 1 + data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6289 | if (l > out->size) |
| 6290 | return -1; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6291 | out->data = l; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6292 | |
| 6293 | *(p++)='/'; |
| 6294 | memcpy(p, s, ln); |
| 6295 | p += ln; |
| 6296 | *(p++)='='; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6297 | memcpy(p, data_ptr, data_len); |
| 6298 | p += data_len; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6299 | } |
| 6300 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6301 | if (!out->data) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6302 | return 0; |
| 6303 | |
| 6304 | return 1; |
| 6305 | } |
| 6306 | |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6307 | void ssl_sock_set_alpn(struct connection *conn, const unsigned char *alpn, int len) |
| 6308 | { |
| 6309 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6310 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6311 | |
| 6312 | SSL_set_alpn_protos(ctx->ssl, alpn, len); |
Olivier Houchard | ab28a32 | 2018-12-21 19:45:40 +0100 | [diff] [blame] | 6313 | #endif |
| 6314 | } |
| 6315 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6316 | /* Sets advertised SNI for outgoing connections. Please set <hostname> to NULL |
| 6317 | * to disable SNI. |
| 6318 | */ |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6319 | void ssl_sock_set_servername(struct connection *conn, const char *hostname) |
| 6320 | { |
| 6321 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6322 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6323 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6324 | char *prev_name; |
| 6325 | |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6326 | if (!ssl_sock_is_ssl(conn)) |
| 6327 | return; |
| 6328 | |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6329 | /* if the SNI changes, we must destroy the reusable context so that a |
| 6330 | * new connection will present a new SNI. As an optimization we could |
| 6331 | * later imagine having a small cache of ssl_ctx to hold a few SNI per |
| 6332 | * server. |
| 6333 | */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6334 | prev_name = (char *)SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6335 | if ((!prev_name && hostname) || |
| 6336 | (prev_name && (!hostname || strcmp(hostname, prev_name) != 0))) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6337 | SSL_set_session(ctx->ssl, NULL); |
Willy Tarreau | 119a408 | 2016-12-22 21:58:38 +0100 | [diff] [blame] | 6338 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6339 | SSL_set_tlsext_host_name(ctx->ssl, hostname); |
Willy Tarreau | 6307641 | 2015-07-10 11:33:32 +0200 | [diff] [blame] | 6340 | #endif |
| 6341 | } |
| 6342 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6343 | /* Extract peer certificate's common name into the chunk dest |
| 6344 | * Returns |
| 6345 | * the len of the extracted common name |
| 6346 | * or 0 if no CN found in DN |
| 6347 | * or -1 on error case (i.e. no peer certificate) |
| 6348 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6349 | int ssl_sock_get_remote_common_name(struct connection *conn, |
| 6350 | struct buffer *dest) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6351 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6352 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6353 | X509 *crt = NULL; |
| 6354 | X509_NAME *name; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6355 | const char find_cn[] = "CN"; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6356 | const struct buffer find_cn_chunk = { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6357 | .area = (char *)&find_cn, |
| 6358 | .data = sizeof(find_cn)-1 |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6359 | }; |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6360 | int result = -1; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6361 | |
| 6362 | if (!ssl_sock_is_ssl(conn)) |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6363 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6364 | |
| 6365 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6366 | crt = SSL_get_peer_certificate(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6367 | if (!crt) |
| 6368 | goto out; |
| 6369 | |
| 6370 | name = X509_get_subject_name(crt); |
| 6371 | if (!name) |
| 6372 | goto out; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6373 | |
Emeric Brun | 0abf836 | 2014-06-24 18:26:41 +0200 | [diff] [blame] | 6374 | result = ssl_sock_get_dn_entry(name, &find_cn_chunk, 1, dest); |
| 6375 | out: |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6376 | if (crt) |
| 6377 | X509_free(crt); |
| 6378 | |
| 6379 | return result; |
| 6380 | } |
| 6381 | |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6382 | /* returns 1 if client passed a certificate for this session, 0 if not */ |
| 6383 | int ssl_sock_get_cert_used_sess(struct connection *conn) |
| 6384 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6385 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6386 | X509 *crt = NULL; |
| 6387 | |
| 6388 | if (!ssl_sock_is_ssl(conn)) |
| 6389 | return 0; |
| 6390 | |
| 6391 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6392 | crt = SSL_get_peer_certificate(ctx->ssl); |
Dave McCowan | 328fb58 | 2014-07-30 10:39:13 -0400 | [diff] [blame] | 6393 | if (!crt) |
| 6394 | return 0; |
| 6395 | |
| 6396 | X509_free(crt); |
| 6397 | return 1; |
| 6398 | } |
| 6399 | |
| 6400 | /* returns 1 if client passed a certificate for this connection, 0 if not */ |
| 6401 | int ssl_sock_get_cert_used_conn(struct connection *conn) |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6402 | { |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6403 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6404 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6405 | if (!ssl_sock_is_ssl(conn)) |
| 6406 | return 0; |
| 6407 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6408 | return SSL_SOCK_ST_FL_VERIFY_DONE & ctx->xprt_st ? 1 : 0; |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | /* returns result from SSL verify */ |
| 6412 | unsigned int ssl_sock_get_verify_result(struct connection *conn) |
| 6413 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6414 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 6415 | |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6416 | if (!ssl_sock_is_ssl(conn)) |
| 6417 | return (unsigned int)X509_V_ERR_APPLICATION_VERIFICATION; |
| 6418 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6419 | return (unsigned int)SSL_get_verify_result(ctx->ssl); |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 6420 | } |
| 6421 | |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6422 | /* Returns the application layer protocol name in <str> and <len> when known. |
| 6423 | * Zero is returned if the protocol name was not found, otherwise non-zero is |
| 6424 | * returned. The string is allocated in the SSL context and doesn't have to be |
| 6425 | * freed by the caller. NPN is also checked if available since older versions |
| 6426 | * of openssl (1.0.1) which are more common in field only support this one. |
| 6427 | */ |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6428 | 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] | 6429 | { |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6430 | #if defined(TLSEXT_TYPE_application_layer_protocol_negotiation) || \ |
| 6431 | defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6432 | struct ssl_sock_ctx *ctx = xprt_ctx; |
| 6433 | if (!ctx) |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6434 | return 0; |
| 6435 | |
| 6436 | *str = NULL; |
| 6437 | |
| 6438 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6439 | SSL_get0_alpn_selected(ctx->ssl, (const unsigned char **)str, (unsigned *)len); |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6440 | if (*str) |
| 6441 | return 1; |
| 6442 | #endif |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 6443 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6444 | 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] | 6445 | if (*str) |
| 6446 | return 1; |
| 6447 | #endif |
Olivier Houchard | e179d0e | 2019-03-21 18:27:17 +0100 | [diff] [blame] | 6448 | #endif |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 6449 | return 0; |
| 6450 | } |
| 6451 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 6452 | /***** Below are some sample fetching functions for ACL/patterns *****/ |
| 6453 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6454 | static int |
| 6455 | smp_fetch_ssl_fc_has_early(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 6456 | { |
| 6457 | struct connection *conn; |
| 6458 | |
| 6459 | conn = objt_conn(smp->sess->origin); |
| 6460 | if (!conn || conn->xprt != &ssl_sock) |
| 6461 | return 0; |
| 6462 | |
| 6463 | smp->flags = 0; |
| 6464 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 25ae45a | 2017-11-29 19:51:19 +0100 | [diff] [blame] | 6465 | smp->data.u.sint = ((conn->flags & CO_FL_EARLY_DATA) && |
| 6466 | (conn->flags & (CO_FL_EARLY_SSL_HS | CO_FL_HANDSHAKE))) ? 1 : 0; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 6467 | |
| 6468 | return 1; |
| 6469 | } |
| 6470 | |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6471 | /* boolean, returns true if client cert was present */ |
| 6472 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6473 | 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] | 6474 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6475 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6476 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6477 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6478 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6479 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6480 | return 0; |
| 6481 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6482 | ctx = conn->xprt_ctx; |
| 6483 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6484 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6485 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6486 | return 0; |
| 6487 | } |
| 6488 | |
| 6489 | smp->flags = 0; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6490 | smp->data.type = SMP_T_BOOL; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 6491 | 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] | 6492 | |
| 6493 | return 1; |
| 6494 | } |
| 6495 | |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6496 | /* binary, returns a certificate in a binary chunk (der/raw). |
| 6497 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6498 | * should be use. |
| 6499 | */ |
| 6500 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6501 | 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] | 6502 | { |
| 6503 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
| 6504 | X509 *crt = NULL; |
| 6505 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6506 | struct buffer *smp_trash; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6507 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6508 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6509 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6510 | conn = objt_conn(smp->sess->origin); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6511 | if (!conn || conn->xprt != &ssl_sock) |
| 6512 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6513 | ctx = conn->xprt_ctx; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6514 | |
| 6515 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 6516 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6517 | return 0; |
| 6518 | } |
| 6519 | |
| 6520 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6521 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6522 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6523 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6524 | |
| 6525 | if (!crt) |
| 6526 | goto out; |
| 6527 | |
| 6528 | smp_trash = get_trash_chunk(); |
| 6529 | if (ssl_sock_crt2der(crt, smp_trash) <= 0) |
| 6530 | goto out; |
| 6531 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6532 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6533 | smp->data.type = SMP_T_BIN; |
Emeric Brun | 43e7958 | 2014-10-29 19:03:26 +0100 | [diff] [blame] | 6534 | ret = 1; |
| 6535 | out: |
| 6536 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6537 | if (cert_peer && crt) |
| 6538 | X509_free(crt); |
| 6539 | return ret; |
| 6540 | } |
| 6541 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6542 | /* binary, returns serial of certificate in a binary chunk. |
| 6543 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6544 | * should be use. |
| 6545 | */ |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6546 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6547 | 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] | 6548 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6549 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6550 | X509 *crt = NULL; |
| 6551 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6552 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6553 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6554 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6555 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6556 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6557 | if (!conn || conn->xprt != &ssl_sock) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6558 | return 0; |
| 6559 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6560 | ctx = conn->xprt_ctx; |
| 6561 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6562 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6563 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6564 | return 0; |
| 6565 | } |
| 6566 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6567 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6568 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6569 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6570 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6571 | |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6572 | if (!crt) |
| 6573 | goto out; |
| 6574 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6575 | smp_trash = get_trash_chunk(); |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6576 | if (ssl_sock_get_serial(crt, smp_trash) <= 0) |
| 6577 | goto out; |
| 6578 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6579 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6580 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6581 | ret = 1; |
| 6582 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6583 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6584 | if (cert_peer && crt) |
Willy Tarreau | 8d59840 | 2012-10-22 17:58:39 +0200 | [diff] [blame] | 6585 | X509_free(crt); |
| 6586 | return ret; |
| 6587 | } |
Emeric Brun | e64aef1 | 2012-09-21 13:15:06 +0200 | [diff] [blame] | 6588 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6589 | /* binary, returns the client certificate's SHA-1 fingerprint (SHA-1 hash of DER-encoded certificate) in a binary chunk. |
| 6590 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6591 | * should be use. |
| 6592 | */ |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6593 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6594 | 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] | 6595 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6596 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6597 | X509 *crt = NULL; |
| 6598 | const EVP_MD *digest; |
| 6599 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6600 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6601 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6602 | struct ssl_sock_ctx *ctx; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6603 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6604 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6605 | if (!conn || conn->xprt != &ssl_sock) |
| 6606 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6607 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6608 | |
| 6609 | if (!(conn->flags & CO_FL_CONNECTED)) { |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6610 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6611 | return 0; |
| 6612 | } |
| 6613 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6614 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6615 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6616 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6617 | crt = SSL_get_certificate(ctx->ssl); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6618 | if (!crt) |
| 6619 | goto out; |
| 6620 | |
| 6621 | smp_trash = get_trash_chunk(); |
| 6622 | digest = EVP_sha1(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6623 | X509_digest(crt, digest, (unsigned char *) smp_trash->area, |
| 6624 | (unsigned int *)&smp_trash->data); |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6625 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6626 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6627 | smp->data.type = SMP_T_BIN; |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6628 | ret = 1; |
| 6629 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6630 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6631 | if (cert_peer && crt) |
James Voth | a051b4a | 2013-05-14 20:37:59 +0200 | [diff] [blame] | 6632 | X509_free(crt); |
| 6633 | return ret; |
| 6634 | } |
| 6635 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6636 | /* string, returns certificate's notafter date in ASN1_UTCTIME format. |
| 6637 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6638 | * should be use. |
| 6639 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6640 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6641 | 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] | 6642 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6643 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6644 | X509 *crt = NULL; |
| 6645 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6646 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6647 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6648 | struct ssl_sock_ctx *ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6649 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6650 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6651 | if (!conn || conn->xprt != &ssl_sock) |
| 6652 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6653 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6654 | |
| 6655 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6656 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6657 | return 0; |
| 6658 | } |
| 6659 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6660 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6661 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6662 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6663 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6664 | if (!crt) |
| 6665 | goto out; |
| 6666 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6667 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6668 | if (ssl_sock_get_time(X509_getm_notAfter(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6669 | goto out; |
| 6670 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6671 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6672 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6673 | ret = 1; |
| 6674 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6675 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6676 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6677 | X509_free(crt); |
| 6678 | return ret; |
| 6679 | } |
| 6680 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6681 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's issuer |
| 6682 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6683 | * should be use. |
| 6684 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6685 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6686 | 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] | 6687 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6688 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6689 | X509 *crt = NULL; |
| 6690 | X509_NAME *name; |
| 6691 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6692 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6693 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6694 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6695 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6696 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6697 | if (!conn || conn->xprt != &ssl_sock) |
| 6698 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6699 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6700 | |
| 6701 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6702 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6703 | return 0; |
| 6704 | } |
| 6705 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6706 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6707 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6708 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6709 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6710 | if (!crt) |
| 6711 | goto out; |
| 6712 | |
| 6713 | name = X509_get_issuer_name(crt); |
| 6714 | if (!name) |
| 6715 | goto out; |
| 6716 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6717 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6718 | if (args && args[0].type == ARGT_STR) { |
| 6719 | int pos = 1; |
| 6720 | |
| 6721 | if (args[1].type == ARGT_SINT) |
| 6722 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6723 | |
| 6724 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6725 | goto out; |
| 6726 | } |
| 6727 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6728 | goto out; |
| 6729 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6730 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6731 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6732 | ret = 1; |
| 6733 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6734 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6735 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6736 | X509_free(crt); |
| 6737 | return ret; |
| 6738 | } |
| 6739 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6740 | /* string, returns notbefore date in ASN1_UTCTIME format. |
| 6741 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6742 | * should be use. |
| 6743 | */ |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6744 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6745 | 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] | 6746 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6747 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6748 | X509 *crt = NULL; |
| 6749 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6750 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6751 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6752 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6753 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6754 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6755 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6756 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6757 | ctx = conn->xprt_ctx; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6758 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6759 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6760 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6761 | return 0; |
| 6762 | } |
| 6763 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6764 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6765 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6766 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6767 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6768 | if (!crt) |
| 6769 | goto out; |
| 6770 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6771 | smp_trash = get_trash_chunk(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 6772 | if (ssl_sock_get_time(X509_getm_notBefore(crt), smp_trash) <= 0) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6773 | goto out; |
| 6774 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6775 | smp->data.u.str = *smp_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6776 | smp->data.type = SMP_T_STR; |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6777 | ret = 1; |
| 6778 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6779 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6780 | if (cert_peer && crt) |
Emeric Brun | ce5ad80 | 2012-10-22 14:11:22 +0200 | [diff] [blame] | 6781 | X509_free(crt); |
| 6782 | return ret; |
| 6783 | } |
| 6784 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6785 | /* string, returns a string of a formatted full dn \C=..\O=..\OU=.. \CN=.. of certificate's subject |
| 6786 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6787 | * should be use. |
| 6788 | */ |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6789 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6790 | 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] | 6791 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6792 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6793 | X509 *crt = NULL; |
| 6794 | X509_NAME *name; |
| 6795 | int ret = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6796 | struct buffer *smp_trash; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6797 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6798 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6799 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6800 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6801 | if (!conn || conn->xprt != &ssl_sock) |
| 6802 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6803 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6804 | |
| 6805 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6806 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6807 | return 0; |
| 6808 | } |
| 6809 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6810 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6811 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6812 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6813 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6814 | if (!crt) |
| 6815 | goto out; |
| 6816 | |
| 6817 | name = X509_get_subject_name(crt); |
| 6818 | if (!name) |
| 6819 | goto out; |
| 6820 | |
Willy Tarreau | 47ca545 | 2012-12-23 20:22:19 +0100 | [diff] [blame] | 6821 | smp_trash = get_trash_chunk(); |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6822 | if (args && args[0].type == ARGT_STR) { |
| 6823 | int pos = 1; |
| 6824 | |
| 6825 | if (args[1].type == ARGT_SINT) |
| 6826 | pos = args[1].data.sint; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6827 | |
| 6828 | if (ssl_sock_get_dn_entry(name, &args[0].data.str, pos, smp_trash) <= 0) |
| 6829 | goto out; |
| 6830 | } |
| 6831 | else if (ssl_sock_get_dn_oneline(name, smp_trash) <= 0) |
| 6832 | goto out; |
| 6833 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6834 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6835 | smp->data.u.str = *smp_trash; |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6836 | ret = 1; |
| 6837 | out: |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6838 | /* SSL_get_peer_certificate, it increase X509 * ref count */ |
| 6839 | if (cert_peer && crt) |
Emeric Brun | 8785589 | 2012-10-17 17:39:35 +0200 | [diff] [blame] | 6840 | X509_free(crt); |
| 6841 | return ret; |
| 6842 | } |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6843 | |
| 6844 | /* integer, returns true if current session use a client certificate */ |
| 6845 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6846 | 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] | 6847 | { |
| 6848 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6849 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6850 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6851 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6852 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6853 | if (!conn || conn->xprt != &ssl_sock) |
| 6854 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6855 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6856 | |
| 6857 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6858 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6859 | return 0; |
| 6860 | } |
| 6861 | |
| 6862 | /* SSL_get_peer_certificate returns a ptr on allocated X509 struct */ |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6863 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6864 | if (crt) { |
| 6865 | X509_free(crt); |
| 6866 | } |
| 6867 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6868 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6869 | smp->data.u.sint = (crt != NULL); |
Emeric Brun | 9143d37 | 2012-12-20 15:44:16 +0100 | [diff] [blame] | 6870 | return 1; |
| 6871 | } |
| 6872 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6873 | /* integer, returns the certificate version |
| 6874 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6875 | * should be use. |
| 6876 | */ |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6877 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6878 | 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] | 6879 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6880 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6881 | X509 *crt; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6882 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6883 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6884 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6885 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6886 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6887 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6888 | ctx = conn->xprt_ctx; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6889 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6890 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6891 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6892 | return 0; |
| 6893 | } |
| 6894 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6895 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6896 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6897 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6898 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6899 | if (!crt) |
| 6900 | return 0; |
| 6901 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 6902 | smp->data.u.sint = (unsigned int)(1 + X509_get_version(crt)); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6903 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6904 | if (cert_peer) |
| 6905 | X509_free(crt); |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6906 | smp->data.type = SMP_T_SINT; |
Emeric Brun | a7359fd | 2012-10-17 15:03:11 +0200 | [diff] [blame] | 6907 | |
| 6908 | return 1; |
| 6909 | } |
| 6910 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6911 | /* string, returns the certificate's signature algorithm. |
| 6912 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6913 | * should be use. |
| 6914 | */ |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6915 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6916 | 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] | 6917 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6918 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6919 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6920 | __OPENSSL_110_CONST__ ASN1_OBJECT *algorithm; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6921 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6922 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6923 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6924 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6925 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6926 | if (!conn || conn->xprt != &ssl_sock) |
| 6927 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6928 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6929 | |
| 6930 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6931 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6932 | return 0; |
| 6933 | } |
| 6934 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6935 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6936 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6937 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6938 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6939 | if (!crt) |
| 6940 | return 0; |
| 6941 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6942 | X509_ALGOR_get0(&algorithm, NULL, NULL, X509_get0_tbs_sigalg(crt)); |
| 6943 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6944 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6945 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 6946 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6947 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6948 | if (cert_peer) |
| 6949 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6950 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 6951 | } |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6952 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 6953 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 6954 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6955 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6956 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 6957 | if (cert_peer) |
| 6958 | X509_free(crt); |
Emeric Brun | 7f56e74 | 2012-10-19 18:15:40 +0200 | [diff] [blame] | 6959 | |
| 6960 | return 1; |
| 6961 | } |
| 6962 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6963 | /* string, returns the certificate's key algorithm. |
| 6964 | * The 5th keyword char is used to know if SSL_get_certificate or SSL_get_peer_certificate |
| 6965 | * should be use. |
| 6966 | */ |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6967 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6968 | 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] | 6969 | { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6970 | int cert_peer = (kw[4] == 'c') ? 1 : 0; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6971 | X509 *crt; |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6972 | ASN1_OBJECT *algorithm; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6973 | int nid; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6974 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6975 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6976 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6977 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6978 | if (!conn || conn->xprt != &ssl_sock) |
| 6979 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6980 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 6981 | |
| 6982 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6983 | smp->flags |= SMP_F_MAY_CHANGE; |
| 6984 | return 0; |
| 6985 | } |
| 6986 | |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6987 | if (cert_peer) |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6988 | crt = SSL_get_peer_certificate(ctx->ssl); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6989 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 6990 | crt = SSL_get_certificate(ctx->ssl); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6991 | if (!crt) |
| 6992 | return 0; |
| 6993 | |
Dirkjan Bussink | 1866d6d | 2016-08-29 13:26:37 +0200 | [diff] [blame] | 6994 | X509_PUBKEY_get0_param(&algorithm, NULL, NULL, NULL, X509_get_X509_PUBKEY(crt)); |
| 6995 | nid = OBJ_obj2nid(algorithm); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 6996 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 6997 | smp->data.u.str.area = (char *)OBJ_nid2sn(nid); |
| 6998 | if (!smp->data.u.str.area) { |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 6999 | /* SSL_get_peer_certificate increase X509 * ref count */ |
| 7000 | if (cert_peer) |
| 7001 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7002 | return 0; |
Emeric Brun | 9bf3ba2 | 2013-10-07 14:31:44 +0200 | [diff] [blame] | 7003 | } |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7004 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7005 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7006 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7007 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | ba841a1 | 2014-04-30 17:05:08 +0200 | [diff] [blame] | 7008 | if (cert_peer) |
| 7009 | X509_free(crt); |
Emeric Brun | 521a011 | 2012-10-22 12:22:55 +0200 | [diff] [blame] | 7010 | |
| 7011 | return 1; |
| 7012 | } |
| 7013 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7014 | /* boolean, returns true if front conn. transport layer is SSL. |
| 7015 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7016 | * char is 'b'. |
| 7017 | */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7018 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7019 | 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] | 7020 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7021 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7022 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7023 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7024 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7025 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7026 | return 1; |
| 7027 | } |
| 7028 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7029 | /* boolean, returns true if client present a SNI */ |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7030 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7031 | 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] | 7032 | { |
| 7033 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7034 | struct connection *conn = objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7035 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7036 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7037 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7038 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7039 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7040 | SSL_get_servername(ctx->ssl, TLSEXT_NAMETYPE_host_name) != NULL; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7041 | return 1; |
| 7042 | #else |
| 7043 | return 0; |
| 7044 | #endif |
| 7045 | } |
| 7046 | |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7047 | /* boolean, returns true if client session has been resumed. |
| 7048 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7049 | * char is 'b'. |
| 7050 | */ |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7051 | static int |
| 7052 | smp_fetch_ssl_fc_is_resumed(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7053 | { |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7054 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7055 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7056 | struct ssl_sock_ctx *ctx = conn ? conn->xprt_ctx : NULL; |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 7057 | |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7058 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7059 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7060 | smp->data.u.sint = (conn && conn->xprt == &ssl_sock) && |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7061 | conn->xprt_ctx && |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7062 | SSL_session_reused(ctx->ssl); |
Nenad Merdanovic | 26ea822 | 2015-05-18 02:28:57 +0200 | [diff] [blame] | 7063 | return 1; |
| 7064 | } |
| 7065 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7066 | /* string, returns the used cipher if front conn. transport layer is SSL. |
| 7067 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7068 | * char is 'b'. |
| 7069 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7070 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7071 | 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] | 7072 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7073 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7074 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7075 | struct ssl_sock_ctx *ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7076 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7077 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7078 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7079 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7080 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7081 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7082 | smp->data.u.str.area = (char *)SSL_get_cipher_name(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7083 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7084 | return 0; |
| 7085 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7086 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7087 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7088 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7089 | |
| 7090 | return 1; |
| 7091 | } |
| 7092 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7093 | /* integer, returns the algoritm's keysize if front conn. transport layer |
| 7094 | * is SSL. |
| 7095 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7096 | * char is 'b'. |
| 7097 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7098 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7099 | 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] | 7100 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7101 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7102 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7103 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7104 | int sint; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7105 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7106 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7107 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7108 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7109 | ctx = conn->xprt_ctx; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7110 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7111 | if (!SSL_get_cipher_bits(ctx->ssl, &sint)) |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7112 | return 0; |
| 7113 | |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7114 | smp->data.u.sint = sint; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7115 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7116 | |
| 7117 | return 1; |
| 7118 | } |
| 7119 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7120 | /* integer, returns the used keysize if front conn. transport layer is SSL. |
| 7121 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7122 | * char is 'b'. |
| 7123 | */ |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7124 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7125 | 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] | 7126 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7127 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7128 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7129 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7130 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7131 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7132 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7133 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7134 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7135 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7136 | 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] | 7137 | if (!smp->data.u.sint) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7138 | return 0; |
| 7139 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7140 | smp->data.type = SMP_T_SINT; |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7141 | |
| 7142 | return 1; |
| 7143 | } |
| 7144 | |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7145 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7146 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7147 | 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] | 7148 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7149 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7150 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7151 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7152 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7153 | smp->data.type = SMP_T_STR; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7154 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 7155 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 7156 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7157 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7158 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7159 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7160 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7161 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7162 | SSL_get0_next_proto_negotiated(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7163 | (const unsigned char **)&smp->data.u.str.area, |
| 7164 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7165 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7166 | if (!smp->data.u.str.area) |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7167 | return 0; |
| 7168 | |
| 7169 | return 1; |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7170 | } |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7171 | #endif |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7172 | |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7173 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7174 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7175 | 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] | 7176 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7177 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7178 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7179 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7180 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7181 | smp->data.type = SMP_T_STR; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7182 | |
Olivier Houchard | 6b77f49 | 2018-11-22 18:18:29 +0100 | [diff] [blame] | 7183 | conn = (kw[4] != 'b' ) ? objt_conn(smp->sess->origin) : |
| 7184 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7185 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7186 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7187 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7188 | ctx = conn->xprt_ctx; |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7189 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7190 | smp->data.u.str.area = NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7191 | SSL_get0_alpn_selected(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7192 | (const unsigned char **)&smp->data.u.str.area, |
| 7193 | (unsigned *)&smp->data.u.str.data); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7194 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7195 | if (!smp->data.u.str.area) |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7196 | return 0; |
| 7197 | |
| 7198 | return 1; |
| 7199 | } |
| 7200 | #endif |
| 7201 | |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 7202 | /* string, returns the used protocol if front conn. transport layer is SSL. |
| 7203 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7204 | * char is 'b'. |
| 7205 | */ |
Willy Tarreau | a33c654 | 2012-10-15 13:19:06 +0200 | [diff] [blame] | 7206 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7207 | 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] | 7208 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7209 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7210 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7211 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 7212 | |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7213 | smp->flags = 0; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7214 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7215 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7216 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7217 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7218 | smp->data.u.str.area = (char *)SSL_get_version(ctx->ssl); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7219 | if (!smp->data.u.str.area) |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7220 | return 0; |
| 7221 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7222 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7223 | smp->flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7224 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7225 | |
| 7226 | return 1; |
| 7227 | } |
| 7228 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7229 | /* 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] | 7230 | * This function is also usable on backend conn if the fetch keyword 5th |
| 7231 | * char is 'b'. |
| 7232 | */ |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7233 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 589fcad | 2012-10-16 14:13:26 +0200 | [diff] [blame] | 7234 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7235 | 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] | 7236 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7237 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7238 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
Willy Tarreau | e237fe1 | 2016-03-10 17:05:28 +0100 | [diff] [blame] | 7239 | SSL_SESSION *ssl_sess; |
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 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7242 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7243 | smp->data.type = SMP_T_BIN; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7244 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7245 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7246 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7247 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7248 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7249 | ssl_sess = SSL_get_session(ctx->ssl); |
Willy Tarreau | 192252e | 2015-04-04 01:47:55 +0200 | [diff] [blame] | 7250 | if (!ssl_sess) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7251 | return 0; |
| 7252 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7253 | smp->data.u.str.area = (char *)SSL_SESSION_get_id(ssl_sess, |
| 7254 | (unsigned int *)&smp->data.u.str.data); |
| 7255 | if (!smp->data.u.str.area || !smp->data.u.str.data) |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7256 | return 0; |
| 7257 | |
| 7258 | return 1; |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7259 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7260 | #endif |
| 7261 | |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7262 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7263 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7264 | static int |
| 7265 | smp_fetch_ssl_fc_session_key(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7266 | { |
| 7267 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7268 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
| 7269 | SSL_SESSION *ssl_sess; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7270 | struct buffer *data; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7271 | struct ssl_sock_ctx *ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7272 | |
| 7273 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7274 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7275 | ctx = conn->xprt_ctx; |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7276 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7277 | ssl_sess = SSL_get_session(ctx->ssl); |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7278 | if (!ssl_sess) |
| 7279 | return 0; |
| 7280 | |
| 7281 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7282 | data->data = SSL_SESSION_get_master_key(ssl_sess, |
| 7283 | (unsigned char *) data->area, |
| 7284 | data->size); |
| 7285 | if (!data->data) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 7286 | return 0; |
| 7287 | |
| 7288 | smp->flags = 0; |
| 7289 | smp->data.type = SMP_T_BIN; |
| 7290 | smp->data.u.str = *data; |
| 7291 | |
| 7292 | return 1; |
| 7293 | } |
| 7294 | #endif |
| 7295 | |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7296 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Emeric Brun | fe68f68 | 2012-10-16 14:59:28 +0200 | [diff] [blame] | 7297 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7298 | 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] | 7299 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7300 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7301 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7302 | |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 7303 | smp->flags = SMP_F_CONST; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7304 | smp->data.type = SMP_T_STR; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7305 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7306 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7307 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7308 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7309 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7310 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7311 | 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] | 7312 | if (!smp->data.u.str.area) |
Willy Tarreau | 3e394c9 | 2012-09-14 23:56:58 +0200 | [diff] [blame] | 7313 | return 0; |
| 7314 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7315 | smp->data.u.str.data = strlen(smp->data.u.str.area); |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7316 | return 1; |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7317 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7318 | #endif |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 7319 | |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7320 | static int |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7321 | smp_fetch_ssl_fc_cl_bin(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7322 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7323 | struct connection *conn; |
| 7324 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7325 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7326 | |
| 7327 | conn = objt_conn(smp->sess->origin); |
| 7328 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7329 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7330 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7331 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7332 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7333 | if (!capture) |
| 7334 | return 0; |
| 7335 | |
| 7336 | smp->flags = SMP_F_CONST; |
| 7337 | smp->data.type = SMP_T_BIN; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7338 | smp->data.u.str.area = capture->ciphersuite; |
| 7339 | smp->data.u.str.data = capture->ciphersuite_len; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7340 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7341 | } |
| 7342 | |
| 7343 | static int |
| 7344 | smp_fetch_ssl_fc_cl_hex(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7345 | { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7346 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7347 | |
| 7348 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7349 | return 0; |
| 7350 | |
| 7351 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7352 | 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] | 7353 | smp->data.type = SMP_T_BIN; |
| 7354 | smp->data.u.str = *data; |
| 7355 | return 1; |
| 7356 | } |
| 7357 | |
| 7358 | static int |
| 7359 | smp_fetch_ssl_fc_cl_xxh64(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7360 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7361 | struct connection *conn; |
| 7362 | struct ssl_capture *capture; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7363 | struct ssl_sock_ctx *ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7364 | |
| 7365 | conn = objt_conn(smp->sess->origin); |
| 7366 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7367 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7368 | ctx = conn->xprt_ctx; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7369 | |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7370 | capture = SSL_get_ex_data(ctx->ssl, ssl_capture_ptr_index); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7371 | if (!capture) |
| 7372 | return 0; |
| 7373 | |
| 7374 | smp->data.type = SMP_T_SINT; |
| 7375 | smp->data.u.sint = capture->xxh64; |
| 7376 | return 1; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7377 | } |
| 7378 | |
| 7379 | static int |
| 7380 | smp_fetch_ssl_fc_cl_str(const struct arg *args, struct sample *smp, const char *kw, void *private) |
| 7381 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7382 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL) |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7383 | struct buffer *data; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7384 | int i; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7385 | |
| 7386 | if (!smp_fetch_ssl_fc_cl_bin(args, smp, kw, private)) |
| 7387 | return 0; |
| 7388 | |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7389 | data = get_trash_chunk(); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7390 | for (i = 0; i + 1 < smp->data.u.str.data; i += 2) { |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7391 | const char *str; |
| 7392 | const SSL_CIPHER *cipher; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7393 | 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] | 7394 | uint16_t id = (bin[0] << 8) | bin[1]; |
| 7395 | #if defined(OPENSSL_IS_BORINGSSL) |
| 7396 | cipher = SSL_get_cipher_by_value(id); |
| 7397 | #else |
Willy Tarreau | b729077 | 2018-10-15 11:01:59 +0200 | [diff] [blame] | 7398 | struct connection *conn = __objt_conn(smp->sess->origin); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7399 | struct ssl_sock_ctx *ctx = conn->xprt_ctx; |
| 7400 | cipher = SSL_CIPHER_find(ctx->ssl, bin); |
Emmanuel Hocdet | ddcde19 | 2017-09-01 17:32:08 +0200 | [diff] [blame] | 7401 | #endif |
| 7402 | str = SSL_CIPHER_get_name(cipher); |
| 7403 | if (!str || strcmp(str, "(NONE)") == 0) |
| 7404 | chunk_appendf(data, "%sUNKNOWN(%04x)", i == 0 ? "" : ",", id); |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7405 | else |
| 7406 | chunk_appendf(data, "%s%s", i == 0 ? "" : ",", str); |
| 7407 | } |
| 7408 | smp->data.type = SMP_T_STR; |
| 7409 | smp->data.u.str = *data; |
| 7410 | return 1; |
| 7411 | #else |
| 7412 | return smp_fetch_ssl_fc_cl_xxh64(args, smp, kw, private); |
| 7413 | #endif |
| 7414 | } |
| 7415 | |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7416 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 7417 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7418 | 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] | 7419 | { |
Emeric Brun | eb8def9 | 2018-02-19 15:59:48 +0100 | [diff] [blame] | 7420 | struct connection *conn = (kw[4] != 'b') ? objt_conn(smp->sess->origin) : |
| 7421 | smp->strm ? cs_conn(objt_cs(smp->strm->si[1].end)) : NULL; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7422 | int finished_len; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 7423 | struct buffer *finished_trash; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7424 | struct ssl_sock_ctx *ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7425 | |
| 7426 | smp->flags = 0; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7427 | if (!conn || !conn->xprt_ctx || conn->xprt != &ssl_sock) |
| 7428 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7429 | ctx = conn->xprt_ctx; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7430 | |
| 7431 | if (!(conn->flags & CO_FL_CONNECTED)) { |
| 7432 | smp->flags |= SMP_F_MAY_CHANGE; |
| 7433 | return 0; |
| 7434 | } |
| 7435 | |
| 7436 | finished_trash = get_trash_chunk(); |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7437 | if (!SSL_session_reused(ctx->ssl)) |
| 7438 | finished_len = SSL_get_peer_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7439 | finished_trash->area, |
| 7440 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7441 | else |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7442 | finished_len = SSL_get_finished(ctx->ssl, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7443 | finished_trash->area, |
| 7444 | finished_trash->size); |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7445 | |
| 7446 | if (!finished_len) |
| 7447 | return 0; |
| 7448 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7449 | finished_trash->data = finished_len; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 7450 | smp->data.u.str = *finished_trash; |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7451 | smp->data.type = SMP_T_BIN; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7452 | |
| 7453 | return 1; |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7454 | } |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 7455 | #endif |
David S | c1ad52e | 2014-04-08 18:48:47 -0400 | [diff] [blame] | 7456 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7457 | /* 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] | 7458 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7459 | 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] | 7460 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7461 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7462 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7463 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7464 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7465 | if (!conn || conn->xprt != &ssl_sock) |
| 7466 | return 0; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7467 | ctx = conn->xprt_ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7468 | |
| 7469 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7470 | smp->flags = SMP_F_MAY_CHANGE; |
| 7471 | return 0; |
| 7472 | } |
| 7473 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7474 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7475 | 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] | 7476 | smp->flags = 0; |
| 7477 | |
| 7478 | return 1; |
| 7479 | } |
| 7480 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7481 | /* 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] | 7482 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7483 | 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] | 7484 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7485 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7486 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7487 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7488 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7489 | if (!conn || conn->xprt != &ssl_sock) |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7490 | return 0; |
| 7491 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7492 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7493 | smp->flags = SMP_F_MAY_CHANGE; |
| 7494 | return 0; |
| 7495 | } |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7496 | ctx = conn->xprt_ctx; |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7497 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7498 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7499 | 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] | 7500 | smp->flags = 0; |
| 7501 | |
| 7502 | return 1; |
| 7503 | } |
| 7504 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7505 | /* integer, returns the first verify error on client certificate */ |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7506 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7507 | 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] | 7508 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7509 | struct connection *conn; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7510 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7511 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7512 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7513 | if (!conn || conn->xprt != &ssl_sock) |
| 7514 | return 0; |
| 7515 | |
| 7516 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | f282a81 | 2012-09-21 15:27:54 +0200 | [diff] [blame] | 7517 | smp->flags = SMP_F_MAY_CHANGE; |
| 7518 | return 0; |
| 7519 | } |
| 7520 | |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7521 | ctx = conn->xprt_ctx; |
| 7522 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7523 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 7b5fd1e | 2019-02-28 18:10:45 +0100 | [diff] [blame] | 7524 | 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] | 7525 | smp->flags = 0; |
| 7526 | |
| 7527 | return 1; |
| 7528 | } |
| 7529 | |
Emeric Brun | 2525b6b | 2012-10-18 15:59:43 +0200 | [diff] [blame] | 7530 | /* integer, returns the verify result on client cert */ |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7531 | static int |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 7532 | 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] | 7533 | { |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7534 | struct connection *conn; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7535 | struct ssl_sock_ctx *ctx; |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7536 | |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 7537 | conn = objt_conn(smp->sess->origin); |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7538 | if (!conn || conn->xprt != &ssl_sock) |
| 7539 | return 0; |
| 7540 | |
| 7541 | if (!(conn->flags & CO_FL_CONNECTED)) { |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7542 | smp->flags = SMP_F_MAY_CHANGE; |
| 7543 | return 0; |
| 7544 | } |
| 7545 | |
Willy Tarreau | b363a1f | 2013-10-01 10:45:07 +0200 | [diff] [blame] | 7546 | if (!conn->xprt_ctx) |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7547 | return 0; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7548 | ctx = conn->xprt_ctx; |
Emeric Brun | baf8ffb | 2012-09-21 15:27:20 +0200 | [diff] [blame] | 7549 | |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 7550 | smp->data.type = SMP_T_SINT; |
Olivier Houchard | 66ab498 | 2019-02-26 18:37:15 +0100 | [diff] [blame] | 7551 | 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] | 7552 | smp->flags = 0; |
| 7553 | |
| 7554 | return 1; |
| 7555 | } |
| 7556 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7557 | /* parse the "ca-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7558 | 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] | 7559 | { |
| 7560 | if (!*args[cur_arg + 1]) { |
| 7561 | if (err) |
| 7562 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7563 | return ERR_ALERT | ERR_FATAL; |
| 7564 | } |
| 7565 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7566 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7567 | 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] | 7568 | else |
| 7569 | memprintf(&conf->ca_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7570 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7571 | return 0; |
| 7572 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7573 | static int bind_parse_ca_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7574 | { |
| 7575 | return ssl_bind_parse_ca_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7576 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7577 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7578 | /* parse the "ca-sign-file" bind keyword */ |
| 7579 | static int bind_parse_ca_sign_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7580 | { |
| 7581 | if (!*args[cur_arg + 1]) { |
| 7582 | if (err) |
| 7583 | memprintf(err, "'%s' : missing CAfile path", args[cur_arg]); |
| 7584 | return ERR_ALERT | ERR_FATAL; |
| 7585 | } |
| 7586 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7587 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7588 | 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] | 7589 | else |
| 7590 | memprintf(&conf->ca_sign_file, "%s", args[cur_arg + 1]); |
| 7591 | |
| 7592 | return 0; |
| 7593 | } |
| 7594 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7595 | /* parse the "ca-sign-pass" bind keyword */ |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 7596 | static int bind_parse_ca_sign_pass(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7597 | { |
| 7598 | if (!*args[cur_arg + 1]) { |
| 7599 | if (err) |
| 7600 | memprintf(err, "'%s' : missing CAkey password", args[cur_arg]); |
| 7601 | return ERR_ALERT | ERR_FATAL; |
| 7602 | } |
| 7603 | memprintf(&conf->ca_sign_pass, "%s", args[cur_arg + 1]); |
| 7604 | return 0; |
| 7605 | } |
| 7606 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7607 | /* parse the "ciphers" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7608 | 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] | 7609 | { |
| 7610 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7611 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7612 | return ERR_ALERT | ERR_FATAL; |
| 7613 | } |
| 7614 | |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 7615 | free(conf->ciphers); |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7616 | conf->ciphers = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7617 | return 0; |
| 7618 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7619 | static int bind_parse_ciphers(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7620 | { |
| 7621 | return ssl_bind_parse_ciphers(args, cur_arg, px, &conf->ssl_conf, err); |
| 7622 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7623 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 7624 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 7625 | /* parse the "ciphersuites" bind keyword */ |
| 7626 | static int ssl_bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7627 | { |
| 7628 | if (!*args[cur_arg + 1]) { |
| 7629 | memprintf(err, "'%s' : missing cipher suite", args[cur_arg]); |
| 7630 | return ERR_ALERT | ERR_FATAL; |
| 7631 | } |
| 7632 | |
| 7633 | free(conf->ciphersuites); |
| 7634 | conf->ciphersuites = strdup(args[cur_arg + 1]); |
| 7635 | return 0; |
| 7636 | } |
| 7637 | static int bind_parse_ciphersuites(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7638 | { |
| 7639 | return ssl_bind_parse_ciphersuites(args, cur_arg, px, &conf->ssl_conf, err); |
| 7640 | } |
| 7641 | #endif |
| 7642 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7643 | /* parse the "crt" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 7644 | 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] | 7645 | { |
Willy Tarreau | 3801103 | 2013-08-13 16:59:39 +0200 | [diff] [blame] | 7646 | char path[MAXPATHLEN]; |
Willy Tarreau | b75d692 | 2014-04-14 18:05:41 +0200 | [diff] [blame] | 7647 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7648 | if (!*args[cur_arg + 1]) { |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 7649 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7650 | return ERR_ALERT | ERR_FATAL; |
| 7651 | } |
| 7652 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7653 | if ((*args[cur_arg + 1] != '/' ) && global_ssl.crt_base) { |
| 7654 | 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] | 7655 | memprintf(err, "'%s' : path too long", args[cur_arg]); |
| 7656 | return ERR_ALERT | ERR_FATAL; |
| 7657 | } |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7658 | 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] | 7659 | if (ssl_sock_load_cert(path, conf, err) > 0) |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7660 | return ERR_ALERT | ERR_FATAL; |
| 7661 | |
| 7662 | return 0; |
| 7663 | } |
| 7664 | |
Willy Tarreau | 0320934 | 2016-12-22 17:08:28 +0100 | [diff] [blame] | 7665 | if (ssl_sock_load_cert(args[cur_arg + 1], conf, err) > 0) |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7666 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7667 | |
| 7668 | return 0; |
| 7669 | } |
| 7670 | |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7671 | /* parse the "crt-list" bind keyword */ |
| 7672 | static int bind_parse_crt_list(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7673 | { |
| 7674 | if (!*args[cur_arg + 1]) { |
| 7675 | memprintf(err, "'%s' : missing certificate location", args[cur_arg]); |
| 7676 | return ERR_ALERT | ERR_FATAL; |
| 7677 | } |
| 7678 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7679 | 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] | 7680 | memprintf(err, "'%s' : %s", args[cur_arg], *err); |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7681 | return ERR_ALERT | ERR_FATAL; |
Willy Tarreau | ad1731d | 2013-04-02 17:35:58 +0200 | [diff] [blame] | 7682 | } |
Emmanuel Hocdet | fe61656 | 2013-01-22 15:31:15 +0100 | [diff] [blame] | 7683 | |
| 7684 | return 0; |
| 7685 | } |
| 7686 | |
Emeric Brun | fb510ea | 2012-10-05 12:00:26 +0200 | [diff] [blame] | 7687 | /* parse the "crl-file" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7688 | 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] | 7689 | { |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7690 | #ifndef X509_V_FLAG_CRL_CHECK |
| 7691 | if (err) |
| 7692 | memprintf(err, "'%s' : library does not support CRL verify", args[cur_arg]); |
| 7693 | return ERR_ALERT | ERR_FATAL; |
| 7694 | #else |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 7695 | if (!*args[cur_arg + 1]) { |
| 7696 | if (err) |
| 7697 | memprintf(err, "'%s' : missing CRLfile path", args[cur_arg]); |
| 7698 | return ERR_ALERT | ERR_FATAL; |
| 7699 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7700 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 7701 | if ((*args[cur_arg + 1] != '/') && global_ssl.ca_base) |
| 7702 | 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] | 7703 | else |
| 7704 | memprintf(&conf->crl_file, "%s", args[cur_arg + 1]); |
Emeric Brun | c8e8d12 | 2012-10-02 18:42:10 +0200 | [diff] [blame] | 7705 | |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7706 | return 0; |
Emeric Brun | 051cdab | 2012-10-02 19:25:50 +0200 | [diff] [blame] | 7707 | #endif |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7708 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7709 | static int bind_parse_crl_file(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7710 | { |
| 7711 | return ssl_bind_parse_crl_file(args, cur_arg, px, &conf->ssl_conf, err); |
| 7712 | } |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7713 | |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7714 | /* parse the "curves" bind keyword keyword */ |
| 7715 | static int ssl_bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7716 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7717 | #if HA_OPENSSL_VERSION_NUMBER >= 0x1000200fL |
Emmanuel Hocdet | e7f2b73 | 2017-01-09 16:15:54 +0100 | [diff] [blame] | 7718 | if (!*args[cur_arg + 1]) { |
| 7719 | if (err) |
| 7720 | memprintf(err, "'%s' : missing curve suite", args[cur_arg]); |
| 7721 | return ERR_ALERT | ERR_FATAL; |
| 7722 | } |
| 7723 | conf->curves = strdup(args[cur_arg + 1]); |
| 7724 | return 0; |
| 7725 | #else |
| 7726 | if (err) |
| 7727 | memprintf(err, "'%s' : library does not support curve suite", args[cur_arg]); |
| 7728 | return ERR_ALERT | ERR_FATAL; |
| 7729 | #endif |
| 7730 | } |
| 7731 | static int bind_parse_curves(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7732 | { |
| 7733 | return ssl_bind_parse_curves(args, cur_arg, px, &conf->ssl_conf, err); |
| 7734 | } |
| 7735 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7736 | /* parse the "ecdhe" bind keyword keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7737 | 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] | 7738 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7739 | #if HA_OPENSSL_VERSION_NUMBER < 0x0090800fL |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7740 | if (err) |
| 7741 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (too old)", args[cur_arg]); |
| 7742 | return ERR_ALERT | ERR_FATAL; |
| 7743 | #elif defined(OPENSSL_NO_ECDH) |
| 7744 | if (err) |
| 7745 | memprintf(err, "'%s' : library does not support elliptic curve Diffie-Hellman (disabled via OPENSSL_NO_ECDH)", args[cur_arg]); |
| 7746 | return ERR_ALERT | ERR_FATAL; |
| 7747 | #else |
| 7748 | if (!*args[cur_arg + 1]) { |
| 7749 | if (err) |
| 7750 | memprintf(err, "'%s' : missing named curve", args[cur_arg]); |
| 7751 | return ERR_ALERT | ERR_FATAL; |
| 7752 | } |
| 7753 | |
| 7754 | conf->ecdhe = strdup(args[cur_arg + 1]); |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7755 | |
| 7756 | return 0; |
Emeric Brun | 2b58d04 | 2012-09-20 17:10:03 +0200 | [diff] [blame] | 7757 | #endif |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7758 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7759 | static int bind_parse_ecdhe(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7760 | { |
| 7761 | return ssl_bind_parse_ecdhe(args, cur_arg, px, &conf->ssl_conf, err); |
| 7762 | } |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 7763 | |
Bertrand Jacquin | ff13c06 | 2016-11-13 16:37:11 +0000 | [diff] [blame] | 7764 | /* parse the "crt-ignore-err" and "ca-ignore-err" bind keywords */ |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7765 | static int bind_parse_ignore_err(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7766 | { |
| 7767 | int code; |
| 7768 | char *p = args[cur_arg + 1]; |
| 7769 | unsigned long long *ignerr = &conf->crt_ignerr; |
| 7770 | |
| 7771 | if (!*p) { |
| 7772 | if (err) |
| 7773 | memprintf(err, "'%s' : missing error IDs list", args[cur_arg]); |
| 7774 | return ERR_ALERT | ERR_FATAL; |
| 7775 | } |
| 7776 | |
| 7777 | if (strcmp(args[cur_arg], "ca-ignore-err") == 0) |
| 7778 | ignerr = &conf->ca_ignerr; |
| 7779 | |
| 7780 | if (strcmp(p, "all") == 0) { |
| 7781 | *ignerr = ~0ULL; |
| 7782 | return 0; |
| 7783 | } |
| 7784 | |
| 7785 | while (p) { |
| 7786 | code = atoi(p); |
| 7787 | if ((code <= 0) || (code > 63)) { |
| 7788 | if (err) |
| 7789 | memprintf(err, "'%s' : ID '%d' out of range (1..63) in error IDs list '%s'", |
| 7790 | args[cur_arg], code, args[cur_arg + 1]); |
| 7791 | return ERR_ALERT | ERR_FATAL; |
| 7792 | } |
| 7793 | *ignerr |= 1ULL << code; |
| 7794 | p = strchr(p, ','); |
| 7795 | if (p) |
| 7796 | p++; |
| 7797 | } |
| 7798 | |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7799 | return 0; |
| 7800 | } |
| 7801 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7802 | /* parse tls_method_options "no-xxx" and "force-xxx" */ |
| 7803 | 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] | 7804 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7805 | uint16_t v; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7806 | char *p; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7807 | p = strchr(arg, '-'); |
| 7808 | if (!p) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7809 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7810 | p++; |
| 7811 | if (!strcmp(p, "sslv3")) |
| 7812 | v = CONF_SSLV3; |
| 7813 | else if (!strcmp(p, "tlsv10")) |
| 7814 | v = CONF_TLSV10; |
| 7815 | else if (!strcmp(p, "tlsv11")) |
| 7816 | v = CONF_TLSV11; |
| 7817 | else if (!strcmp(p, "tlsv12")) |
| 7818 | v = CONF_TLSV12; |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 7819 | else if (!strcmp(p, "tlsv13")) |
| 7820 | v = CONF_TLSV13; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7821 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7822 | goto fail; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7823 | if (!strncmp(arg, "no-", 3)) |
| 7824 | methods->flags |= methodVersions[v].flag; |
| 7825 | else if (!strncmp(arg, "force-", 6)) |
| 7826 | methods->min = methods->max = v; |
| 7827 | else |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7828 | goto fail; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7829 | return 0; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7830 | fail: |
| 7831 | if (err) |
| 7832 | memprintf(err, "'%s' : option not implemented", arg); |
| 7833 | return ERR_ALERT | ERR_FATAL; |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7834 | } |
| 7835 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7836 | 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] | 7837 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7838 | 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] | 7839 | } |
| 7840 | |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7841 | 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] | 7842 | { |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7843 | return parse_tls_method_options(args[*cur_arg], &newsrv->ssl_ctx.methods, err); |
| 7844 | } |
| 7845 | |
| 7846 | /* parse tls_method min/max: "ssl-min-ver" and "ssl-max-ver" */ |
| 7847 | static int parse_tls_method_minmax(char **args, int cur_arg, struct tls_version_filter *methods, char **err) |
| 7848 | { |
| 7849 | uint16_t i, v = 0; |
| 7850 | char *argv = args[cur_arg + 1]; |
| 7851 | if (!*argv) { |
| 7852 | if (err) |
| 7853 | memprintf(err, "'%s' : missing the ssl/tls version", args[cur_arg]); |
| 7854 | return ERR_ALERT | ERR_FATAL; |
| 7855 | } |
| 7856 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 7857 | if (!strcmp(argv, methodVersions[i].name)) |
| 7858 | v = i; |
| 7859 | if (!v) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7860 | if (err) |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7861 | memprintf(err, "'%s' : unknown ssl/tls version", args[cur_arg + 1]); |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 7862 | return ERR_ALERT | ERR_FATAL; |
| 7863 | } |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7864 | if (!strcmp("ssl-min-ver", args[cur_arg])) |
| 7865 | methods->min = v; |
| 7866 | else if (!strcmp("ssl-max-ver", args[cur_arg])) |
| 7867 | methods->max = v; |
| 7868 | else { |
| 7869 | if (err) |
| 7870 | memprintf(err, "'%s' : option not implemented", args[cur_arg]); |
| 7871 | return ERR_ALERT | ERR_FATAL; |
| 7872 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7873 | return 0; |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7874 | } |
Emeric Brun | 2cb7ae5 | 2012-10-05 14:14:21 +0200 | [diff] [blame] | 7875 | |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 7876 | static int ssl_bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7877 | { |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 7878 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10101000L) && !defined(OPENSSL_IS_BORINGSSL) |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7879 | 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] | 7880 | #endif |
| 7881 | return parse_tls_method_minmax(args, cur_arg, &conf->ssl_methods, err); |
| 7882 | } |
| 7883 | |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 7884 | static int bind_parse_tls_method_minmax(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7885 | { |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 7886 | 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] | 7887 | } |
| 7888 | |
| 7889 | static int srv_parse_tls_method_minmax(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 7890 | { |
| 7891 | return parse_tls_method_minmax(args, *cur_arg, &newsrv->ssl_ctx.methods, err); |
| 7892 | } |
| 7893 | |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7894 | /* parse the "no-tls-tickets" bind keyword */ |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 7895 | 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] | 7896 | { |
Emeric Brun | 8967549 | 2012-10-05 13:48:26 +0200 | [diff] [blame] | 7897 | conf->ssl_options |= BC_SSL_O_NO_TLS_TICKETS; |
Emeric Brun | 81c00f0 | 2012-09-21 14:31:21 +0200 | [diff] [blame] | 7898 | return 0; |
| 7899 | } |
Emeric Brun | 2d0c482 | 2012-10-02 13:45:20 +0200 | [diff] [blame] | 7900 | |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7901 | /* parse the "allow-0rtt" bind keyword */ |
| 7902 | static int ssl_bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 7903 | { |
| 7904 | conf->early_data = 1; |
| 7905 | return 0; |
| 7906 | } |
| 7907 | |
| 7908 | static int bind_parse_allow_0rtt(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7909 | { |
Olivier Houchard | 9679ac9 | 2017-10-27 14:58:08 +0200 | [diff] [blame] | 7910 | conf->ssl_conf.early_data = 1; |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 7911 | return 0; |
| 7912 | } |
| 7913 | |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7914 | /* parse the "npn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7915 | 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] | 7916 | { |
Bernard Spil | 13c53f8 | 2018-02-15 13:34:58 +0100 | [diff] [blame] | 7917 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7918 | char *p1, *p2; |
| 7919 | |
| 7920 | if (!*args[cur_arg + 1]) { |
| 7921 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[cur_arg]); |
| 7922 | return ERR_ALERT | ERR_FATAL; |
| 7923 | } |
| 7924 | |
| 7925 | free(conf->npn_str); |
| 7926 | |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7927 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 7928 | * so we reuse each comma to store the next <len> and need |
| 7929 | * one more for the end of the string. |
| 7930 | */ |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7931 | conf->npn_len = strlen(args[cur_arg + 1]) + 1; |
Willy Tarreau | 3724da1 | 2016-02-12 17:11:12 +0100 | [diff] [blame] | 7932 | conf->npn_str = calloc(1, conf->npn_len + 1); |
Willy Tarreau | 6c9a3d5 | 2012-10-18 18:57:14 +0200 | [diff] [blame] | 7933 | memcpy(conf->npn_str + 1, args[cur_arg + 1], conf->npn_len); |
| 7934 | |
| 7935 | /* replace commas with the name length */ |
| 7936 | p1 = conf->npn_str; |
| 7937 | p2 = p1 + 1; |
| 7938 | while (1) { |
| 7939 | p2 = memchr(p1 + 1, ',', conf->npn_str + conf->npn_len - (p1 + 1)); |
| 7940 | if (!p2) |
| 7941 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7942 | |
| 7943 | if (p2 - (p1 + 1) > 255) { |
| 7944 | *p2 = '\0'; |
| 7945 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 7946 | return ERR_ALERT | ERR_FATAL; |
| 7947 | } |
| 7948 | |
| 7949 | *p1 = p2 - (p1 + 1); |
| 7950 | p1 = p2; |
| 7951 | |
| 7952 | if (!*p2) |
| 7953 | break; |
| 7954 | |
| 7955 | *(p2++) = '\0'; |
| 7956 | } |
| 7957 | return 0; |
| 7958 | #else |
| 7959 | if (err) |
| 7960 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[cur_arg]); |
| 7961 | return ERR_ALERT | ERR_FATAL; |
| 7962 | #endif |
| 7963 | } |
| 7964 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7965 | static int bind_parse_npn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 7966 | { |
| 7967 | return ssl_bind_parse_npn(args, cur_arg, px, &conf->ssl_conf, err); |
| 7968 | } |
| 7969 | |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7970 | /* parse the "alpn" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 7971 | 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] | 7972 | { |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 7973 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7974 | char *p1, *p2; |
| 7975 | |
| 7976 | if (!*args[cur_arg + 1]) { |
| 7977 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[cur_arg]); |
| 7978 | return ERR_ALERT | ERR_FATAL; |
| 7979 | } |
| 7980 | |
| 7981 | free(conf->alpn_str); |
| 7982 | |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7983 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 7984 | * so we reuse each comma to store the next <len> and need |
| 7985 | * one more for the end of the string. |
| 7986 | */ |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7987 | conf->alpn_len = strlen(args[cur_arg + 1]) + 1; |
Marcoen Hirschberg | bef6091 | 2016-02-12 17:05:24 +0100 | [diff] [blame] | 7988 | conf->alpn_str = calloc(1, conf->alpn_len + 1); |
Willy Tarreau | ab861d3 | 2013-04-02 02:30:41 +0200 | [diff] [blame] | 7989 | memcpy(conf->alpn_str + 1, args[cur_arg + 1], conf->alpn_len); |
| 7990 | |
| 7991 | /* replace commas with the name length */ |
| 7992 | p1 = conf->alpn_str; |
| 7993 | p2 = p1 + 1; |
| 7994 | while (1) { |
| 7995 | p2 = memchr(p1 + 1, ',', conf->alpn_str + conf->alpn_len - (p1 + 1)); |
| 7996 | if (!p2) |
| 7997 | p2 = p1 + 1 + strlen(p1 + 1); |
| 7998 | |
| 7999 | if (p2 - (p1 + 1) > 255) { |
| 8000 | *p2 = '\0'; |
| 8001 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[cur_arg], p1 + 1); |
| 8002 | return ERR_ALERT | ERR_FATAL; |
| 8003 | } |
| 8004 | |
| 8005 | *p1 = p2 - (p1 + 1); |
| 8006 | p1 = p2; |
| 8007 | |
| 8008 | if (!*p2) |
| 8009 | break; |
| 8010 | |
| 8011 | *(p2++) = '\0'; |
| 8012 | } |
| 8013 | return 0; |
| 8014 | #else |
| 8015 | if (err) |
| 8016 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[cur_arg]); |
| 8017 | return ERR_ALERT | ERR_FATAL; |
| 8018 | #endif |
| 8019 | } |
| 8020 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8021 | static int bind_parse_alpn(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8022 | { |
| 8023 | return ssl_bind_parse_alpn(args, cur_arg, px, &conf->ssl_conf, err); |
| 8024 | } |
| 8025 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8026 | /* parse the "ssl" bind keyword */ |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8027 | 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] | 8028 | { |
Willy Tarreau | 71a8c7c | 2016-12-21 22:04:54 +0100 | [diff] [blame] | 8029 | conf->xprt = &ssl_sock; |
Willy Tarreau | 4348fad | 2012-09-20 16:48:07 +0200 | [diff] [blame] | 8030 | conf->is_ssl = 1; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8031 | |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8032 | if (global_ssl.listen_default_ciphers && !conf->ssl_conf.ciphers) |
| 8033 | conf->ssl_conf.ciphers = strdup(global_ssl.listen_default_ciphers); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8034 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8035 | if (global_ssl.listen_default_ciphersuites && !conf->ssl_conf.ciphersuites) |
| 8036 | conf->ssl_conf.ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 8037 | #endif |
Emmanuel Hocdet | 4608ed9 | 2017-01-20 13:06:27 +0100 | [diff] [blame] | 8038 | conf->ssl_options |= global_ssl.listen_default_ssloptions; |
Emmanuel Hocdet | 4366476 | 2017-08-09 18:26:20 +0200 | [diff] [blame] | 8039 | conf->ssl_conf.ssl_methods.flags |= global_ssl.listen_default_sslmethods.flags; |
| 8040 | if (!conf->ssl_conf.ssl_methods.min) |
| 8041 | conf->ssl_conf.ssl_methods.min = global_ssl.listen_default_sslmethods.min; |
| 8042 | if (!conf->ssl_conf.ssl_methods.max) |
| 8043 | conf->ssl_conf.ssl_methods.max = global_ssl.listen_default_sslmethods.max; |
Emeric Brun | 76d8895 | 2012-10-05 15:47:31 +0200 | [diff] [blame] | 8044 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 8045 | return 0; |
| 8046 | } |
| 8047 | |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8048 | /* parse the "prefer-client-ciphers" bind keyword */ |
| 8049 | static int bind_parse_pcc(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8050 | { |
| 8051 | conf->ssl_options |= BC_SSL_O_PREF_CLIE_CIPH; |
| 8052 | return 0; |
| 8053 | } |
| 8054 | |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8055 | /* parse the "generate-certificates" bind keyword */ |
| 8056 | static int bind_parse_generate_certs(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8057 | { |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 8058 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 8059 | conf->generate_certs = 1; |
| 8060 | #else |
| 8061 | memprintf(err, "%sthis version of openssl cannot generate SSL certificates.\n", |
| 8062 | err && *err ? *err : ""); |
| 8063 | #endif |
| 8064 | return 0; |
| 8065 | } |
| 8066 | |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 8067 | /* parse the "strict-sni" bind keyword */ |
| 8068 | static int bind_parse_strict_sni(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8069 | { |
| 8070 | conf->strict_sni = 1; |
| 8071 | return 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8072 | } |
| 8073 | |
| 8074 | /* parse the "tls-ticket-keys" bind keyword */ |
| 8075 | static int bind_parse_tls_ticket_keys(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8076 | { |
| 8077 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 8078 | FILE *f; |
| 8079 | int i = 0; |
| 8080 | char thisline[LINESIZE]; |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8081 | struct tls_keys_ref *keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8082 | |
| 8083 | if (!*args[cur_arg + 1]) { |
| 8084 | if (err) |
| 8085 | memprintf(err, "'%s' : missing TLS ticket keys file path", args[cur_arg]); |
| 8086 | return ERR_ALERT | ERR_FATAL; |
| 8087 | } |
| 8088 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8089 | keys_ref = tlskeys_ref_lookup(args[cur_arg + 1]); |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 8090 | if (keys_ref) { |
| 8091 | keys_ref->refcount++; |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8092 | conf->keys_ref = keys_ref; |
| 8093 | return 0; |
| 8094 | } |
| 8095 | |
Vincent Bernat | 02779b6 | 2016-04-03 13:48:43 +0200 | [diff] [blame] | 8096 | keys_ref = malloc(sizeof(*keys_ref)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8097 | if (!keys_ref) { |
| 8098 | if (err) |
| 8099 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 8100 | return ERR_ALERT | ERR_FATAL; |
| 8101 | } |
| 8102 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8103 | keys_ref->tlskeys = malloc(TLS_TICKETS_NO * sizeof(union tls_sess_key)); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8104 | if (!keys_ref->tlskeys) { |
| 8105 | free(keys_ref); |
| 8106 | if (err) |
| 8107 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 8108 | return ERR_ALERT | ERR_FATAL; |
| 8109 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8110 | |
| 8111 | if ((f = fopen(args[cur_arg + 1], "r")) == NULL) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8112 | free(keys_ref->tlskeys); |
| 8113 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8114 | if (err) |
| 8115 | memprintf(err, "'%s' : unable to load ssl tickets keys file", args[cur_arg+1]); |
| 8116 | return ERR_ALERT | ERR_FATAL; |
| 8117 | } |
| 8118 | |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8119 | keys_ref->filename = strdup(args[cur_arg + 1]); |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8120 | if (!keys_ref->filename) { |
| 8121 | free(keys_ref->tlskeys); |
| 8122 | free(keys_ref); |
| 8123 | if (err) |
| 8124 | memprintf(err, "'%s' : allocation error", args[cur_arg+1]); |
| 8125 | return ERR_ALERT | ERR_FATAL; |
| 8126 | } |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8127 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8128 | keys_ref->key_size_bits = 0; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8129 | while (fgets(thisline, sizeof(thisline), f) != NULL) { |
| 8130 | int len = strlen(thisline); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8131 | int dec_size; |
| 8132 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8133 | /* Strip newline characters from the end */ |
| 8134 | if(thisline[len - 1] == '\n') |
| 8135 | thisline[--len] = 0; |
| 8136 | |
| 8137 | if(thisline[len - 1] == '\r') |
| 8138 | thisline[--len] = 0; |
| 8139 | |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8140 | dec_size = base64dec(thisline, len, (char *) (keys_ref->tlskeys + i % TLS_TICKETS_NO), sizeof(union tls_sess_key)); |
| 8141 | if (dec_size < 0) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8142 | free(keys_ref->filename); |
| 8143 | free(keys_ref->tlskeys); |
| 8144 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8145 | if (err) |
| 8146 | 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] | 8147 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8148 | return ERR_ALERT | ERR_FATAL; |
| 8149 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 8150 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_128))) { |
| 8151 | keys_ref->key_size_bits = 128; |
| 8152 | } |
| 8153 | else if (!keys_ref->key_size_bits && (dec_size == sizeof(struct tls_sess_key_256))) { |
| 8154 | keys_ref->key_size_bits = 256; |
| 8155 | } |
| 8156 | else if (((dec_size != sizeof(struct tls_sess_key_128)) && (dec_size != sizeof(struct tls_sess_key_256))) |
| 8157 | || ((dec_size == sizeof(struct tls_sess_key_128) && (keys_ref->key_size_bits != 128))) |
| 8158 | || ((dec_size == sizeof(struct tls_sess_key_256) && (keys_ref->key_size_bits != 256)))) { |
| 8159 | free(keys_ref->filename); |
| 8160 | free(keys_ref->tlskeys); |
| 8161 | free(keys_ref); |
| 8162 | if (err) |
| 8163 | memprintf(err, "'%s' : wrong sized key on line %d", args[cur_arg+1], i + 1); |
| 8164 | fclose(f); |
| 8165 | return ERR_ALERT | ERR_FATAL; |
| 8166 | } |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8167 | i++; |
| 8168 | } |
| 8169 | |
| 8170 | if (i < TLS_TICKETS_NO) { |
Emeric Brun | 09852f7 | 2019-01-10 10:51:13 +0100 | [diff] [blame] | 8171 | free(keys_ref->filename); |
| 8172 | free(keys_ref->tlskeys); |
| 8173 | free(keys_ref); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8174 | if (err) |
| 8175 | 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] | 8176 | fclose(f); |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8177 | return ERR_ALERT | ERR_FATAL; |
| 8178 | } |
| 8179 | |
| 8180 | fclose(f); |
| 8181 | |
| 8182 | /* Use penultimate key for encryption, handle when TLS_TICKETS_NO = 1 */ |
Nenad Merdanovic | 1789115 | 2016-03-25 22:16:57 +0100 | [diff] [blame] | 8183 | i -= 2; |
| 8184 | 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] | 8185 | keys_ref->unique_id = -1; |
Willy Tarreau | 17b4aa1 | 2018-07-17 10:05:32 +0200 | [diff] [blame] | 8186 | keys_ref->refcount = 1; |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 8187 | HA_RWLOCK_INIT(&keys_ref->lock); |
Nenad Merdanovic | 146defa | 2015-05-09 08:46:00 +0200 | [diff] [blame] | 8188 | conf->keys_ref = keys_ref; |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8189 | |
Nenad Merdanovic | 200b0fa | 2015-05-09 08:46:01 +0200 | [diff] [blame] | 8190 | LIST_ADD(&tlskeys_reference, &keys_ref->list); |
| 8191 | |
Nenad Merdanovic | 05552d4 | 2015-02-27 19:56:49 +0100 | [diff] [blame] | 8192 | return 0; |
| 8193 | #else |
| 8194 | if (err) |
| 8195 | memprintf(err, "'%s' : TLS ticket callback extension not supported", args[cur_arg]); |
| 8196 | return ERR_ALERT | ERR_FATAL; |
| 8197 | #endif /* SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB */ |
Emmanuel Hocdet | 6562337 | 2013-01-24 17:17:15 +0100 | [diff] [blame] | 8198 | } |
| 8199 | |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8200 | /* parse the "verify" bind keyword */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8201 | 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] | 8202 | { |
| 8203 | if (!*args[cur_arg + 1]) { |
| 8204 | if (err) |
| 8205 | memprintf(err, "'%s' : missing verify method", args[cur_arg]); |
| 8206 | return ERR_ALERT | ERR_FATAL; |
| 8207 | } |
| 8208 | |
| 8209 | if (strcmp(args[cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8210 | conf->verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8211 | else if (strcmp(args[cur_arg + 1], "optional") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8212 | conf->verify = SSL_SOCK_VERIFY_OPTIONAL; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8213 | else if (strcmp(args[cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8214 | conf->verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8215 | else { |
| 8216 | if (err) |
| 8217 | memprintf(err, "'%s' : unknown verify method '%s', only 'none', 'optional', and 'required' are supported\n", |
| 8218 | args[cur_arg], args[cur_arg + 1]); |
| 8219 | return ERR_ALERT | ERR_FATAL; |
| 8220 | } |
| 8221 | |
| 8222 | return 0; |
| 8223 | } |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 8224 | static int bind_parse_verify(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8225 | { |
| 8226 | return ssl_bind_parse_verify(args, cur_arg, px, &conf->ssl_conf, err); |
| 8227 | } |
Emeric Brun | d94b3fe | 2012-09-20 18:23:56 +0200 | [diff] [blame] | 8228 | |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 8229 | /* parse the "no-ca-names" bind keyword */ |
| 8230 | static int ssl_bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct ssl_bind_conf *conf, char **err) |
| 8231 | { |
| 8232 | conf->no_ca_names = 1; |
| 8233 | return 0; |
| 8234 | } |
| 8235 | static int bind_parse_no_ca_names(char **args, int cur_arg, struct proxy *px, struct bind_conf *conf, char **err) |
| 8236 | { |
| 8237 | return ssl_bind_parse_no_ca_names(args, cur_arg, px, &conf->ssl_conf, err); |
| 8238 | } |
| 8239 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8240 | /************** "server" keywords ****************/ |
| 8241 | |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8242 | /* parse the "npn" bind keyword */ |
| 8243 | static int srv_parse_npn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8244 | { |
| 8245 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 8246 | char *p1, *p2; |
| 8247 | |
| 8248 | if (!*args[*cur_arg + 1]) { |
| 8249 | memprintf(err, "'%s' : missing the comma-delimited NPN protocol suite", args[*cur_arg]); |
| 8250 | return ERR_ALERT | ERR_FATAL; |
| 8251 | } |
| 8252 | |
| 8253 | free(newsrv->ssl_ctx.npn_str); |
| 8254 | |
| 8255 | /* the NPN string is built as a suite of (<len> <name>)*, |
| 8256 | * so we reuse each comma to store the next <len> and need |
| 8257 | * one more for the end of the string. |
| 8258 | */ |
| 8259 | newsrv->ssl_ctx.npn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8260 | newsrv->ssl_ctx.npn_str = calloc(1, newsrv->ssl_ctx.npn_len + 1); |
| 8261 | memcpy(newsrv->ssl_ctx.npn_str + 1, args[*cur_arg + 1], |
| 8262 | newsrv->ssl_ctx.npn_len); |
| 8263 | |
| 8264 | /* replace commas with the name length */ |
| 8265 | p1 = newsrv->ssl_ctx.npn_str; |
| 8266 | p2 = p1 + 1; |
| 8267 | while (1) { |
| 8268 | p2 = memchr(p1 + 1, ',', newsrv->ssl_ctx.npn_str + |
| 8269 | newsrv->ssl_ctx.npn_len - (p1 + 1)); |
| 8270 | if (!p2) |
| 8271 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8272 | |
| 8273 | if (p2 - (p1 + 1) > 255) { |
| 8274 | *p2 = '\0'; |
| 8275 | memprintf(err, "'%s' : NPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8276 | return ERR_ALERT | ERR_FATAL; |
| 8277 | } |
| 8278 | |
| 8279 | *p1 = p2 - (p1 + 1); |
| 8280 | p1 = p2; |
| 8281 | |
| 8282 | if (!*p2) |
| 8283 | break; |
| 8284 | |
| 8285 | *(p2++) = '\0'; |
| 8286 | } |
| 8287 | return 0; |
| 8288 | #else |
| 8289 | if (err) |
| 8290 | memprintf(err, "'%s' : library does not support TLS NPN extension", args[*cur_arg]); |
| 8291 | return ERR_ALERT | ERR_FATAL; |
| 8292 | #endif |
| 8293 | } |
| 8294 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8295 | /* parse the "alpn" or the "check-alpn" server keyword */ |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8296 | static int srv_parse_alpn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8297 | { |
| 8298 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
| 8299 | char *p1, *p2; |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8300 | char **alpn_str; |
| 8301 | int *alpn_len; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8302 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8303 | if (*args[*cur_arg] == 'c') { |
| 8304 | alpn_str = &newsrv->check.alpn_str; |
| 8305 | alpn_len = &newsrv->check.alpn_len; |
| 8306 | } else { |
| 8307 | alpn_str = &newsrv->ssl_ctx.alpn_str; |
| 8308 | alpn_len = &newsrv->ssl_ctx.alpn_len; |
| 8309 | |
| 8310 | } |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8311 | if (!*args[*cur_arg + 1]) { |
| 8312 | memprintf(err, "'%s' : missing the comma-delimited ALPN protocol suite", args[*cur_arg]); |
| 8313 | return ERR_ALERT | ERR_FATAL; |
| 8314 | } |
| 8315 | |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8316 | free(*alpn_str); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8317 | |
| 8318 | /* the ALPN string is built as a suite of (<len> <name>)*, |
| 8319 | * so we reuse each comma to store the next <len> and need |
| 8320 | * one more for the end of the string. |
| 8321 | */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8322 | *alpn_len = strlen(args[*cur_arg + 1]) + 1; |
| 8323 | *alpn_str = calloc(1, *alpn_len + 1); |
| 8324 | memcpy(*alpn_str + 1, args[*cur_arg + 1], *alpn_len); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8325 | |
| 8326 | /* replace commas with the name length */ |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8327 | p1 = *alpn_str; |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8328 | p2 = p1 + 1; |
| 8329 | while (1) { |
Olivier Houchard | 9215014 | 2018-12-21 19:47:01 +0100 | [diff] [blame] | 8330 | p2 = memchr(p1 + 1, ',', *alpn_str + *alpn_len - (p1 + 1)); |
Olivier Houchard | c756600 | 2018-11-20 23:33:50 +0100 | [diff] [blame] | 8331 | if (!p2) |
| 8332 | p2 = p1 + 1 + strlen(p1 + 1); |
| 8333 | |
| 8334 | if (p2 - (p1 + 1) > 255) { |
| 8335 | *p2 = '\0'; |
| 8336 | memprintf(err, "'%s' : ALPN protocol name too long : '%s'", args[*cur_arg], p1 + 1); |
| 8337 | return ERR_ALERT | ERR_FATAL; |
| 8338 | } |
| 8339 | |
| 8340 | *p1 = p2 - (p1 + 1); |
| 8341 | p1 = p2; |
| 8342 | |
| 8343 | if (!*p2) |
| 8344 | break; |
| 8345 | |
| 8346 | *(p2++) = '\0'; |
| 8347 | } |
| 8348 | return 0; |
| 8349 | #else |
| 8350 | if (err) |
| 8351 | memprintf(err, "'%s' : library does not support TLS ALPN extension", args[*cur_arg]); |
| 8352 | return ERR_ALERT | ERR_FATAL; |
| 8353 | #endif |
| 8354 | } |
| 8355 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8356 | /* parse the "ca-file" server keyword */ |
| 8357 | static int srv_parse_ca_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8358 | { |
| 8359 | if (!*args[*cur_arg + 1]) { |
| 8360 | if (err) |
| 8361 | memprintf(err, "'%s' : missing CAfile path", args[*cur_arg]); |
| 8362 | return ERR_ALERT | ERR_FATAL; |
| 8363 | } |
| 8364 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8365 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8366 | 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] | 8367 | else |
| 8368 | memprintf(&newsrv->ssl_ctx.ca_file, "%s", args[*cur_arg + 1]); |
| 8369 | |
| 8370 | return 0; |
| 8371 | } |
| 8372 | |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 8373 | /* parse the "check-sni" server keyword */ |
| 8374 | static int srv_parse_check_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8375 | { |
| 8376 | if (!*args[*cur_arg + 1]) { |
| 8377 | if (err) |
| 8378 | memprintf(err, "'%s' : missing SNI", args[*cur_arg]); |
| 8379 | return ERR_ALERT | ERR_FATAL; |
| 8380 | } |
| 8381 | |
| 8382 | newsrv->check.sni = strdup(args[*cur_arg + 1]); |
| 8383 | if (!newsrv->check.sni) { |
| 8384 | memprintf(err, "'%s' : failed to allocate memory", args[*cur_arg]); |
| 8385 | return ERR_ALERT | ERR_FATAL; |
| 8386 | } |
| 8387 | return 0; |
| 8388 | |
| 8389 | } |
| 8390 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8391 | /* parse the "check-ssl" server keyword */ |
| 8392 | static int srv_parse_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8393 | { |
| 8394 | newsrv->check.use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8395 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 8396 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8397 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8398 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 8399 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8400 | #endif |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8401 | newsrv->ssl_ctx.options |= global_ssl.connect_default_ssloptions; |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8402 | newsrv->ssl_ctx.methods.flags |= global_ssl.connect_default_sslmethods.flags; |
| 8403 | if (!newsrv->ssl_ctx.methods.min) |
| 8404 | newsrv->ssl_ctx.methods.min = global_ssl.connect_default_sslmethods.min; |
| 8405 | if (!newsrv->ssl_ctx.methods.max) |
| 8406 | newsrv->ssl_ctx.methods.max = global_ssl.connect_default_sslmethods.max; |
| 8407 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8408 | return 0; |
| 8409 | } |
| 8410 | |
| 8411 | /* parse the "ciphers" server keyword */ |
| 8412 | static int srv_parse_ciphers(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8413 | { |
| 8414 | if (!*args[*cur_arg + 1]) { |
| 8415 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 8416 | return ERR_ALERT | ERR_FATAL; |
| 8417 | } |
| 8418 | |
| 8419 | free(newsrv->ssl_ctx.ciphers); |
| 8420 | newsrv->ssl_ctx.ciphers = strdup(args[*cur_arg + 1]); |
| 8421 | return 0; |
| 8422 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8423 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8424 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8425 | /* parse the "ciphersuites" server keyword */ |
| 8426 | static int srv_parse_ciphersuites(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8427 | { |
| 8428 | if (!*args[*cur_arg + 1]) { |
| 8429 | memprintf(err, "'%s' : missing cipher suite", args[*cur_arg]); |
| 8430 | return ERR_ALERT | ERR_FATAL; |
| 8431 | } |
| 8432 | |
| 8433 | free(newsrv->ssl_ctx.ciphersuites); |
| 8434 | newsrv->ssl_ctx.ciphersuites = strdup(args[*cur_arg + 1]); |
| 8435 | return 0; |
| 8436 | } |
| 8437 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8438 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8439 | /* parse the "crl-file" server keyword */ |
| 8440 | static int srv_parse_crl_file(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8441 | { |
| 8442 | #ifndef X509_V_FLAG_CRL_CHECK |
| 8443 | if (err) |
| 8444 | memprintf(err, "'%s' : library does not support CRL verify", args[*cur_arg]); |
| 8445 | return ERR_ALERT | ERR_FATAL; |
| 8446 | #else |
| 8447 | if (!*args[*cur_arg + 1]) { |
| 8448 | if (err) |
| 8449 | memprintf(err, "'%s' : missing CRLfile path", args[*cur_arg]); |
| 8450 | return ERR_ALERT | ERR_FATAL; |
| 8451 | } |
| 8452 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8453 | if ((*args[*cur_arg + 1] != '/') && global_ssl.ca_base) |
| 8454 | 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] | 8455 | else |
| 8456 | memprintf(&newsrv->ssl_ctx.crl_file, "%s", args[*cur_arg + 1]); |
| 8457 | |
| 8458 | return 0; |
| 8459 | #endif |
| 8460 | } |
| 8461 | |
Emeric Brun | a7aa309 | 2012-10-26 12:58:00 +0200 | [diff] [blame] | 8462 | /* parse the "crt" server keyword */ |
| 8463 | static int srv_parse_crt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8464 | { |
| 8465 | if (!*args[*cur_arg + 1]) { |
| 8466 | if (err) |
| 8467 | memprintf(err, "'%s' : missing certificate file path", args[*cur_arg]); |
| 8468 | return ERR_ALERT | ERR_FATAL; |
| 8469 | } |
| 8470 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8471 | if ((*args[*cur_arg + 1] != '/') && global_ssl.crt_base) |
Christopher Faulet | ff3a41e | 2017-11-23 09:13:32 +0100 | [diff] [blame] | 8472 | 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] | 8473 | else |
| 8474 | memprintf(&newsrv->ssl_ctx.client_crt, "%s", args[*cur_arg + 1]); |
| 8475 | |
| 8476 | return 0; |
| 8477 | } |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8478 | |
Frédéric Lécaille | 340ae60 | 2017-03-13 10:38:04 +0100 | [diff] [blame] | 8479 | /* parse the "no-check-ssl" server keyword */ |
| 8480 | static int srv_parse_no_check_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8481 | { |
| 8482 | newsrv->check.use_ssl = 0; |
| 8483 | free(newsrv->ssl_ctx.ciphers); |
| 8484 | newsrv->ssl_ctx.ciphers = NULL; |
| 8485 | newsrv->ssl_ctx.options &= ~global_ssl.connect_default_ssloptions; |
| 8486 | return 0; |
| 8487 | } |
| 8488 | |
Frédéric Lécaille | e892c4c | 2017-03-13 12:08:01 +0100 | [diff] [blame] | 8489 | /* parse the "no-send-proxy-v2-ssl" server keyword */ |
| 8490 | static int srv_parse_no_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8491 | { |
| 8492 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 8493 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 8494 | return 0; |
| 8495 | } |
| 8496 | |
| 8497 | /* parse the "no-send-proxy-v2-ssl-cn" server keyword */ |
| 8498 | static int srv_parse_no_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8499 | { |
| 8500 | newsrv->pp_opts &= ~SRV_PP_V2; |
| 8501 | newsrv->pp_opts &= ~SRV_PP_V2_SSL; |
| 8502 | newsrv->pp_opts &= ~SRV_PP_V2_SSL_CN; |
| 8503 | return 0; |
| 8504 | } |
| 8505 | |
Frédéric Lécaille | e381d76 | 2017-03-13 11:54:17 +0100 | [diff] [blame] | 8506 | /* parse the "no-ssl" server keyword */ |
| 8507 | static int srv_parse_no_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8508 | { |
| 8509 | newsrv->use_ssl = 0; |
| 8510 | free(newsrv->ssl_ctx.ciphers); |
| 8511 | newsrv->ssl_ctx.ciphers = NULL; |
| 8512 | return 0; |
| 8513 | } |
| 8514 | |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 8515 | /* parse the "allow-0rtt" server keyword */ |
| 8516 | static int srv_parse_allow_0rtt(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8517 | { |
| 8518 | newsrv->ssl_ctx.options |= SRV_SSL_O_EARLY_DATA; |
| 8519 | return 0; |
| 8520 | } |
| 8521 | |
Willy Tarreau | 2a3fb1c | 2015-02-05 16:47:07 +0100 | [diff] [blame] | 8522 | /* parse the "no-ssl-reuse" server keyword */ |
| 8523 | static int srv_parse_no_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8524 | { |
| 8525 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_REUSE; |
| 8526 | return 0; |
| 8527 | } |
| 8528 | |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 8529 | /* parse the "no-tls-tickets" server keyword */ |
| 8530 | static int srv_parse_no_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8531 | { |
| 8532 | newsrv->ssl_ctx.options |= SRV_SSL_O_NO_TLS_TICKETS; |
| 8533 | return 0; |
| 8534 | } |
David S | afb7683 | 2014-05-08 23:42:08 -0400 | [diff] [blame] | 8535 | /* parse the "send-proxy-v2-ssl" server keyword */ |
| 8536 | static int srv_parse_send_proxy_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8537 | { |
| 8538 | newsrv->pp_opts |= SRV_PP_V2; |
| 8539 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 8540 | return 0; |
| 8541 | } |
| 8542 | |
| 8543 | /* parse the "send-proxy-v2-ssl-cn" server keyword */ |
| 8544 | static int srv_parse_send_proxy_cn(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8545 | { |
| 8546 | newsrv->pp_opts |= SRV_PP_V2; |
| 8547 | newsrv->pp_opts |= SRV_PP_V2_SSL; |
| 8548 | newsrv->pp_opts |= SRV_PP_V2_SSL_CN; |
| 8549 | return 0; |
| 8550 | } |
Emeric Brun | f9c5c47 | 2012-10-11 15:28:34 +0200 | [diff] [blame] | 8551 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8552 | /* parse the "sni" server keyword */ |
| 8553 | static int srv_parse_sni(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8554 | { |
| 8555 | #ifndef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 8556 | memprintf(err, "'%s' : the current SSL library doesn't support the SNI TLS extension", args[*cur_arg]); |
| 8557 | return ERR_ALERT | ERR_FATAL; |
| 8558 | #else |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8559 | char *arg; |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8560 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8561 | arg = args[*cur_arg + 1]; |
| 8562 | if (!*arg) { |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8563 | memprintf(err, "'%s' : missing sni expression", args[*cur_arg]); |
| 8564 | return ERR_ALERT | ERR_FATAL; |
| 8565 | } |
| 8566 | |
Frédéric Lécaille | 9a146de | 2017-03-20 14:54:41 +0100 | [diff] [blame] | 8567 | free(newsrv->sni_expr); |
| 8568 | newsrv->sni_expr = strdup(arg); |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8569 | |
Willy Tarreau | 732eac4 | 2015-07-09 11:40:25 +0200 | [diff] [blame] | 8570 | return 0; |
| 8571 | #endif |
| 8572 | } |
| 8573 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8574 | /* parse the "ssl" server keyword */ |
| 8575 | static int srv_parse_ssl(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8576 | { |
| 8577 | newsrv->use_ssl = 1; |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8578 | if (global_ssl.connect_default_ciphers && !newsrv->ssl_ctx.ciphers) |
| 8579 | newsrv->ssl_ctx.ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8580 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8581 | if (global_ssl.connect_default_ciphersuites && !newsrv->ssl_ctx.ciphersuites) |
| 8582 | newsrv->ssl_ctx.ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 8583 | #endif |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 8584 | return 0; |
| 8585 | } |
| 8586 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8587 | /* parse the "ssl-reuse" server keyword */ |
| 8588 | static int srv_parse_ssl_reuse(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8589 | { |
| 8590 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_REUSE; |
| 8591 | return 0; |
| 8592 | } |
| 8593 | |
Frédéric Lécaille | 2cfcdbe | 2017-03-13 11:32:20 +0100 | [diff] [blame] | 8594 | /* parse the "tls-tickets" server keyword */ |
| 8595 | static int srv_parse_tls_tickets(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8596 | { |
| 8597 | newsrv->ssl_ctx.options &= ~SRV_SSL_O_NO_TLS_TICKETS; |
| 8598 | return 0; |
| 8599 | } |
| 8600 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8601 | /* parse the "verify" server keyword */ |
| 8602 | static int srv_parse_verify(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8603 | { |
| 8604 | if (!*args[*cur_arg + 1]) { |
| 8605 | if (err) |
| 8606 | memprintf(err, "'%s' : missing verify method", args[*cur_arg]); |
| 8607 | return ERR_ALERT | ERR_FATAL; |
| 8608 | } |
| 8609 | |
| 8610 | if (strcmp(args[*cur_arg + 1], "none") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8611 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_NONE; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8612 | else if (strcmp(args[*cur_arg + 1], "required") == 0) |
Emeric Brun | 850efd5 | 2014-01-29 12:24:34 +0100 | [diff] [blame] | 8613 | newsrv->ssl_ctx.verify = SSL_SOCK_VERIFY_REQUIRED; |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8614 | else { |
| 8615 | if (err) |
| 8616 | memprintf(err, "'%s' : unknown verify method '%s', only 'none' and 'required' are supported\n", |
| 8617 | args[*cur_arg], args[*cur_arg + 1]); |
| 8618 | return ERR_ALERT | ERR_FATAL; |
| 8619 | } |
| 8620 | |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8621 | return 0; |
| 8622 | } |
| 8623 | |
| 8624 | /* parse the "verifyhost" server keyword */ |
| 8625 | static int srv_parse_verifyhost(char **args, int *cur_arg, struct proxy *px, struct server *newsrv, char **err) |
| 8626 | { |
| 8627 | if (!*args[*cur_arg + 1]) { |
| 8628 | if (err) |
| 8629 | memprintf(err, "'%s' : missing hostname to verify against", args[*cur_arg]); |
| 8630 | return ERR_ALERT | ERR_FATAL; |
| 8631 | } |
| 8632 | |
Frédéric Lécaille | 273f321 | 2017-03-13 15:52:01 +0100 | [diff] [blame] | 8633 | free(newsrv->ssl_ctx.verify_host); |
Evan Broder | be55431 | 2013-06-27 00:05:25 -0700 | [diff] [blame] | 8634 | newsrv->ssl_ctx.verify_host = strdup(args[*cur_arg + 1]); |
| 8635 | |
Emeric Brun | ef42d92 | 2012-10-11 16:11:36 +0200 | [diff] [blame] | 8636 | return 0; |
| 8637 | } |
| 8638 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 8639 | /* parse the "ssl-default-bind-options" keyword in global section */ |
| 8640 | static int ssl_parse_default_bind_options(char **args, int section_type, struct proxy *curpx, |
| 8641 | struct proxy *defpx, const char *file, int line, |
| 8642 | char **err) { |
| 8643 | int i = 1; |
| 8644 | |
| 8645 | if (*(args[i]) == 0) { |
| 8646 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8647 | return -1; |
| 8648 | } |
| 8649 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8650 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8651 | global_ssl.listen_default_ssloptions |= BC_SSL_O_NO_TLS_TICKETS; |
Lukas Tribus | 53ae85c | 2017-05-04 15:45:40 +0000 | [diff] [blame] | 8652 | else if (!strcmp(args[i], "prefer-client-ciphers")) |
| 8653 | global_ssl.listen_default_ssloptions |= BC_SSL_O_PREF_CLIE_CIPH; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8654 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8655 | if (!parse_tls_method_minmax(args, i, &global_ssl.listen_default_sslmethods, err)) |
| 8656 | i++; |
| 8657 | else { |
| 8658 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8659 | return -1; |
| 8660 | } |
| 8661 | } |
| 8662 | 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] | 8663 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8664 | return -1; |
| 8665 | } |
| 8666 | i++; |
| 8667 | } |
| 8668 | return 0; |
| 8669 | } |
| 8670 | |
| 8671 | /* parse the "ssl-default-server-options" keyword in global section */ |
| 8672 | static int ssl_parse_default_server_options(char **args, int section_type, struct proxy *curpx, |
| 8673 | struct proxy *defpx, const char *file, int line, |
| 8674 | char **err) { |
| 8675 | int i = 1; |
| 8676 | |
| 8677 | if (*(args[i]) == 0) { |
| 8678 | memprintf(err, "global statement '%s' expects an option as an argument.", args[0]); |
| 8679 | return -1; |
| 8680 | } |
| 8681 | while (*(args[i])) { |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 8682 | if (!strcmp(args[i], "no-tls-tickets")) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8683 | global_ssl.connect_default_ssloptions |= SRV_SSL_O_NO_TLS_TICKETS; |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 8684 | else if (!strcmp(args[i], "ssl-min-ver") || !strcmp(args[i], "ssl-max-ver")) { |
| 8685 | if (!parse_tls_method_minmax(args, i, &global_ssl.connect_default_sslmethods, err)) |
| 8686 | i++; |
| 8687 | else { |
| 8688 | memprintf(err, "%s on global statement '%s'.", *err, args[0]); |
| 8689 | return -1; |
| 8690 | } |
| 8691 | } |
| 8692 | 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] | 8693 | memprintf(err, "unknown option '%s' on global statement '%s'.", args[i], args[0]); |
| 8694 | return -1; |
| 8695 | } |
| 8696 | i++; |
| 8697 | } |
| 8698 | return 0; |
| 8699 | } |
| 8700 | |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8701 | /* parse the "ca-base" / "crt-base" keywords in global section. |
| 8702 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8703 | */ |
| 8704 | static int ssl_parse_global_ca_crt_base(char **args, int section_type, struct proxy *curpx, |
| 8705 | struct proxy *defpx, const char *file, int line, |
| 8706 | char **err) |
| 8707 | { |
| 8708 | char **target; |
| 8709 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8710 | 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] | 8711 | |
| 8712 | if (too_many_args(1, args, err, NULL)) |
| 8713 | return -1; |
| 8714 | |
| 8715 | if (*target) { |
| 8716 | memprintf(err, "'%s' already specified.", args[0]); |
| 8717 | return -1; |
| 8718 | } |
| 8719 | |
| 8720 | if (*(args[1]) == 0) { |
| 8721 | memprintf(err, "global statement '%s' expects a directory path as an argument.", args[0]); |
| 8722 | return -1; |
| 8723 | } |
| 8724 | *target = strdup(args[1]); |
| 8725 | return 0; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8726 | } |
| 8727 | |
| 8728 | /* parse the "ssl-mode-async" keyword in global section. |
| 8729 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8730 | */ |
| 8731 | static int ssl_parse_global_ssl_async(char **args, int section_type, struct proxy *curpx, |
| 8732 | struct proxy *defpx, const char *file, int line, |
| 8733 | char **err) |
| 8734 | { |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8735 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1010000fL) && !defined(OPENSSL_NO_ASYNC) |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8736 | global_ssl.async = 1; |
Emeric Brun | ece0c33 | 2017-12-06 13:51:49 +0100 | [diff] [blame] | 8737 | global.ssl_used_async_engines = nb_engines; |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8738 | return 0; |
| 8739 | #else |
| 8740 | memprintf(err, "'%s': openssl library does not support async mode", args[0]); |
| 8741 | return -1; |
| 8742 | #endif |
| 8743 | } |
| 8744 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8745 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 8746 | static int ssl_check_async_engine_count(void) { |
| 8747 | int err_code = 0; |
| 8748 | |
Emeric Brun | 3854e01 | 2017-05-17 20:42:48 +0200 | [diff] [blame] | 8749 | if (global_ssl.async && (openssl_engines_initialized > 32)) { |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 8750 | 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] | 8751 | err_code = ERR_ABORT; |
| 8752 | } |
| 8753 | return err_code; |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 8754 | } |
| 8755 | |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8756 | /* parse the "ssl-engine" keyword in global section. |
| 8757 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8758 | */ |
| 8759 | static int ssl_parse_global_ssl_engine(char **args, int section_type, struct proxy *curpx, |
| 8760 | struct proxy *defpx, const char *file, int line, |
| 8761 | char **err) |
| 8762 | { |
| 8763 | char *algo; |
| 8764 | int ret = -1; |
| 8765 | |
| 8766 | if (*(args[1]) == 0) { |
| 8767 | memprintf(err, "global statement '%s' expects a valid engine name as an argument.", args[0]); |
| 8768 | return ret; |
| 8769 | } |
| 8770 | |
| 8771 | if (*(args[2]) == 0) { |
| 8772 | /* if no list of algorithms is given, it defaults to ALL */ |
| 8773 | algo = strdup("ALL"); |
| 8774 | goto add_engine; |
| 8775 | } |
| 8776 | |
| 8777 | /* otherwise the expected format is ssl-engine <engine_name> algo <list of algo> */ |
| 8778 | if (strcmp(args[2], "algo") != 0) { |
| 8779 | memprintf(err, "global statement '%s' expects to have algo keyword.", args[0]); |
| 8780 | return ret; |
| 8781 | } |
| 8782 | |
| 8783 | if (*(args[3]) == 0) { |
| 8784 | memprintf(err, "global statement '%s' expects algorithm names as an argument.", args[0]); |
| 8785 | return ret; |
| 8786 | } |
| 8787 | algo = strdup(args[3]); |
| 8788 | |
| 8789 | add_engine: |
| 8790 | if (ssl_init_single_engine(args[1], algo)==0) { |
| 8791 | openssl_engines_initialized++; |
| 8792 | ret = 0; |
| 8793 | } |
| 8794 | free(algo); |
| 8795 | return ret; |
| 8796 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 8797 | #endif |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 8798 | |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8799 | /* parse the "ssl-default-bind-ciphers" / "ssl-default-server-ciphers" keywords |
| 8800 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8801 | */ |
| 8802 | static int ssl_parse_global_ciphers(char **args, int section_type, struct proxy *curpx, |
| 8803 | struct proxy *defpx, const char *file, int line, |
| 8804 | char **err) |
| 8805 | { |
| 8806 | char **target; |
| 8807 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8808 | 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] | 8809 | |
| 8810 | if (too_many_args(1, args, err, NULL)) |
| 8811 | return -1; |
| 8812 | |
| 8813 | if (*(args[1]) == 0) { |
| 8814 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8815 | return -1; |
| 8816 | } |
| 8817 | |
| 8818 | free(*target); |
| 8819 | *target = strdup(args[1]); |
| 8820 | return 0; |
| 8821 | } |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8822 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 8823 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 8824 | /* parse the "ssl-default-bind-ciphersuites" / "ssl-default-server-ciphersuites" keywords |
| 8825 | * in global section. Returns <0 on alert, >0 on warning, 0 on success. |
| 8826 | */ |
| 8827 | static int ssl_parse_global_ciphersuites(char **args, int section_type, struct proxy *curpx, |
| 8828 | struct proxy *defpx, const char *file, int line, |
| 8829 | char **err) |
| 8830 | { |
| 8831 | char **target; |
| 8832 | |
| 8833 | target = (args[0][12] == 'b') ? &global_ssl.listen_default_ciphersuites : &global_ssl.connect_default_ciphersuites; |
| 8834 | |
| 8835 | if (too_many_args(1, args, err, NULL)) |
| 8836 | return -1; |
| 8837 | |
| 8838 | if (*(args[1]) == 0) { |
| 8839 | memprintf(err, "global statement '%s' expects a cipher suite as an argument.", args[0]); |
| 8840 | return -1; |
| 8841 | } |
| 8842 | |
| 8843 | free(*target); |
| 8844 | *target = strdup(args[1]); |
| 8845 | return 0; |
| 8846 | } |
| 8847 | #endif |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 8848 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8849 | /* parse various global tune.ssl settings consisting in positive integers. |
| 8850 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8851 | */ |
| 8852 | static int ssl_parse_global_int(char **args, int section_type, struct proxy *curpx, |
| 8853 | struct proxy *defpx, const char *file, int line, |
| 8854 | char **err) |
| 8855 | { |
| 8856 | int *target; |
| 8857 | |
| 8858 | if (strcmp(args[0], "tune.ssl.cachesize") == 0) |
| 8859 | target = &global.tune.sslcachesize; |
| 8860 | else if (strcmp(args[0], "tune.ssl.maxrecord") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8861 | target = (int *)&global_ssl.max_record; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8862 | else if (strcmp(args[0], "tune.ssl.ssl-ctx-cache-size") == 0) |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8863 | target = &global_ssl.ctx_cache; |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 8864 | else if (strcmp(args[0], "maxsslconn") == 0) |
| 8865 | target = &global.maxsslconn; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8866 | else if (strcmp(args[0], "tune.ssl.capture-cipherlist-size") == 0) |
| 8867 | target = &global_ssl.capture_cipherlist; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8868 | else { |
| 8869 | memprintf(err, "'%s' keyword not unhandled (please report this bug).", args[0]); |
| 8870 | return -1; |
| 8871 | } |
| 8872 | |
| 8873 | if (too_many_args(1, args, err, NULL)) |
| 8874 | return -1; |
| 8875 | |
| 8876 | if (*(args[1]) == 0) { |
| 8877 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8878 | return -1; |
| 8879 | } |
| 8880 | |
| 8881 | *target = atoi(args[1]); |
| 8882 | if (*target < 0) { |
| 8883 | memprintf(err, "'%s' expects a positive numeric value.", args[0]); |
| 8884 | return -1; |
| 8885 | } |
| 8886 | return 0; |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8887 | } |
| 8888 | |
| 8889 | static int ssl_parse_global_capture_cipherlist(char **args, int section_type, struct proxy *curpx, |
| 8890 | struct proxy *defpx, const char *file, int line, |
| 8891 | char **err) |
| 8892 | { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8893 | int ret; |
| 8894 | |
| 8895 | ret = ssl_parse_global_int(args, section_type, curpx, defpx, file, line, err); |
| 8896 | if (ret != 0) |
| 8897 | return ret; |
| 8898 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8899 | if (pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8900 | memprintf(err, "'%s' is already configured.", args[0]); |
| 8901 | return -1; |
| 8902 | } |
| 8903 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8904 | pool_head_ssl_capture = create_pool("ssl-capture", sizeof(struct ssl_capture) + global_ssl.capture_cipherlist, MEM_F_SHARED); |
| 8905 | if (!pool_head_ssl_capture) { |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 8906 | memprintf(err, "Out of memory error."); |
| 8907 | return -1; |
| 8908 | } |
| 8909 | return 0; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8910 | } |
| 8911 | |
| 8912 | /* parse "ssl.force-private-cache". |
| 8913 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8914 | */ |
| 8915 | static int ssl_parse_global_private_cache(char **args, int section_type, struct proxy *curpx, |
| 8916 | struct proxy *defpx, const char *file, int line, |
| 8917 | char **err) |
| 8918 | { |
| 8919 | if (too_many_args(0, args, err, NULL)) |
| 8920 | return -1; |
| 8921 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8922 | global_ssl.private_cache = 1; |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8923 | return 0; |
| 8924 | } |
| 8925 | |
| 8926 | /* parse "ssl.lifetime". |
| 8927 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8928 | */ |
| 8929 | static int ssl_parse_global_lifetime(char **args, int section_type, struct proxy *curpx, |
| 8930 | struct proxy *defpx, const char *file, int line, |
| 8931 | char **err) |
| 8932 | { |
| 8933 | const char *res; |
| 8934 | |
| 8935 | if (too_many_args(1, args, err, NULL)) |
| 8936 | return -1; |
| 8937 | |
| 8938 | if (*(args[1]) == 0) { |
| 8939 | memprintf(err, "'%s' expects ssl sessions <lifetime> in seconds as argument.", args[0]); |
| 8940 | return -1; |
| 8941 | } |
| 8942 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8943 | res = parse_time_err(args[1], &global_ssl.life_time, TIME_UNIT_S); |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8944 | if (res) { |
| 8945 | memprintf(err, "unexpected character '%c' in argument to <%s>.", *res, args[0]); |
| 8946 | return -1; |
| 8947 | } |
| 8948 | return 0; |
| 8949 | } |
| 8950 | |
| 8951 | #ifndef OPENSSL_NO_DH |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 8952 | /* parse "ssl-dh-param-file". |
| 8953 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8954 | */ |
| 8955 | static int ssl_parse_global_dh_param_file(char **args, int section_type, struct proxy *curpx, |
| 8956 | struct proxy *defpx, const char *file, int line, |
| 8957 | char **err) |
| 8958 | { |
| 8959 | if (too_many_args(1, args, err, NULL)) |
| 8960 | return -1; |
| 8961 | |
| 8962 | if (*(args[1]) == 0) { |
| 8963 | memprintf(err, "'%s' expects a file path as an argument.", args[0]); |
| 8964 | return -1; |
| 8965 | } |
| 8966 | |
| 8967 | if (ssl_sock_load_global_dh_param_from_file(args[1])) { |
| 8968 | memprintf(err, "'%s': unable to load DH parameters from file <%s>.", args[0], args[1]); |
| 8969 | return -1; |
| 8970 | } |
| 8971 | return 0; |
| 8972 | } |
| 8973 | |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8974 | /* parse "ssl.default-dh-param". |
| 8975 | * Returns <0 on alert, >0 on warning, 0 on success. |
| 8976 | */ |
| 8977 | static int ssl_parse_global_default_dh(char **args, int section_type, struct proxy *curpx, |
| 8978 | struct proxy *defpx, const char *file, int line, |
| 8979 | char **err) |
| 8980 | { |
| 8981 | if (too_many_args(1, args, err, NULL)) |
| 8982 | return -1; |
| 8983 | |
| 8984 | if (*(args[1]) == 0) { |
| 8985 | memprintf(err, "'%s' expects an integer argument.", args[0]); |
| 8986 | return -1; |
| 8987 | } |
| 8988 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 8989 | global_ssl.default_dh_param = atoi(args[1]); |
| 8990 | if (global_ssl.default_dh_param < 1024) { |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 8991 | memprintf(err, "'%s' expects a value >= 1024.", args[0]); |
| 8992 | return -1; |
| 8993 | } |
| 8994 | return 0; |
| 8995 | } |
| 8996 | #endif |
| 8997 | |
| 8998 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 8999 | /* This function is used with TLS ticket keys management. It permits to browse |
| 9000 | * each reference. The variable <getnext> must contain the current node, |
| 9001 | * <end> point to the root node. |
| 9002 | */ |
| 9003 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9004 | static inline |
| 9005 | struct tls_keys_ref *tlskeys_list_get_next(struct tls_keys_ref *getnext, struct list *end) |
| 9006 | { |
| 9007 | struct tls_keys_ref *ref = getnext; |
| 9008 | |
| 9009 | while (1) { |
| 9010 | |
| 9011 | /* Get next list entry. */ |
| 9012 | ref = LIST_NEXT(&ref->list, struct tls_keys_ref *, list); |
| 9013 | |
| 9014 | /* If the entry is the last of the list, return NULL. */ |
| 9015 | if (&ref->list == end) |
| 9016 | return NULL; |
| 9017 | |
| 9018 | return ref; |
| 9019 | } |
| 9020 | } |
| 9021 | |
| 9022 | static inline |
| 9023 | struct tls_keys_ref *tlskeys_ref_lookup_ref(const char *reference) |
| 9024 | { |
| 9025 | int id; |
| 9026 | char *error; |
| 9027 | |
| 9028 | /* If the reference starts by a '#', this is numeric id. */ |
| 9029 | if (reference[0] == '#') { |
| 9030 | /* Try to convert the numeric id. If the conversion fails, the lookup fails. */ |
| 9031 | id = strtol(reference + 1, &error, 10); |
| 9032 | if (*error != '\0') |
| 9033 | return NULL; |
| 9034 | |
| 9035 | /* Perform the unique id lookup. */ |
| 9036 | return tlskeys_ref_lookupid(id); |
| 9037 | } |
| 9038 | |
| 9039 | /* Perform the string lookup. */ |
| 9040 | return tlskeys_ref_lookup(reference); |
| 9041 | } |
| 9042 | #endif |
| 9043 | |
| 9044 | |
| 9045 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9046 | |
| 9047 | static int cli_io_handler_tlskeys_files(struct appctx *appctx); |
| 9048 | |
| 9049 | static inline int cli_io_handler_tlskeys_entries(struct appctx *appctx) { |
| 9050 | return cli_io_handler_tlskeys_files(appctx); |
| 9051 | } |
| 9052 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9053 | /* dumps all tls keys. Relies on cli.i0 (non-null = only list file names), cli.i1 |
| 9054 | * (next index to be dumped), and cli.p0 (next key reference). |
| 9055 | */ |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9056 | static int cli_io_handler_tlskeys_files(struct appctx *appctx) { |
| 9057 | |
| 9058 | struct stream_interface *si = appctx->owner; |
| 9059 | |
| 9060 | switch (appctx->st2) { |
| 9061 | case STAT_ST_INIT: |
| 9062 | /* Display the column headers. If the message cannot be sent, |
Joseph Herlant | 017b3da | 2018-11-15 09:07:59 -0800 | [diff] [blame] | 9063 | * quit the function with returning 0. The function is called |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9064 | * later and restart at the state "STAT_ST_INIT". |
| 9065 | */ |
| 9066 | chunk_reset(&trash); |
| 9067 | |
| 9068 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) |
| 9069 | chunk_appendf(&trash, "# id secret\n"); |
| 9070 | else |
| 9071 | chunk_appendf(&trash, "# id (file)\n"); |
| 9072 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9073 | if (ci_putchk(si_ic(si), &trash) == -1) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9074 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9075 | return 0; |
| 9076 | } |
| 9077 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9078 | /* Now, we start the browsing of the references lists. |
| 9079 | * Note that the following call to LIST_ELEM return bad pointer. The only |
| 9080 | * available field of this pointer is <list>. It is used with the function |
| 9081 | * tlskeys_list_get_next() for retruning the first available entry |
| 9082 | */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9083 | if (appctx->ctx.cli.p0 == NULL) { |
| 9084 | appctx->ctx.cli.p0 = LIST_ELEM(&tlskeys_reference, struct tls_keys_ref *, list); |
| 9085 | 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] | 9086 | } |
| 9087 | |
| 9088 | appctx->st2 = STAT_ST_LIST; |
| 9089 | /* fall through */ |
| 9090 | |
| 9091 | case STAT_ST_LIST: |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9092 | while (appctx->ctx.cli.p0) { |
| 9093 | struct tls_keys_ref *ref = appctx->ctx.cli.p0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9094 | |
| 9095 | chunk_reset(&trash); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9096 | 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] | 9097 | chunk_appendf(&trash, "# "); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9098 | |
| 9099 | if (appctx->ctx.cli.i1 == 0) |
| 9100 | chunk_appendf(&trash, "%d (%s)\n", ref->unique_id, ref->filename); |
| 9101 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9102 | if (appctx->io_handler == cli_io_handler_tlskeys_entries) { |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9103 | int head; |
| 9104 | |
| 9105 | HA_RWLOCK_RDLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
| 9106 | head = ref->tls_ticket_enc_index; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9107 | while (appctx->ctx.cli.i1 < TLS_TICKETS_NO) { |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 9108 | struct buffer *t2 = get_trash_chunk(); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9109 | |
| 9110 | chunk_reset(t2); |
| 9111 | /* 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] | 9112 | if (ref->key_size_bits == 128) { |
| 9113 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 9114 | sizeof(struct tls_sess_key_128), |
| 9115 | t2->area, t2->size); |
| 9116 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 9117 | t2->area); |
| 9118 | } |
| 9119 | else if (ref->key_size_bits == 256) { |
| 9120 | t2->data = a2base64((char *)(ref->tlskeys + (head + 2 + appctx->ctx.cli.i1) % TLS_TICKETS_NO), |
| 9121 | sizeof(struct tls_sess_key_256), |
| 9122 | t2->area, t2->size); |
| 9123 | chunk_appendf(&trash, "%d.%d %s\n", ref->unique_id, appctx->ctx.cli.i1, |
| 9124 | t2->area); |
| 9125 | } |
| 9126 | else { |
| 9127 | /* This case should never happen */ |
| 9128 | chunk_appendf(&trash, "%d.%d <unknown>\n", ref->unique_id, appctx->ctx.cli.i1); |
| 9129 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9130 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9131 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9132 | /* let's try again later from this stream. We add ourselves into |
| 9133 | * this stream's users so that it can remove us upon termination. |
| 9134 | */ |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9135 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9136 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9137 | return 0; |
| 9138 | } |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9139 | appctx->ctx.cli.i1++; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9140 | } |
Christopher Faulet | 16f45c8 | 2018-02-16 11:23:49 +0100 | [diff] [blame] | 9141 | HA_RWLOCK_RDUNLOCK(TLSKEYS_REF_LOCK, &ref->lock); |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9142 | appctx->ctx.cli.i1 = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9143 | } |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 9144 | if (ci_putchk(si_ic(si), &trash) == -1) { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9145 | /* let's try again later from this stream. We add ourselves into |
| 9146 | * this stream's users so that it can remove us upon termination. |
| 9147 | */ |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 9148 | si_rx_room_blk(si); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9149 | return 0; |
| 9150 | } |
| 9151 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9152 | 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] | 9153 | break; |
| 9154 | |
| 9155 | /* get next list entry and check the end of the list */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9156 | 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] | 9157 | } |
| 9158 | |
| 9159 | appctx->st2 = STAT_ST_FIN; |
| 9160 | /* fall through */ |
| 9161 | |
| 9162 | default: |
| 9163 | appctx->st2 = STAT_ST_FIN; |
| 9164 | return 1; |
| 9165 | } |
| 9166 | return 0; |
| 9167 | } |
| 9168 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9169 | /* 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] | 9170 | 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] | 9171 | { |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9172 | /* no parameter, shows only file list */ |
| 9173 | if (!*args[2]) { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9174 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9175 | appctx->io_handler = cli_io_handler_tlskeys_files; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 9176 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9177 | } |
| 9178 | |
| 9179 | if (args[2][0] == '*') { |
| 9180 | /* list every TLS ticket keys */ |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9181 | appctx->ctx.cli.i0 = 1; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9182 | } else { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9183 | appctx->ctx.cli.p0 = tlskeys_ref_lookup_ref(args[2]); |
| 9184 | if (!appctx->ctx.cli.p0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9185 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9186 | appctx->ctx.cli.msg = "'show tls-keys' unable to locate referenced filename\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9187 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9188 | return 1; |
| 9189 | } |
| 9190 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9191 | appctx->io_handler = cli_io_handler_tlskeys_entries; |
Willy Tarreau | 3067bfa | 2016-12-05 14:50:15 +0100 | [diff] [blame] | 9192 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9193 | } |
| 9194 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9195 | 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] | 9196 | { |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9197 | struct tls_keys_ref *ref; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9198 | int ret; |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9199 | |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9200 | /* Expect two parameters: the filename and the new new TLS key in encoding */ |
| 9201 | if (!*args[3] || !*args[4]) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9202 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9203 | appctx->ctx.cli.msg = "'set ssl tls-key' expects a filename and the new TLS key in base64 encoding.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9204 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9205 | return 1; |
| 9206 | } |
| 9207 | |
Willy Tarreau | f5f26e8 | 2016-12-16 18:47:27 +0100 | [diff] [blame] | 9208 | ref = tlskeys_ref_lookup_ref(args[3]); |
| 9209 | if (!ref) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9210 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9211 | appctx->ctx.cli.msg = "'set ssl tls-key' unable to locate referenced filename\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9212 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9213 | return 1; |
| 9214 | } |
| 9215 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9216 | ret = base64dec(args[4], strlen(args[4]), trash.area, trash.size); |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9217 | if (ret < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9218 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9219 | appctx->ctx.cli.msg = "'set ssl tls-key' received invalid base64 encoded TLS key.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9220 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9221 | return 1; |
| 9222 | } |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9223 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9224 | trash.data = ret; |
Emeric Brun | 9e75477 | 2019-01-10 17:51:55 +0100 | [diff] [blame] | 9225 | if (ssl_sock_update_tlskey_ref(ref, &trash) < 0) { |
| 9226 | appctx->ctx.cli.severity = LOG_ERR; |
| 9227 | appctx->ctx.cli.msg = "'set ssl tls-key' received a key of wrong size.\n"; |
| 9228 | appctx->st0 = CLI_ST_PRINT; |
| 9229 | return 1; |
| 9230 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9231 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 9232 | appctx->ctx.cli.msg = "TLS ticket key updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9233 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9234 | return 1; |
| 9235 | |
| 9236 | } |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9237 | #endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9238 | |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 9239 | 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] | 9240 | { |
| 9241 | #if (defined SSL_CTRL_SET_TLSEXT_STATUS_REQ_CB && !defined OPENSSL_NO_OCSP) |
| 9242 | char *err = NULL; |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9243 | int i, j, ret; |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9244 | |
| 9245 | if (!payload) |
| 9246 | payload = args[3]; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9247 | |
| 9248 | /* Expect one parameter: the new response in base64 encoding */ |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9249 | if (!*payload) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9250 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9251 | appctx->ctx.cli.msg = "'set ssl ocsp-response' expects response in base64 encoding.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9252 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9253 | return 1; |
| 9254 | } |
Aurélien Nephtali | 1e0867c | 2018-04-18 14:04:58 +0200 | [diff] [blame] | 9255 | |
| 9256 | /* remove \r and \n from the payload */ |
| 9257 | for (i = 0, j = 0; payload[i]; i++) { |
| 9258 | if (payload[i] == '\r' || payload[i] == '\n') |
| 9259 | continue; |
| 9260 | payload[j++] = payload[i]; |
| 9261 | } |
| 9262 | payload[j] = 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9263 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9264 | ret = base64dec(payload, j, trash.area, trash.size); |
| 9265 | if (ret < 0) { |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9266 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9267 | appctx->ctx.cli.msg = "'set ssl ocsp-response' received invalid base64 encoded response.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9268 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9269 | return 1; |
| 9270 | } |
| 9271 | |
Willy Tarreau | 1c913e4 | 2018-08-22 05:26:57 +0200 | [diff] [blame] | 9272 | trash.data = ret; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9273 | if (ssl_sock_update_ocsp_response(&trash, &err)) { |
| 9274 | if (err) { |
| 9275 | memprintf(&err, "%s.\n", err); |
| 9276 | appctx->ctx.cli.err = err; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9277 | appctx->st0 = CLI_ST_PRINT_FREE; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9278 | } |
Aurélien Nephtali | 9a4da68 | 2018-04-16 19:02:42 +0200 | [diff] [blame] | 9279 | else { |
| 9280 | appctx->ctx.cli.severity = LOG_ERR; |
| 9281 | appctx->ctx.cli.msg = "Failed to update OCSP response.\n"; |
| 9282 | appctx->st0 = CLI_ST_PRINT; |
| 9283 | } |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9284 | return 1; |
| 9285 | } |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9286 | appctx->ctx.cli.severity = LOG_INFO; |
Aurélien Nephtali | 6e8a41d | 2018-03-15 21:48:50 +0100 | [diff] [blame] | 9287 | appctx->ctx.cli.msg = "OCSP Response updated!\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9288 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9289 | return 1; |
| 9290 | #else |
Andjelko Iharos | c3680ec | 2017-07-20 16:49:14 +0200 | [diff] [blame] | 9291 | appctx->ctx.cli.severity = LOG_ERR; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9292 | appctx->ctx.cli.msg = "HAProxy was compiled against a version of OpenSSL that doesn't support OCSP stapling.\n"; |
Willy Tarreau | 3b6e547 | 2016-11-24 15:53:53 +0100 | [diff] [blame] | 9293 | appctx->st0 = CLI_ST_PRINT; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9294 | return 1; |
| 9295 | #endif |
| 9296 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9297 | } |
| 9298 | |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 9299 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9300 | static inline int sample_conv_var2smp_str(const struct arg *arg, struct sample *smp) |
| 9301 | { |
| 9302 | switch (arg->type) { |
| 9303 | case ARGT_STR: |
| 9304 | smp->data.type = SMP_T_STR; |
| 9305 | smp->data.u.str = arg->data.str; |
| 9306 | return 1; |
| 9307 | case ARGT_VAR: |
| 9308 | if (!vars_get_by_desc(&arg->data.var, smp)) |
| 9309 | return 0; |
| 9310 | if (!sample_casts[smp->data.type][SMP_T_STR]) |
| 9311 | return 0; |
| 9312 | if (!sample_casts[smp->data.type][SMP_T_STR](smp)) |
| 9313 | return 0; |
| 9314 | return 1; |
| 9315 | default: |
| 9316 | return 0; |
| 9317 | } |
| 9318 | } |
| 9319 | |
| 9320 | static int check_aes_gcm(struct arg *args, struct sample_conv *conv, |
| 9321 | const char *file, int line, char **err) |
| 9322 | { |
| 9323 | switch(args[0].data.sint) { |
| 9324 | case 128: |
| 9325 | case 192: |
| 9326 | case 256: |
| 9327 | break; |
| 9328 | default: |
| 9329 | memprintf(err, "key size must be 128, 192 or 256 (bits)."); |
| 9330 | return 0; |
| 9331 | } |
| 9332 | /* Try to decode a variable. */ |
| 9333 | vars_check_arg(&args[1], NULL); |
| 9334 | vars_check_arg(&args[2], NULL); |
| 9335 | vars_check_arg(&args[3], NULL); |
| 9336 | return 1; |
| 9337 | } |
| 9338 | |
| 9339 | /* Arguements: AES size in bits, nonce, key, tag. The last three arguments are base64 encoded */ |
| 9340 | static int sample_conv_aes_gcm_dec(const struct arg *arg_p, struct sample *smp, void *private) |
| 9341 | { |
| 9342 | struct sample nonce, key, aead_tag; |
| 9343 | struct buffer *smp_trash, *smp_trash_alloc; |
| 9344 | EVP_CIPHER_CTX *ctx; |
| 9345 | int dec_size, ret; |
| 9346 | |
| 9347 | smp_set_owner(&nonce, smp->px, smp->sess, smp->strm, smp->opt); |
| 9348 | if (!sample_conv_var2smp_str(&arg_p[1], &nonce)) |
| 9349 | return 0; |
| 9350 | |
| 9351 | smp_set_owner(&key, smp->px, smp->sess, smp->strm, smp->opt); |
| 9352 | if (!sample_conv_var2smp_str(&arg_p[2], &key)) |
| 9353 | return 0; |
| 9354 | |
| 9355 | smp_set_owner(&aead_tag, smp->px, smp->sess, smp->strm, smp->opt); |
| 9356 | if (!sample_conv_var2smp_str(&arg_p[3], &aead_tag)) |
| 9357 | return 0; |
| 9358 | |
| 9359 | smp_trash = get_trash_chunk(); |
| 9360 | smp_trash_alloc = alloc_trash_chunk(); |
| 9361 | if (!smp_trash_alloc) |
| 9362 | return 0; |
| 9363 | |
| 9364 | ctx = EVP_CIPHER_CTX_new(); |
| 9365 | |
| 9366 | if (!ctx) |
| 9367 | goto err; |
| 9368 | |
| 9369 | dec_size = base64dec(nonce.data.u.str.area, nonce.data.u.str.data, smp_trash->area, smp_trash->size); |
| 9370 | if (dec_size < 0) |
| 9371 | goto err; |
| 9372 | smp_trash->data = dec_size; |
| 9373 | |
| 9374 | /* Set cipher type and mode */ |
| 9375 | switch(arg_p[0].data.sint) { |
| 9376 | case 128: |
| 9377 | EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL); |
| 9378 | break; |
| 9379 | case 192: |
| 9380 | EVP_DecryptInit_ex(ctx, EVP_aes_192_gcm(), NULL, NULL, NULL); |
| 9381 | break; |
| 9382 | case 256: |
| 9383 | EVP_DecryptInit_ex(ctx, EVP_aes_256_gcm(), NULL, NULL, NULL); |
| 9384 | break; |
| 9385 | } |
| 9386 | |
| 9387 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, smp_trash->data, NULL); |
| 9388 | |
| 9389 | /* Initialise IV */ |
| 9390 | if(!EVP_DecryptInit_ex(ctx, NULL, NULL, NULL, (unsigned char *) smp_trash->area)) |
| 9391 | goto err; |
| 9392 | |
| 9393 | dec_size = base64dec(key.data.u.str.area, key.data.u.str.data, smp_trash->area, smp_trash->size); |
| 9394 | if (dec_size < 0) |
| 9395 | goto err; |
| 9396 | smp_trash->data = dec_size; |
| 9397 | |
| 9398 | /* Initialise key */ |
| 9399 | if (!EVP_DecryptInit_ex(ctx, NULL, NULL, (unsigned char *) smp_trash->area, NULL)) |
| 9400 | goto err; |
| 9401 | |
| 9402 | if (!EVP_DecryptUpdate(ctx, (unsigned char *) smp_trash->area, (int *) &smp_trash->data, |
| 9403 | (unsigned char *) smp->data.u.str.area, (int) smp->data.u.str.data)) |
| 9404 | goto err; |
| 9405 | |
| 9406 | dec_size = base64dec(aead_tag.data.u.str.area, aead_tag.data.u.str.data, smp_trash_alloc->area, smp_trash_alloc->size); |
| 9407 | if (dec_size < 0) |
| 9408 | goto err; |
| 9409 | smp_trash_alloc->data = dec_size; |
| 9410 | dec_size = smp_trash->data; |
| 9411 | |
| 9412 | EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, smp_trash_alloc->data, (void *) smp_trash_alloc->area); |
| 9413 | ret = EVP_DecryptFinal_ex(ctx, (unsigned char *) smp_trash->area + smp_trash->data, (int *) &smp_trash->data); |
| 9414 | |
| 9415 | if (ret <= 0) |
| 9416 | goto err; |
| 9417 | |
| 9418 | smp->data.u.str.data = dec_size + smp_trash->data; |
| 9419 | smp->data.u.str.area = smp_trash->area; |
| 9420 | smp->data.type = SMP_T_BIN; |
| 9421 | smp->flags &= ~SMP_F_CONST; |
| 9422 | free_trash_chunk(smp_trash_alloc); |
| 9423 | return 1; |
| 9424 | |
| 9425 | err: |
| 9426 | free_trash_chunk(smp_trash_alloc); |
| 9427 | return 0; |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9428 | } |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9429 | # endif |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9430 | |
| 9431 | /* register cli keywords */ |
| 9432 | static struct cli_kw_list cli_kws = {{ },{ |
| 9433 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9434 | { { "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] | 9435 | { { "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] | 9436 | #endif |
Emmanuel Hocdet | fdec789 | 2017-01-13 17:48:18 +0100 | [diff] [blame] | 9437 | { { "set", "ssl", "ocsp-response", NULL }, NULL, cli_parse_set_ocspresponse, NULL }, |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9438 | { { NULL }, NULL, NULL, NULL } |
| 9439 | }}; |
| 9440 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9441 | INITCALL1(STG_REGISTER, cli_register_kw, &cli_kws); |
William Lallemand | 32af203 | 2016-10-29 18:09:35 +0200 | [diff] [blame] | 9442 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9443 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9444 | * Please take care of keeping this list alphabetically sorted. |
| 9445 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 9446 | static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, { |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9447 | { "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] | 9448 | { "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] | 9449 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Jérôme Magnin | e064a80 | 2018-12-03 22:21:04 +0100 | [diff] [blame] | 9450 | { "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] | 9451 | #endif |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9452 | { "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] | 9453 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
| 9454 | { "ssl_bc_npn", smp_fetch_ssl_fc_npn, 0, NULL, SMP_T_STR, SMP_USE_L5SRV }, |
| 9455 | #endif |
Emeric Brun | 74f7ffa | 2018-02-19 16:14:12 +0100 | [diff] [blame] | 9456 | { "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] | 9457 | { "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] | 9458 | { "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] | 9459 | { "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] | 9460 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | 645ae79 | 2014-04-30 14:21:06 +0200 | [diff] [blame] | 9461 | { "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] | 9462 | #endif |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9463 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 9464 | { "ssl_bc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5SRV }, |
| 9465 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9466 | { "ssl_c_ca_err", smp_fetch_ssl_c_ca_err, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 9467 | { "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] | 9468 | { "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] | 9469 | { "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] | 9470 | { "ssl_c_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9471 | { "ssl_c_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9472 | { "ssl_c_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9473 | { "ssl_c_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9474 | { "ssl_c_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9475 | { "ssl_c_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9476 | { "ssl_c_serial", smp_fetch_ssl_x_serial, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9477 | { "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] | 9478 | { "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] | 9479 | { "ssl_c_verify", smp_fetch_ssl_c_verify, 0, NULL, SMP_T_SINT, SMP_USE_L5CLI }, |
| 9480 | { "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] | 9481 | { "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] | 9482 | { "ssl_f_i_dn", smp_fetch_ssl_x_i_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9483 | { "ssl_f_key_alg", smp_fetch_ssl_x_key_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9484 | { "ssl_f_notafter", smp_fetch_ssl_x_notafter, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9485 | { "ssl_f_notbefore", smp_fetch_ssl_x_notbefore, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9486 | { "ssl_f_sig_alg", smp_fetch_ssl_x_sig_alg, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9487 | { "ssl_f_s_dn", smp_fetch_ssl_x_s_dn, ARG2(0,STR,SINT), NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9488 | { "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] | 9489 | { "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] | 9490 | { "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] | 9491 | { "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] | 9492 | { "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] | 9493 | { "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] | 9494 | { "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] | 9495 | { "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] | 9496 | { "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] | 9497 | { "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] | 9498 | #if defined(OPENSSL_NPN_NEGOTIATED) && !defined(OPENSSL_NO_NEXTPROTONEG) |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9499 | { "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] | 9500 | #endif |
Dirkjan Bussink | 48f1c4e | 2014-02-13 12:29:42 +0100 | [diff] [blame] | 9501 | #ifdef TLSEXT_TYPE_application_layer_protocol_negotiation |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9502 | { "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] | 9503 | #endif |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9504 | { "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] | 9505 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Emeric Brun | b73a9b0 | 2014-04-30 18:49:19 +0200 | [diff] [blame] | 9506 | { "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] | 9507 | #endif |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 9508 | { "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] | 9509 | #if HA_OPENSSL_VERSION_NUMBER > 0x0090800fL |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9510 | { "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] | 9511 | #endif |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9512 | #if HA_OPENSSL_VERSION_NUMBER >= 0x10100000L || defined(OPENSSL_IS_BORINGSSL) |
Patrick Hemmer | e027547 | 2018-04-28 19:15:51 -0400 | [diff] [blame] | 9513 | { "ssl_fc_session_key", smp_fetch_ssl_fc_session_key, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9514 | #endif |
Patrick Hemmer | 4196677 | 2018-04-28 19:15:48 -0400 | [diff] [blame] | 9515 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
Thierry FOURNIER | 7654c9f | 2013-12-17 00:20:33 +0100 | [diff] [blame] | 9516 | { "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] | 9517 | #endif |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9518 | { "ssl_fc_cipherlist_bin", smp_fetch_ssl_fc_cl_bin, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9519 | { "ssl_fc_cipherlist_hex", smp_fetch_ssl_fc_cl_hex, 0, NULL, SMP_T_BIN, SMP_USE_L5CLI }, |
| 9520 | { "ssl_fc_cipherlist_str", smp_fetch_ssl_fc_cl_str, 0, NULL, SMP_T_STR, SMP_USE_L5CLI }, |
| 9521 | { "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] | 9522 | { NULL, NULL, 0, 0, 0 }, |
| 9523 | }}; |
| 9524 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9525 | INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords); |
| 9526 | |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9527 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9528 | * Please take care of keeping this list alphabetically sorted. |
| 9529 | */ |
Willy Tarreau | dc13c11 | 2013-06-21 23:16:39 +0200 | [diff] [blame] | 9530 | static struct acl_kw_list acl_kws = {ILH, { |
Thierry FOURNIER | c5a4e98 | 2014-03-05 16:07:08 +0100 | [diff] [blame] | 9531 | { "ssl_fc_sni_end", "ssl_fc_sni", PAT_MATCH_END }, |
| 9532 | { "ssl_fc_sni_reg", "ssl_fc_sni", PAT_MATCH_REG }, |
Willy Tarreau | 8ed669b | 2013-01-11 15:49:37 +0100 | [diff] [blame] | 9533 | { /* END */ }, |
Willy Tarreau | 7875d09 | 2012-09-10 08:20:03 +0200 | [diff] [blame] | 9534 | }}; |
| 9535 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9536 | INITCALL1(STG_REGISTER, acl_register_keywords, &acl_kws); |
| 9537 | |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9538 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9539 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 9540 | * all code contributors. |
| 9541 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 9542 | * the config parser can report an appropriate error when a known keyword was |
| 9543 | * not enabled. |
| 9544 | */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9545 | static struct ssl_bind_kw ssl_bind_kws[] = { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9546 | { "allow-0rtt", ssl_bind_parse_allow_0rtt, 0 }, /* allow 0-RTT */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9547 | { "alpn", ssl_bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 9548 | { "ca-file", ssl_bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 9549 | { "ciphers", ssl_bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9550 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9551 | { "ciphersuites", ssl_bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 9552 | #endif |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9553 | { "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] | 9554 | { "curves", ssl_bind_parse_curves, 1 }, /* set SSL curve suite */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9555 | { "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] | 9556 | { "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] | 9557 | { "npn", ssl_bind_parse_npn, 1 }, /* set NPN supported protocols */ |
Emmanuel Hocdet | df701a2 | 2017-05-18 12:46:50 +0200 | [diff] [blame] | 9558 | { "ssl-min-ver", ssl_bind_parse_tls_method_minmax,1 }, /* minimum version */ |
| 9559 | { "ssl-max-ver", ssl_bind_parse_tls_method_minmax,1 }, /* maximum version */ |
Emmanuel Hocdet | 9826329 | 2016-12-29 18:26:15 +0100 | [diff] [blame] | 9560 | { "verify", ssl_bind_parse_verify, 1 }, /* set SSL verify method */ |
| 9561 | { NULL, NULL, 0 }, |
| 9562 | }; |
| 9563 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9564 | /* no initcall for ssl_bind_kws, these ones are parsed in the parser loop */ |
| 9565 | |
Willy Tarreau | 51fb765 | 2012-09-18 18:24:39 +0200 | [diff] [blame] | 9566 | static struct bind_kw_list bind_kws = { "SSL", { }, { |
Olivier Houchard | c2aae74 | 2017-09-22 18:26:28 +0200 | [diff] [blame] | 9567 | { "allow-0rtt", bind_parse_allow_0rtt, 0 }, /* Allow 0RTT */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9568 | { "alpn", bind_parse_alpn, 1 }, /* set ALPN supported protocols */ |
| 9569 | { "ca-file", bind_parse_ca_file, 1 }, /* set CAfile to process verify on client cert */ |
| 9570 | { "ca-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ignore on verify depth > 0 */ |
| 9571 | { "ca-sign-file", bind_parse_ca_sign_file, 1 }, /* set CAFile used to generate and sign server certs */ |
| 9572 | { "ca-sign-pass", bind_parse_ca_sign_pass, 1 }, /* set CAKey passphrase */ |
| 9573 | { "ciphers", bind_parse_ciphers, 1 }, /* set SSL cipher suite */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9574 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9575 | { "ciphersuites", bind_parse_ciphersuites, 1 }, /* set TLS 1.3 cipher suite */ |
| 9576 | #endif |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9577 | { "crl-file", bind_parse_crl_file, 1 }, /* set certificat revocation list file use on client cert verify */ |
| 9578 | { "crt", bind_parse_crt, 1 }, /* load SSL certificates from this location */ |
| 9579 | { "crt-ignore-err", bind_parse_ignore_err, 1 }, /* set error IDs to ingore on verify depth == 0 */ |
| 9580 | { "crt-list", bind_parse_crt_list, 1 }, /* load a list of crt from this location */ |
| 9581 | { "curves", bind_parse_curves, 1 }, /* set SSL curve suite */ |
| 9582 | { "ecdhe", bind_parse_ecdhe, 1 }, /* defines named curve for elliptic curve Diffie-Hellman */ |
| 9583 | { "force-sslv3", bind_parse_tls_method_options, 0 }, /* force SSLv3 */ |
| 9584 | { "force-tlsv10", bind_parse_tls_method_options, 0 }, /* force TLSv10 */ |
| 9585 | { "force-tlsv11", bind_parse_tls_method_options, 0 }, /* force TLSv11 */ |
| 9586 | { "force-tlsv12", bind_parse_tls_method_options, 0 }, /* force TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9587 | { "force-tlsv13", bind_parse_tls_method_options, 0 }, /* force TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9588 | { "generate-certificates", bind_parse_generate_certs, 0 }, /* enable the server certificates generation */ |
Emmanuel Hocdet | 174dfe5 | 2017-07-28 15:01:05 +0200 | [diff] [blame] | 9589 | { "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] | 9590 | { "no-sslv3", bind_parse_tls_method_options, 0 }, /* disable SSLv3 */ |
| 9591 | { "no-tlsv10", bind_parse_tls_method_options, 0 }, /* disable TLSv10 */ |
| 9592 | { "no-tlsv11", bind_parse_tls_method_options, 0 }, /* disable TLSv11 */ |
| 9593 | { "no-tlsv12", bind_parse_tls_method_options, 0 }, /* disable TLSv12 */ |
Emmanuel Hocdet | 42fb980 | 2017-03-30 19:29:39 +0200 | [diff] [blame] | 9594 | { "no-tlsv13", bind_parse_tls_method_options, 0 }, /* disable TLSv13 */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9595 | { "no-tls-tickets", bind_parse_no_tls_tickets, 0 }, /* disable session resumption tickets */ |
| 9596 | { "ssl", bind_parse_ssl, 0 }, /* enable SSL processing */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9597 | { "ssl-min-ver", bind_parse_tls_method_minmax, 1 }, /* minimum version */ |
| 9598 | { "ssl-max-ver", bind_parse_tls_method_minmax, 1 }, /* maximum version */ |
Emmanuel Hocdet | 5db33cb | 2017-03-30 19:19:37 +0200 | [diff] [blame] | 9599 | { "strict-sni", bind_parse_strict_sni, 0 }, /* refuse negotiation if sni doesn't match a certificate */ |
| 9600 | { "tls-ticket-keys", bind_parse_tls_ticket_keys, 1 }, /* set file to load TLS ticket keys from */ |
| 9601 | { "verify", bind_parse_verify, 1 }, /* set SSL verify method */ |
| 9602 | { "npn", bind_parse_npn, 1 }, /* set NPN supported protocols */ |
| 9603 | { "prefer-client-ciphers", bind_parse_pcc, 0 }, /* prefer client ciphers */ |
Willy Tarreau | 79eeafa | 2012-09-14 07:53:05 +0200 | [diff] [blame] | 9604 | { NULL, NULL, 0 }, |
| 9605 | }}; |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9606 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9607 | INITCALL1(STG_REGISTER, bind_register_keywords, &bind_kws); |
| 9608 | |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9609 | /* Note: must not be declared <const> as its list will be overwritten. |
| 9610 | * Please take care of keeping this list alphabetically sorted, doing so helps |
| 9611 | * all code contributors. |
| 9612 | * Optional keywords are also declared with a NULL ->parse() function so that |
| 9613 | * the config parser can report an appropriate error when a known keyword was |
| 9614 | * not enabled. |
| 9615 | */ |
| 9616 | static struct srv_kw_list srv_kws = { "SSL", { }, { |
Olivier Houchard | 522eea7 | 2017-11-03 16:27:47 +0100 | [diff] [blame] | 9617 | { "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] | 9618 | { "alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9619 | { "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] | 9620 | { "check-alpn", srv_parse_alpn, 1, 1 }, /* Set ALPN used for checks */ |
Olivier Houchard | 9130a96 | 2017-10-17 17:33:43 +0200 | [diff] [blame] | 9621 | { "check-sni", srv_parse_check_sni, 1, 1 }, /* set SNI */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9622 | { "check-ssl", srv_parse_check_ssl, 0, 1 }, /* enable SSL for health checks */ |
| 9623 | { "ciphers", srv_parse_ciphers, 1, 1 }, /* select the cipher suite */ |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9624 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9625 | { "ciphersuites", srv_parse_ciphersuites, 1, 1 }, /* select the cipher suite */ |
| 9626 | #endif |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9627 | { "crl-file", srv_parse_crl_file, 1, 1 }, /* set certificate revocation list file use on server cert verify */ |
| 9628 | { "crt", srv_parse_crt, 1, 1 }, /* set client certificate */ |
| 9629 | { "force-sslv3", srv_parse_tls_method_options, 0, 1 }, /* force SSLv3 */ |
| 9630 | { "force-tlsv10", srv_parse_tls_method_options, 0, 1 }, /* force TLSv10 */ |
| 9631 | { "force-tlsv11", srv_parse_tls_method_options, 0, 1 }, /* force TLSv11 */ |
| 9632 | { "force-tlsv12", srv_parse_tls_method_options, 0, 1 }, /* force TLSv12 */ |
| 9633 | { "force-tlsv13", srv_parse_tls_method_options, 0, 1 }, /* force TLSv13 */ |
| 9634 | { "no-check-ssl", srv_parse_no_check_ssl, 0, 1 }, /* disable SSL for health checks */ |
| 9635 | { "no-send-proxy-v2-ssl", srv_parse_no_send_proxy_ssl, 0, 1 }, /* do not send PROXY protocol header v2 with SSL info */ |
| 9636 | { "no-send-proxy-v2-ssl-cn", srv_parse_no_send_proxy_cn, 0, 1 }, /* do not send PROXY protocol header v2 with CN */ |
| 9637 | { "no-ssl", srv_parse_no_ssl, 0, 1 }, /* disable SSL processing */ |
| 9638 | { "no-ssl-reuse", srv_parse_no_ssl_reuse, 0, 1 }, /* disable session reuse */ |
| 9639 | { "no-sslv3", srv_parse_tls_method_options, 0, 0 }, /* disable SSLv3 */ |
| 9640 | { "no-tlsv10", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv10 */ |
| 9641 | { "no-tlsv11", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv11 */ |
| 9642 | { "no-tlsv12", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv12 */ |
| 9643 | { "no-tlsv13", srv_parse_tls_method_options, 0, 0 }, /* disable TLSv13 */ |
| 9644 | { "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] | 9645 | { "npn", srv_parse_npn, 1, 1 }, /* Set NPN supported protocols */ |
Emmanuel Hocdet | e1c722b | 2017-03-31 15:02:54 +0200 | [diff] [blame] | 9646 | { "send-proxy-v2-ssl", srv_parse_send_proxy_ssl, 0, 1 }, /* send PROXY protocol header v2 with SSL info */ |
| 9647 | { "send-proxy-v2-ssl-cn", srv_parse_send_proxy_cn, 0, 1 }, /* send PROXY protocol header v2 with CN */ |
| 9648 | { "sni", srv_parse_sni, 1, 1 }, /* send SNI extension */ |
| 9649 | { "ssl", srv_parse_ssl, 0, 1 }, /* enable SSL processing */ |
| 9650 | { "ssl-min-ver", srv_parse_tls_method_minmax, 1, 1 }, /* minimum version */ |
| 9651 | { "ssl-max-ver", srv_parse_tls_method_minmax, 1, 1 }, /* maximum version */ |
| 9652 | { "ssl-reuse", srv_parse_ssl_reuse, 0, 1 }, /* enable session reuse */ |
| 9653 | { "tls-tickets", srv_parse_tls_tickets, 0, 1 }, /* enable session resumption tickets */ |
| 9654 | { "verify", srv_parse_verify, 1, 1 }, /* set SSL verify method */ |
| 9655 | { "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] | 9656 | { NULL, NULL, 0, 0 }, |
| 9657 | }}; |
| 9658 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9659 | INITCALL1(STG_REGISTER, srv_register_keywords, &srv_kws); |
| 9660 | |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9661 | static struct cfg_kw_list cfg_kws = {ILH, { |
Willy Tarreau | 8c3b0fd | 2016-12-21 22:44:46 +0100 | [diff] [blame] | 9662 | { CFG_GLOBAL, "ca-base", ssl_parse_global_ca_crt_base }, |
| 9663 | { CFG_GLOBAL, "crt-base", ssl_parse_global_ca_crt_base }, |
Willy Tarreau | 0bea58d | 2016-12-21 23:17:25 +0100 | [diff] [blame] | 9664 | { CFG_GLOBAL, "maxsslconn", ssl_parse_global_int }, |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9665 | { CFG_GLOBAL, "ssl-default-bind-options", ssl_parse_default_bind_options }, |
| 9666 | { CFG_GLOBAL, "ssl-default-server-options", ssl_parse_default_server_options }, |
Willy Tarreau | 14e36a1 | 2016-12-21 23:28:13 +0100 | [diff] [blame] | 9667 | #ifndef OPENSSL_NO_DH |
| 9668 | { CFG_GLOBAL, "ssl-dh-param-file", ssl_parse_global_dh_param_file }, |
| 9669 | #endif |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9670 | { CFG_GLOBAL, "ssl-mode-async", ssl_parse_global_ssl_async }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9671 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9672 | { CFG_GLOBAL, "ssl-engine", ssl_parse_global_ssl_engine }, |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9673 | #endif |
Willy Tarreau | 9ceda38 | 2016-12-21 23:13:03 +0100 | [diff] [blame] | 9674 | { CFG_GLOBAL, "tune.ssl.cachesize", ssl_parse_global_int }, |
| 9675 | #ifndef OPENSSL_NO_DH |
| 9676 | { CFG_GLOBAL, "tune.ssl.default-dh-param", ssl_parse_global_default_dh }, |
| 9677 | #endif |
| 9678 | { CFG_GLOBAL, "tune.ssl.force-private-cache", ssl_parse_global_private_cache }, |
| 9679 | { CFG_GLOBAL, "tune.ssl.lifetime", ssl_parse_global_lifetime }, |
| 9680 | { CFG_GLOBAL, "tune.ssl.maxrecord", ssl_parse_global_int }, |
| 9681 | { CFG_GLOBAL, "tune.ssl.ssl-ctx-cache-size", ssl_parse_global_int }, |
Thierry FOURNIER | 5bf7732 | 2017-02-25 12:45:22 +0100 | [diff] [blame] | 9682 | { CFG_GLOBAL, "tune.ssl.capture-cipherlist-size", ssl_parse_global_capture_cipherlist }, |
Willy Tarreau | f22e968 | 2016-12-21 23:23:19 +0100 | [diff] [blame] | 9683 | { CFG_GLOBAL, "ssl-default-bind-ciphers", ssl_parse_global_ciphers }, |
| 9684 | { CFG_GLOBAL, "ssl-default-server-ciphers", ssl_parse_global_ciphers }, |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9685 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9686 | { CFG_GLOBAL, "ssl-default-bind-ciphersuites", ssl_parse_global_ciphersuites }, |
| 9687 | { CFG_GLOBAL, "ssl-default-server-ciphersuites", ssl_parse_global_ciphersuites }, |
| 9688 | #endif |
Emeric Brun | 2c86cbf | 2014-10-30 15:56:50 +0100 | [diff] [blame] | 9689 | { 0, NULL, NULL }, |
| 9690 | }}; |
| 9691 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9692 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 9693 | |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9694 | /* Note: must not be declared <const> as its list will be overwritten */ |
| 9695 | static struct sample_conv_kw_list conv_kws = {ILH, { |
Willy Tarreau | 86a394e | 2019-05-09 14:15:32 +0200 | [diff] [blame] | 9696 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x1000100fL) |
Nenad Merdanovic | c31499d | 2019-03-23 11:00:32 +0100 | [diff] [blame] | 9697 | { "aes_gcm_dec", sample_conv_aes_gcm_dec, ARG4(4,SINT,STR,STR,STR), check_aes_gcm, SMP_T_BIN, SMP_T_BIN }, |
| 9698 | #endif |
| 9699 | { NULL, NULL, 0, 0, 0 }, |
| 9700 | }}; |
| 9701 | |
| 9702 | INITCALL1(STG_REGISTER, sample_register_convs, &conv_kws); |
| 9703 | |
Willy Tarreau | f7bc57c | 2012-10-03 00:19:48 +0200 | [diff] [blame] | 9704 | /* transport-layer operations for SSL sockets */ |
Willy Tarreau | d9f5cca | 2016-12-22 21:08:52 +0100 | [diff] [blame] | 9705 | static struct xprt_ops ssl_sock = { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9706 | .snd_buf = ssl_sock_from_buf, |
| 9707 | .rcv_buf = ssl_sock_to_buf, |
Olivier Houchard | df35784 | 2019-03-21 16:30:07 +0100 | [diff] [blame] | 9708 | .subscribe = ssl_subscribe, |
| 9709 | .unsubscribe = ssl_unsubscribe, |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9710 | .rcv_pipe = NULL, |
| 9711 | .snd_pipe = NULL, |
| 9712 | .shutr = NULL, |
| 9713 | .shutw = ssl_sock_shutw, |
| 9714 | .close = ssl_sock_close, |
| 9715 | .init = ssl_sock_init, |
Willy Tarreau | 55d3791 | 2016-12-21 23:38:39 +0100 | [diff] [blame] | 9716 | .prepare_bind_conf = ssl_sock_prepare_bind_conf, |
Willy Tarreau | 795cdab | 2016-12-22 17:30:54 +0100 | [diff] [blame] | 9717 | .destroy_bind_conf = ssl_sock_destroy_bind_conf, |
Willy Tarreau | 17d4538 | 2016-12-22 21:16:08 +0100 | [diff] [blame] | 9718 | .prepare_srv = ssl_sock_prepare_srv_ctx, |
| 9719 | .destroy_srv = ssl_sock_free_srv_ctx, |
Willy Tarreau | 8743f7e | 2016-12-04 18:44:29 +0100 | [diff] [blame] | 9720 | .get_alpn = ssl_sock_get_alpn, |
Willy Tarreau | 8e0bb0a | 2016-11-24 16:58:12 +0100 | [diff] [blame] | 9721 | .name = "SSL", |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9722 | }; |
| 9723 | |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9724 | enum act_return ssl_action_wait_for_hs(struct act_rule *rule, struct proxy *px, |
| 9725 | struct session *sess, struct stream *s, int flags) |
| 9726 | { |
| 9727 | struct connection *conn; |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9728 | struct conn_stream *cs; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9729 | |
| 9730 | conn = objt_conn(sess->origin); |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9731 | cs = objt_cs(s->si[0].end); |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9732 | |
Olivier Houchard | 6fa63d9 | 2017-11-27 18:41:32 +0100 | [diff] [blame] | 9733 | if (conn && cs) { |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9734 | 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] | 9735 | cs->flags |= CS_FL_WAIT_FOR_HS; |
Olivier Houchard | ccaa7de | 2017-10-02 11:51:03 +0200 | [diff] [blame] | 9736 | s->req.flags |= CF_READ_NULL; |
| 9737 | return ACT_RET_YIELD; |
| 9738 | } |
| 9739 | } |
| 9740 | return (ACT_RET_CONT); |
| 9741 | } |
| 9742 | |
| 9743 | 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) |
| 9744 | { |
| 9745 | rule->action_ptr = ssl_action_wait_for_hs; |
| 9746 | |
| 9747 | return ACT_RET_PRS_OK; |
| 9748 | } |
| 9749 | |
| 9750 | static struct action_kw_list http_req_actions = {ILH, { |
| 9751 | { "wait-for-handshake", ssl_parse_wait_for_hs }, |
| 9752 | { /* END */ } |
| 9753 | }}; |
| 9754 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 9755 | INITCALL1(STG_REGISTER, http_req_keywords_register, &http_req_actions); |
| 9756 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9757 | #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] | 9758 | |
| 9759 | static void ssl_sock_sctl_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 9760 | { |
| 9761 | if (ptr) { |
| 9762 | chunk_destroy(ptr); |
| 9763 | free(ptr); |
| 9764 | } |
| 9765 | } |
| 9766 | |
| 9767 | #endif |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 9768 | static void ssl_sock_capture_free_func(void *parent, void *ptr, CRYPTO_EX_DATA *ad, int idx, long argl, void *argp) |
| 9769 | { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9770 | pool_free(pool_head_ssl_capture, ptr); |
Emmanuel Hocdet | aaee750 | 2017-03-07 18:34:58 +0100 | [diff] [blame] | 9771 | } |
Janusz Dziemidowicz | 2c701b5 | 2015-03-07 23:03:59 +0100 | [diff] [blame] | 9772 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9773 | __attribute__((constructor)) |
Willy Tarreau | 92faadf | 2012-10-10 23:04:25 +0200 | [diff] [blame] | 9774 | static void __ssl_sock_init(void) |
| 9775 | { |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9776 | STACK_OF(SSL_COMP)* cm; |
| 9777 | |
Willy Tarreau | ef93460 | 2016-12-22 23:12:01 +0100 | [diff] [blame] | 9778 | if (global_ssl.listen_default_ciphers) |
| 9779 | global_ssl.listen_default_ciphers = strdup(global_ssl.listen_default_ciphers); |
| 9780 | if (global_ssl.connect_default_ciphers) |
| 9781 | global_ssl.connect_default_ciphers = strdup(global_ssl.connect_default_ciphers); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9782 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x10101000L && !defined OPENSSL_IS_BORINGSSL) |
Dirkjan Bussink | 415150f | 2018-09-14 11:14:21 +0200 | [diff] [blame] | 9783 | if (global_ssl.listen_default_ciphersuites) |
| 9784 | global_ssl.listen_default_ciphersuites = strdup(global_ssl.listen_default_ciphersuites); |
| 9785 | if (global_ssl.connect_default_ciphersuites) |
| 9786 | global_ssl.connect_default_ciphersuites = strdup(global_ssl.connect_default_ciphersuites); |
| 9787 | #endif |
Willy Tarreau | 610f04b | 2014-02-13 11:36:41 +0100 | [diff] [blame] | 9788 | |
Willy Tarreau | 13e1410 | 2016-12-22 20:25:26 +0100 | [diff] [blame] | 9789 | xprt_register(XPRT_SSL, &ssl_sock); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9790 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9791 | SSL_library_init(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9792 | #endif |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9793 | cm = SSL_COMP_get_compression_methods(); |
| 9794 | sk_SSL_COMP_zero(cm); |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9795 | #if defined(USE_THREAD) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9796 | ssl_locking_init(); |
| 9797 | #endif |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9798 | #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] | 9799 | sctl_ex_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, ssl_sock_sctl_free_func); |
| 9800 | #endif |
Thierry FOURNIER | 28962c9 | 2018-06-17 21:37:05 +0200 | [diff] [blame] | 9801 | 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] | 9802 | 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] | 9803 | 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] | 9804 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9805 | ENGINE_load_builtin_engines(); |
Grant Zhang | fa6c7ee | 2017-01-14 01:42:15 +0000 | [diff] [blame] | 9806 | hap_register_post_check(ssl_check_async_engine_count); |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9807 | #endif |
Willy Tarreau | d1c5750 | 2016-12-22 22:46:15 +0100 | [diff] [blame] | 9808 | #if (defined SSL_CTRL_SET_TLSEXT_TICKET_KEY_CB && TLS_TICKETS_NO > 0) |
| 9809 | hap_register_post_check(tlskeys_finalize_config); |
| 9810 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9811 | |
| 9812 | global.ssl_session_max_cost = SSL_SESSION_MAX_COST; |
| 9813 | global.ssl_handshake_max_cost = SSL_HANDSHAKE_MAX_COST; |
| 9814 | |
| 9815 | #ifndef OPENSSL_NO_DH |
| 9816 | ssl_dh_ptr_index = SSL_CTX_get_ex_new_index(0, NULL, NULL, NULL, NULL); |
| 9817 | hap_register_post_deinit(ssl_free_dh); |
| 9818 | #endif |
| 9819 | #ifndef OPENSSL_NO_ENGINE |
| 9820 | hap_register_post_deinit(ssl_free_engines); |
| 9821 | #endif |
| 9822 | /* Load SSL string for the verbose & debug mode. */ |
| 9823 | ERR_load_SSL_strings(); |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9824 | #if HA_OPENSSL_VERSION_NUMBER < 0x10100000L |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 9825 | ha_meth = malloc(sizeof(*ha_meth)); |
Olivier Houchard | 66a7b33 | 2019-04-18 15:58:15 +0200 | [diff] [blame] | 9826 | bzero(ha_meth, sizeof(*ha_meth)); |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 9827 | ha_meth->bwrite = ha_ssl_write; |
| 9828 | ha_meth->bread = ha_ssl_read; |
| 9829 | ha_meth->ctrl = ha_ssl_ctrl; |
| 9830 | ha_meth->create = ha_ssl_new; |
| 9831 | ha_meth->destroy = ha_ssl_free; |
| 9832 | ha_meth->bputs = ha_ssl_puts; |
| 9833 | ha_meth->bgets = ha_ssl_gets; |
| 9834 | #else |
| 9835 | ha_meth = BIO_meth_new(0x666, "ha methods"); |
| 9836 | BIO_meth_set_write(ha_meth, ha_ssl_write); |
| 9837 | BIO_meth_set_read(ha_meth, ha_ssl_read); |
| 9838 | BIO_meth_set_ctrl(ha_meth, ha_ssl_ctrl); |
| 9839 | BIO_meth_set_create(ha_meth, ha_ssl_new); |
| 9840 | BIO_meth_set_destroy(ha_meth, ha_ssl_free); |
| 9841 | BIO_meth_set_puts(ha_meth, ha_ssl_puts); |
| 9842 | BIO_meth_set_gets(ha_meth, ha_ssl_gets); |
| 9843 | #endif |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9844 | } |
Willy Tarreau | d92aa5c | 2015-01-15 21:34:39 +0100 | [diff] [blame] | 9845 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9846 | /* Compute and register the version string */ |
| 9847 | static void ssl_register_build_options() |
| 9848 | { |
| 9849 | char *ptr = NULL; |
| 9850 | int i; |
| 9851 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9852 | memprintf(&ptr, "Built with OpenSSL version : " |
| 9853 | #ifdef OPENSSL_IS_BORINGSSL |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9854 | "BoringSSL"); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9855 | #else /* OPENSSL_IS_BORINGSSL */ |
| 9856 | OPENSSL_VERSION_TEXT |
| 9857 | "\nRunning on OpenSSL version : %s%s", |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9858 | OpenSSL_version(OPENSSL_VERSION), |
Willy Tarreau | 1d158ab | 2019-05-09 13:41:45 +0200 | [diff] [blame] | 9859 | ((OPENSSL_VERSION_NUMBER ^ OpenSSL_version_num()) >> 8) ? " (VERSIONS DIFFER!)" : ""); |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9860 | #endif |
| 9861 | memprintf(&ptr, "%s\nOpenSSL library supports TLS extensions : " |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9862 | #if HA_OPENSSL_VERSION_NUMBER < 0x00907000L |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9863 | "no (library version too old)" |
| 9864 | #elif defined(OPENSSL_NO_TLSEXT) |
| 9865 | "no (disabled via OPENSSL_NO_TLSEXT)" |
| 9866 | #else |
| 9867 | "yes" |
| 9868 | #endif |
| 9869 | "", ptr); |
| 9870 | |
| 9871 | memprintf(&ptr, "%s\nOpenSSL library supports SNI : " |
| 9872 | #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME |
| 9873 | "yes" |
| 9874 | #else |
| 9875 | #ifdef OPENSSL_NO_TLSEXT |
| 9876 | "no (because of OPENSSL_NO_TLSEXT)" |
| 9877 | #else |
| 9878 | "no (version might be too old, 0.9.8f min needed)" |
| 9879 | #endif |
| 9880 | #endif |
| 9881 | "", ptr); |
| 9882 | |
Emmanuel Hocdet | f80bc24 | 2017-07-12 14:25:38 +0200 | [diff] [blame] | 9883 | memprintf(&ptr, "%s\nOpenSSL library supports :", ptr); |
| 9884 | for (i = CONF_TLSV_MIN; i <= CONF_TLSV_MAX; i++) |
| 9885 | if (methodVersions[i].option) |
| 9886 | memprintf(&ptr, "%s %s", ptr, methodVersions[i].name); |
Emmanuel Hocdet | 50e25e1 | 2017-03-24 15:20:03 +0100 | [diff] [blame] | 9887 | |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9888 | hap_register_build_opts(ptr, 1); |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9889 | } |
Willy Tarreau | c2c0b61 | 2016-12-21 19:23:20 +0100 | [diff] [blame] | 9890 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 9891 | INITCALL0(STG_REGISTER, ssl_register_build_options); |
Remi Gacogne | 4f902b8 | 2015-05-28 16:23:00 +0200 | [diff] [blame] | 9892 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9893 | |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9894 | #ifndef OPENSSL_NO_ENGINE |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9895 | void ssl_free_engines(void) { |
| 9896 | struct ssl_engine_list *wl, *wlb; |
| 9897 | /* free up engine list */ |
| 9898 | list_for_each_entry_safe(wl, wlb, &openssl_engines, list) { |
| 9899 | ENGINE_finish(wl->e); |
| 9900 | ENGINE_free(wl->e); |
| 9901 | LIST_DEL(&wl->list); |
| 9902 | free(wl); |
| 9903 | } |
| 9904 | } |
Emmanuel Hocdet | 9ac143b | 2017-05-29 14:36:20 +0200 | [diff] [blame] | 9905 | #endif |
Christopher Faulet | 31af49d | 2015-06-09 17:29:50 +0200 | [diff] [blame] | 9906 | |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9907 | #ifndef OPENSSL_NO_DH |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9908 | void ssl_free_dh(void) { |
| 9909 | if (local_dh_1024) { |
| 9910 | DH_free(local_dh_1024); |
| 9911 | local_dh_1024 = NULL; |
| 9912 | } |
| 9913 | if (local_dh_2048) { |
| 9914 | DH_free(local_dh_2048); |
| 9915 | local_dh_2048 = NULL; |
| 9916 | } |
| 9917 | if (local_dh_4096) { |
| 9918 | DH_free(local_dh_4096); |
| 9919 | local_dh_4096 = NULL; |
| 9920 | } |
Remi Gacogne | 47783ef | 2015-05-29 15:53:22 +0200 | [diff] [blame] | 9921 | if (global_dh) { |
| 9922 | DH_free(global_dh); |
| 9923 | global_dh = NULL; |
| 9924 | } |
Grant Zhang | 872f9c2 | 2017-01-21 01:10:18 +0000 | [diff] [blame] | 9925 | } |
| 9926 | #endif |
| 9927 | |
| 9928 | __attribute__((destructor)) |
| 9929 | static void __ssl_sock_deinit(void) |
| 9930 | { |
| 9931 | #if (defined SSL_CTRL_SET_TLSEXT_HOSTNAME && !defined SSL_NO_GENERATE_CERTIFICATES) |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9932 | if (ssl_ctx_lru_tree) { |
| 9933 | lru64_destroy(ssl_ctx_lru_tree); |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 9934 | HA_RWLOCK_DESTROY(&ssl_ctx_lru_rwlock); |
Emeric Brun | 821bb9b | 2017-06-15 16:37:39 +0200 | [diff] [blame] | 9935 | } |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9936 | #endif |
| 9937 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9938 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9939 | ERR_remove_state(0); |
| 9940 | ERR_free_strings(); |
| 9941 | |
| 9942 | EVP_cleanup(); |
Rosen Penev | 6818595 | 2018-12-14 08:47:02 -0800 | [diff] [blame] | 9943 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9944 | |
Willy Tarreau | 5db847a | 2019-05-09 14:13:35 +0200 | [diff] [blame] | 9945 | #if (HA_OPENSSL_VERSION_NUMBER >= 0x00907000L) && (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9946 | CRYPTO_cleanup_all_ex_data(); |
| 9947 | #endif |
Willy Tarreau | 9a1ab08 | 2019-05-09 13:26:41 +0200 | [diff] [blame] | 9948 | #if (HA_OPENSSL_VERSION_NUMBER < 0x10100000L) |
Olivier Houchard | a8955d5 | 2019-04-07 22:00:38 +0200 | [diff] [blame] | 9949 | free(ha_meth); |
| 9950 | #else |
| 9951 | BIO_meth_free(ha_meth); |
| 9952 | #endif |
Remi Gacogne | d3a23c3 | 2015-05-28 16:39:47 +0200 | [diff] [blame] | 9953 | } |
| 9954 | |
| 9955 | |
Emeric Brun | 4659195 | 2012-05-18 15:47:34 +0200 | [diff] [blame] | 9956 | /* |
| 9957 | * Local variables: |
| 9958 | * c-indent-level: 8 |
| 9959 | * c-basic-offset: 8 |
| 9960 | * End: |
| 9961 | */ |